?

Average Error: 40.72% → 10.16%
Time: 13.6s
Precision: binary64
Cost: 1609

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;d \leq -7.5 \cdot 10^{+143} \lor \neg \left(d \leq 5.2 \cdot 10^{+111}\right):\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{d}{d \cdot d + c \cdot c} + \frac{a}{c + \frac{d \cdot d}{c}}\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (or (<= d -7.5e+143) (not (<= d 5.2e+111)))
   (+ (/ b d) (* (/ a d) (/ c d)))
   (+ (* b (/ d (+ (* d d) (* c c)))) (/ a (+ c (/ (* d d) c))))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double tmp;
	if ((d <= -7.5e+143) || !(d <= 5.2e+111)) {
		tmp = (b / d) + ((a / d) * (c / d));
	} else {
		tmp = (b * (d / ((d * d) + (c * c)))) + (a / (c + ((d * d) / c)));
	}
	return tmp;
}
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    real(8) :: tmp
    if ((d <= (-7.5d+143)) .or. (.not. (d <= 5.2d+111))) then
        tmp = (b / d) + ((a / d) * (c / d))
    else
        tmp = (b * (d / ((d * d) + (c * c)))) + (a / (c + ((d * d) / c)))
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	double tmp;
	if ((d <= -7.5e+143) || !(d <= 5.2e+111)) {
		tmp = (b / d) + ((a / d) * (c / d));
	} else {
		tmp = (b * (d / ((d * d) + (c * c)))) + (a / (c + ((d * d) / c)));
	}
	return tmp;
}
def code(a, b, c, d):
	return ((a * c) + (b * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	tmp = 0
	if (d <= -7.5e+143) or not (d <= 5.2e+111):
		tmp = (b / d) + ((a / d) * (c / d))
	else:
		tmp = (b * (d / ((d * d) + (c * c)))) + (a / (c + ((d * d) / c)))
	return tmp
function code(a, b, c, d)
	return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function code(a, b, c, d)
	tmp = 0.0
	if ((d <= -7.5e+143) || !(d <= 5.2e+111))
		tmp = Float64(Float64(b / d) + Float64(Float64(a / d) * Float64(c / d)));
	else
		tmp = Float64(Float64(b * Float64(d / Float64(Float64(d * d) + Float64(c * c)))) + Float64(a / Float64(c + Float64(Float64(d * d) / c))));
	end
	return tmp
end
function tmp = code(a, b, c, d)
	tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
end
function tmp_2 = code(a, b, c, d)
	tmp = 0.0;
	if ((d <= -7.5e+143) || ~((d <= 5.2e+111)))
		tmp = (b / d) + ((a / d) * (c / d));
	else
		tmp = (b * (d / ((d * d) + (c * c)))) + (a / (c + ((d * d) / c)));
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -7.5e+143], N[Not[LessEqual[d, 5.2e+111]], $MachinePrecision]], N[(N[(b / d), $MachinePrecision] + N[(N[(a / d), $MachinePrecision] * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(b * N[(d / N[(N[(d * d), $MachinePrecision] + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(a / N[(c + N[(N[(d * d), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -7.5 \cdot 10^{+143} \lor \neg \left(d \leq 5.2 \cdot 10^{+111}\right):\\
\;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\

\mathbf{else}:\\
\;\;\;\;b \cdot \frac{d}{d \cdot d + c \cdot c} + \frac{a}{c + \frac{d \cdot d}{c}}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original40.72%
Target0.77%
Herbie10.16%
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if d < -7.49999999999999974e143 or 5.1999999999999997e111 < d

    1. Initial program 65.14

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around 0 25.54

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}} \]
    3. Simplified13.56

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}} \]
      Proof

      [Start]25.54

      \[ \frac{b}{d} + \frac{c \cdot a}{{d}^{2}} \]

      *-commutative [<=]25.54

      \[ \frac{b}{d} + \frac{\color{blue}{a \cdot c}}{{d}^{2}} \]

      unpow2 [=>]25.54

      \[ \frac{b}{d} + \frac{a \cdot c}{\color{blue}{d \cdot d}} \]

      times-frac [=>]13.56

      \[ \frac{b}{d} + \color{blue}{\frac{a}{d} \cdot \frac{c}{d}} \]

    if -7.49999999999999974e143 < d < 5.1999999999999997e111

    1. Initial program 29.51

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr18.74

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}} \]
    3. Taylor expanded in a around 0 29.51

      \[\leadsto \color{blue}{\frac{d \cdot b}{{d}^{2} + {c}^{2}} + \frac{c \cdot a}{{d}^{2} + {c}^{2}}} \]
    4. Simplified23.96

      \[\leadsto \color{blue}{\frac{d}{d \cdot d + c \cdot c} \cdot b + \frac{c}{d \cdot d + c \cdot c} \cdot a} \]
      Proof

      [Start]29.51

      \[ \frac{d \cdot b}{{d}^{2} + {c}^{2}} + \frac{c \cdot a}{{d}^{2} + {c}^{2}} \]

      associate-/l* [=>]31.34

      \[ \color{blue}{\frac{d}{\frac{{d}^{2} + {c}^{2}}{b}}} + \frac{c \cdot a}{{d}^{2} + {c}^{2}} \]

      associate-/r/ [=>]27.51

      \[ \color{blue}{\frac{d}{{d}^{2} + {c}^{2}} \cdot b} + \frac{c \cdot a}{{d}^{2} + {c}^{2}} \]

      unpow2 [=>]27.51

      \[ \frac{d}{\color{blue}{d \cdot d} + {c}^{2}} \cdot b + \frac{c \cdot a}{{d}^{2} + {c}^{2}} \]

      unpow2 [=>]27.51

      \[ \frac{d}{d \cdot d + \color{blue}{c \cdot c}} \cdot b + \frac{c \cdot a}{{d}^{2} + {c}^{2}} \]

      associate-/l* [=>]28.67

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{\frac{c}{\frac{{d}^{2} + {c}^{2}}{a}}} \]

      associate-/r/ [=>]23.96

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{\frac{c}{{d}^{2} + {c}^{2}} \cdot a} \]

      unpow2 [=>]23.96

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \frac{c}{\color{blue}{d \cdot d} + {c}^{2}} \cdot a \]

      unpow2 [=>]23.96

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \frac{c}{d \cdot d + \color{blue}{c \cdot c}} \cdot a \]
    5. Applied egg-rr49.24

      \[\leadsto \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{c}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}} \cdot a\right)} - 1\right)} \]
    6. Simplified23.9

      \[\leadsto \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{\frac{a}{\frac{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}{c}}} \]
      Proof

      [Start]49.24

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \left(e^{\mathsf{log1p}\left(\frac{c}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}} \cdot a\right)} - 1\right) \]

      expm1-def [=>]36.63

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{c}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}} \cdot a\right)\right)} \]

      expm1-log1p [=>]23.96

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{\frac{c}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}} \cdot a} \]

      *-commutative [=>]23.96

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{a \cdot \frac{c}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}} \]

      associate-*r/ [=>]27.51

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{\frac{a \cdot c}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}} \]

      associate-/l* [=>]23.9

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \color{blue}{\frac{a}{\frac{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}{c}}} \]
    7. Taylor expanded in d around 0 8.59

      \[\leadsto \frac{d}{d \cdot d + c \cdot c} \cdot b + \frac{a}{\color{blue}{c + \frac{{d}^{2}}{c}}} \]
    8. Simplified8.59

      \[\leadsto \frac{d}{d \cdot d + c \cdot c} \cdot b + \frac{a}{\color{blue}{c + \frac{d \cdot d}{c}}} \]
      Proof

      [Start]8.59

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \frac{a}{c + \frac{{d}^{2}}{c}} \]

      unpow2 [=>]8.59

      \[ \frac{d}{d \cdot d + c \cdot c} \cdot b + \frac{a}{c + \frac{\color{blue}{d \cdot d}}{c}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification10.16

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -7.5 \cdot 10^{+143} \lor \neg \left(d \leq 5.2 \cdot 10^{+111}\right):\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{d}{d \cdot d + c \cdot c} + \frac{a}{c + \frac{d \cdot d}{c}}\\ \end{array} \]

Alternatives

Alternative 1
Error19.83%
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + d \cdot b}{d \cdot d + c \cdot c}\\ t_1 := \frac{1}{c} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \mathbf{if}\;c \leq -4.7 \cdot 10^{+117}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-88}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-124}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d \cdot \frac{d}{c}}\\ \mathbf{elif}\;c \leq 1.5 \cdot 10^{+155}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error24.38%
Cost1232
\[\begin{array}{l} t_0 := \frac{1}{c} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ t_1 := \frac{1}{d} \cdot \left(b + \frac{a}{d} \cdot c\right)\\ \mathbf{if}\;d \leq -4.9 \cdot 10^{+26}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -2.6 \cdot 10^{-104}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -2.6 \cdot 10^{-138}:\\ \;\;\;\;a \cdot \frac{c}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 2.4 \cdot 10^{+48}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error24.35%
Cost1232
\[\begin{array}{l} t_0 := \frac{1}{d} \cdot \left(b + \frac{a}{d} \cdot c\right)\\ \mathbf{if}\;d \leq -4.9 \cdot 10^{+26}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -2.2 \cdot 10^{-104}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -2.6 \cdot 10^{-138}:\\ \;\;\;\;a \cdot \frac{c}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 9.5 \cdot 10^{+47}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error24.44%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{if}\;d \leq -1.7 \cdot 10^{+26}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -2.4 \cdot 10^{-104}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -2.6 \cdot 10^{-138}:\\ \;\;\;\;a \cdot \frac{c}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 1.35 \cdot 10^{+47}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error26.55%
Cost1232
\[\begin{array}{l} t_0 := \frac{1}{c} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \mathbf{if}\;c \leq -1.86 \cdot 10^{-75}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.75 \cdot 10^{-65}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d \cdot \frac{d}{c}}\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{+74}:\\ \;\;\;\;a \cdot \frac{c}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;c \leq 8.2 \cdot 10^{+106}:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error33.77%
Cost1106
\[\begin{array}{l} \mathbf{if}\;c \leq -1.62 \cdot 10^{+66} \lor \neg \left(c \leq 2.1 \cdot 10^{-44}\right) \land \left(c \leq 4.6 \cdot 10^{+73} \lor \neg \left(c \leq 1.05 \cdot 10^{+134}\right)\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \end{array} \]
Alternative 7
Error27.74%
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -7.5 \cdot 10^{-41} \lor \neg \left(c \leq 1.35 \cdot 10^{-73}\right):\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \end{array} \]
Alternative 8
Error23.83%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -3.9 \cdot 10^{+26} \lor \neg \left(d \leq 10^{+46}\right):\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{d} \cdot c\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \end{array} \]
Alternative 9
Error37.84%
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -1.86 \cdot 10^{-75}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.35 \cdot 10^{-73}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 10
Error58.57%
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

herbie shell --seed 2023089 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))

  (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))