Average Error: 26.3 → 10.9
Time: 13.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)}\\ \mathbf{if}\;c \leq -1.9 \cdot 10^{+100}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq -1.25 \cdot 10^{-222}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 7.8 \cdot 10^{-144}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 3.05 \cdot 10^{+57}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{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
 (let* ((t_0 (/ (/ (fma a c (* d b)) (hypot c d)) (hypot c d))))
   (if (<= c -1.9e+100)
     (+ (/ a c) (/ d (/ (* c c) b)))
     (if (<= c -1.25e-222)
       t_0
       (if (<= c 7.8e-144)
         (+ (/ b d) (/ (* c (/ a d)) d))
         (if (<= c 3.05e+57) t_0 (+ (/ a c) (* (/ b c) (/ 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 t_0 = (fma(a, c, (d * b)) / hypot(c, d)) / hypot(c, d);
	double tmp;
	if (c <= -1.9e+100) {
		tmp = (a / c) + (d / ((c * c) / b));
	} else if (c <= -1.25e-222) {
		tmp = t_0;
	} else if (c <= 7.8e-144) {
		tmp = (b / d) + ((c * (a / d)) / d);
	} else if (c <= 3.05e+57) {
		tmp = t_0;
	} else {
		tmp = (a / c) + ((b / c) * (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)
	t_0 = Float64(Float64(fma(a, c, Float64(d * b)) / hypot(c, d)) / hypot(c, d))
	tmp = 0.0
	if (c <= -1.9e+100)
		tmp = Float64(Float64(a / c) + Float64(d / Float64(Float64(c * c) / b)));
	elseif (c <= -1.25e-222)
		tmp = t_0;
	elseif (c <= 7.8e-144)
		tmp = Float64(Float64(b / d) + Float64(Float64(c * Float64(a / d)) / d));
	elseif (c <= 3.05e+57)
		tmp = t_0;
	else
		tmp = Float64(Float64(a / c) + Float64(Float64(b / c) * Float64(d / c)));
	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]}, If[LessEqual[c, -1.9e+100], N[(N[(a / c), $MachinePrecision] + N[(d / N[(N[(c * c), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -1.25e-222], t$95$0, If[LessEqual[c, 7.8e-144], N[(N[(b / d), $MachinePrecision] + N[(N[(c * N[(a / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 3.05e+57], t$95$0, N[(N[(a / c), $MachinePrecision] + N[(N[(b / c), $MachinePrecision] * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\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)}\\
\mathbf{if}\;c \leq -1.9 \cdot 10^{+100}:\\
\;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\

\mathbf{elif}\;c \leq -1.25 \cdot 10^{-222}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;c \leq 3.05 \cdot 10^{+57}:\\
\;\;\;\;t_0\\

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


\end{array}

Error

Target

Original26.3
Target0.5
Herbie10.9
\[\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 4 regimes
  2. if c < -1.89999999999999982e100

    1. Initial program 38.8

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

      \[\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 c around inf 15.5

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

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

      [Start]15.5

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

      associate-/l* [=>]14.0

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

      unpow2 [=>]14.0

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

    if -1.89999999999999982e100 < c < -1.25000000000000002e-222 or 7.8000000000000003e-144 < c < 3.04999999999999988e57

    1. Initial program 16.7

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

      \[\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-rr11.0

      \[\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 -1.25000000000000002e-222 < c < 7.8000000000000003e-144

    1. Initial program 23.1

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

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

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

      [Start]8.2

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

      *-commutative [<=]8.2

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

      unpow2 [=>]8.2

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

      times-frac [=>]6.1

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

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

    if 3.04999999999999988e57 < c

    1. Initial program 36.6

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

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

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

      [Start]18.0

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

      *-commutative [<=]18.0

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

      unpow2 [=>]18.0

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

      times-frac [=>]12.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -1.9 \cdot 10^{+100}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq -1.25 \cdot 10^{-222}:\\ \;\;\;\;\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}\;c \leq 7.8 \cdot 10^{-144}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 3.05 \cdot 10^{+57}:\\ \;\;\;\;\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{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]

Alternatives

Alternative 1
Error13.4
Cost14032
\[\begin{array}{l} \mathbf{if}\;c \leq -3.1 \cdot 10^{+100}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq -1.4 \cdot 10^{-221}:\\ \;\;\;\;\frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.35 \cdot 10^{-128}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 6.3 \cdot 10^{+56}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 2
Error19.7
Cost1628
\[\begin{array}{l} t_0 := a \cdot \frac{c}{c \cdot c + d \cdot d}\\ t_1 := \frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{if}\;c \leq -4.4 \cdot 10^{+68}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1.75 \cdot 10^{-173}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{-181}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 10^{-156}:\\ \;\;\;\;\frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{-102}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 500000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1900000000000:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error20.6
Cost1628
\[\begin{array}{l} t_0 := a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -5.7 \cdot 10^{+57}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq -1.75 \cdot 10^{-173}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{-181}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 7.5 \cdot 10^{-156}:\\ \;\;\;\;\frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 1.48 \cdot 10^{-102}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1300:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.65 \cdot 10^{+14}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 4
Error22.3
Cost1496
\[\begin{array}{l} t_0 := a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -8.8 \cdot 10^{+128}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -1.75 \cdot 10^{-173}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{-181}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 7.5 \cdot 10^{-156}:\\ \;\;\;\;\frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 7.8 \cdot 10^{-103}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.45 \cdot 10^{+107}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 5
Error13.4
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -4 \cdot 10^{+99}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq -1.4 \cdot 10^{-221}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.2 \cdot 10^{-129}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 3.05 \cdot 10^{+57}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 6
Error18.2
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{if}\;c \leq -4.9 \cdot 10^{+42}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq -8.6 \cdot 10^{-13}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -1.8 \cdot 10^{-173}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.95 \cdot 10^{+16}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 7
Error18.1
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1.95 \cdot 10^{+43}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq -2.2 \cdot 10^{-11}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq -1.8 \cdot 10^{-173}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.62 \cdot 10^{+15}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 8
Error16.2
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1.92 \cdot 10^{+43}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq -3.2 \cdot 10^{-11}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq -1.15 \cdot 10^{-67}:\\ \;\;\;\;\frac{c}{\frac{c \cdot c + d \cdot d}{a}}\\ \mathbf{elif}\;c \leq 1.8 \cdot 10^{+14}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 9
Error23.2
Cost1108
\[\begin{array}{l} \mathbf{if}\;c \leq -4.8 \cdot 10^{+41}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -6 \cdot 10^{-13}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -1.8 \cdot 10^{-48}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{-181}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 10^{-156}:\\ \;\;\;\;\frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 4.4 \cdot 10^{+17}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 10
Error22.9
Cost722
\[\begin{array}{l} \mathbf{if}\;c \leq -1.4 \cdot 10^{+42} \lor \neg \left(c \leq -1.9 \cdot 10^{-11}\right) \land \left(c \leq -5.5 \cdot 10^{-48} \lor \neg \left(c \leq 2.7 \cdot 10^{+17}\right)\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 11
Error36.5
Cost324
\[\begin{array}{l} \mathbf{if}\;d \leq -6.4 \cdot 10^{+178}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 12
Error37.0
Cost192
\[\frac{a}{c} \]

Error

Reproduce

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