?

Average Error: 25.6 → 10.3
Time: 11.0s
Precision: binary64
Cost: 20432

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \mathbf{if}\;d \leq -3.6 \cdot 10^{+106}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -3 \cdot 10^{-138}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.3 \cdot 10^{-181}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;d \leq 4.6 \cdot 10^{+110}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (/ (fma a c (* d b)) (hypot c d)) (hypot c d)))
        (t_1 (+ (/ b d) (/ c (* d (/ d a))))))
   (if (<= d -3.6e+106)
     t_1
     (if (<= d -3e-138)
       t_0
       (if (<= d 2.3e-181)
         (+ (/ a c) (* (/ b c) (/ d c)))
         (if (<= d 4.6e+110) t_0 t_1))))))
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 t_0 = (fma(a, c, (d * b)) / hypot(c, d)) / hypot(c, d);
	double t_1 = (b / d) + (c / (d * (d / a)));
	double tmp;
	if (d <= -3.6e+106) {
		tmp = t_1;
	} else if (d <= -3e-138) {
		tmp = t_0;
	} else if (d <= 2.3e-181) {
		tmp = (a / c) + ((b / c) * (d / c));
	} else if (d <= 4.6e+110) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	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)
	t_0 = Float64(Float64(fma(a, c, Float64(d * b)) / hypot(c, d)) / hypot(c, d))
	t_1 = Float64(Float64(b / d) + Float64(c / Float64(d * Float64(d / a))))
	tmp = 0.0
	if (d <= -3.6e+106)
		tmp = t_1;
	elseif (d <= -3e-138)
		tmp = t_0;
	elseif (d <= 2.3e-181)
		tmp = Float64(Float64(a / c) + Float64(Float64(b / c) * Float64(d / c)));
	elseif (d <= 4.6e+110)
		tmp = t_0;
	else
		tmp = t_1;
	end
	return 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_] := Block[{t$95$0 = N[(N[(N[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b / d), $MachinePrecision] + N[(c / N[(d * N[(d / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -3.6e+106], t$95$1, If[LessEqual[d, -3e-138], t$95$0, If[LessEqual[d, 2.3e-181], N[(N[(a / c), $MachinePrecision] + N[(N[(b / c), $MachinePrecision] * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 4.6e+110], t$95$0, t$95$1]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := \frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\
\mathbf{if}\;d \leq -3.6 \cdot 10^{+106}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq -3 \cdot 10^{-138}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq 2.3 \cdot 10^{-181}:\\
\;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\

\mathbf{elif}\;d \leq 4.6 \cdot 10^{+110}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error?

Target

Original25.6
Target0.5
Herbie10.3
\[\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 3 regimes
  2. if d < -3.6000000000000001e106 or 4.6e110 < d

    1. Initial program 40.4

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

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

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

      [Start]16.0

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

      *-commutative [<=]16.0

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

      unpow2 [=>]16.0

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

      times-frac [=>]9.2

      \[ \frac{b}{d} + \color{blue}{\frac{a}{d} \cdot \frac{c}{d}} \]
    4. Applied egg-rr10.7

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

    if -3.6000000000000001e106 < d < -3.0000000000000001e-138 or 2.29999999999999991e-181 < d < 4.6e110

    1. Initial program 15.7

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

      \[\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. Applied egg-rr10.7

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

    if -3.0000000000000001e-138 < d < 2.29999999999999991e-181

    1. Initial program 22.7

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

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

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

      [Start]9.3

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

      *-commutative [<=]9.3

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

      unpow2 [=>]9.3

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

      times-frac [=>]8.9

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.6 \cdot 10^{+106}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \mathbf{elif}\;d \leq -3 \cdot 10^{-138}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq 2.3 \cdot 10^{-181}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;d \leq 4.6 \cdot 10^{+110}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \end{array} \]

Alternatives

Alternative 1
Error12.4
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \mathbf{if}\;d \leq -3.8 \cdot 10^{+79}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1.45 \cdot 10^{-137}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.8 \cdot 10^{-90}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;d \leq 4.5 \cdot 10^{+110}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error16.6
Cost1234
\[\begin{array}{l} \mathbf{if}\;d \leq -1.32 \cdot 10^{+23} \lor \neg \left(d \leq -9 \cdot 10^{-29} \lor \neg \left(d \leq -2 \cdot 10^{-94}\right) \land d \leq 9.5 \cdot 10^{+35}\right):\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 3
Error19.5
Cost1233
\[\begin{array}{l} \mathbf{if}\;d \leq -4.5 \cdot 10^{+61}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -8 \cdot 10^{+46} \lor \neg \left(d \leq -1.58 \cdot 10^{+22}\right) \land d \leq 4.5 \cdot 10^{+48}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 4
Error17.4
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ t_1 := \frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{if}\;d \leq -1.2 \cdot 10^{+21}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -9 \cdot 10^{-29}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -2 \cdot 10^{-94}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 5.9 \cdot 10^{+35}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + c \cdot \frac{a}{d \cdot d}\\ \end{array} \]
Alternative 5
Error17.0
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ t_1 := \frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \mathbf{if}\;d \leq -2.1 \cdot 10^{+21}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -7.6 \cdot 10^{-29}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -2 \cdot 10^{-94}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq 1.3 \cdot 10^{+35}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error17.1
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \mathbf{if}\;d \leq -4.8 \cdot 10^{+21}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -8.8 \cdot 10^{-69}:\\ \;\;\;\;\frac{a}{\frac{c \cdot c + d \cdot d}{c}}\\ \mathbf{elif}\;d \leq -2 \cdot 10^{-94}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq 3.9 \cdot 10^{+34}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error23.4
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -2.9 \cdot 10^{+22}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{+49}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 8
Error38.3
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

herbie shell --seed 2023187 
(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))))