Average Error: 25.6 → 10.9
Time: 4.4s
Precision: binary64
\[\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 d} \cdot \left(a - \frac{c \cdot b}{d}\right)\\ \mathbf{if}\;c \leq -2.416359215897456 \cdot 10^{+150}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;c \leq -4.7154385585253094 \cdot 10^{-18}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -1.086887131696847 \cdot 10^{-42}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-225}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-206}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 1.2964207563348648 \cdot 10^{+110}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \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 (/ (* c b) d))))))
   (if (<= c -2.416359215897456e+150)
     (fma (/ d c) (/ b c) (/ a c))
     (if (<= c -4.7154385585253094e-18)
       t_0
       (if (<= c -1.086887131696847e-42)
         t_1
         (if (<= c -1e-225)
           t_0
           (if (<= c 1e-206)
             t_1
             (if (<= c 1.2964207563348648e+110)
               t_0
               (* (/ 1.0 (hypot c d)) (fma (/ d c) b a))))))))))
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 - ((c * b) / d)));
	double tmp;
	if (c <= -2.416359215897456e+150) {
		tmp = fma((d / c), (b / c), (a / c));
	} else if (c <= -4.7154385585253094e-18) {
		tmp = t_0;
	} else if (c <= -1.086887131696847e-42) {
		tmp = t_1;
	} else if (c <= -1e-225) {
		tmp = t_0;
	} else if (c <= 1e-206) {
		tmp = t_1;
	} else if (c <= 1.2964207563348648e+110) {
		tmp = t_0;
	} else {
		tmp = (1.0 / hypot(c, d)) * fma((d / c), b, a);
	}
	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(Float64(c / Float64(d * d)) * Float64(a - Float64(Float64(c * b) / d))))
	tmp = 0.0
	if (c <= -2.416359215897456e+150)
		tmp = fma(Float64(d / c), Float64(b / c), Float64(a / c));
	elseif (c <= -4.7154385585253094e-18)
		tmp = t_0;
	elseif (c <= -1.086887131696847e-42)
		tmp = t_1;
	elseif (c <= -1e-225)
		tmp = t_0;
	elseif (c <= 1e-206)
		tmp = t_1;
	elseif (c <= 1.2964207563348648e+110)
		tmp = t_0;
	else
		tmp = Float64(Float64(1.0 / hypot(c, d)) * fma(Float64(d / c), b, a));
	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[(N[(c / N[(d * d), $MachinePrecision]), $MachinePrecision] * N[(a - N[(N[(c * b), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -2.416359215897456e+150], N[(N[(d / c), $MachinePrecision] * N[(b / c), $MachinePrecision] + N[(a / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -4.7154385585253094e-18], t$95$0, If[LessEqual[c, -1.086887131696847e-42], t$95$1, If[LessEqual[c, -1e-225], t$95$0, If[LessEqual[c, 1e-206], t$95$1, If[LessEqual[c, 1.2964207563348648e+110], t$95$0, N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(d / c), $MachinePrecision] * b + a), $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)}\\
t_1 := \frac{b}{d} + \frac{c}{d \cdot d} \cdot \left(a - \frac{c \cdot b}{d}\right)\\
\mathbf{if}\;c \leq -2.416359215897456 \cdot 10^{+150}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\

\mathbf{elif}\;c \leq -4.7154385585253094 \cdot 10^{-18}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;c \leq -1.086887131696847 \cdot 10^{-42}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;c \leq -1 \cdot 10^{-225}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;c \leq 10^{-206}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;c \leq 1.2964207563348648 \cdot 10^{+110}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\


\end{array}

Error

Target

Original25.6
Target0.4
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 < -2.4163592158974562e150

    1. Initial program 43.9

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

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

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

    if -2.4163592158974562e150 < c < -4.7154385585253094e-18 or -1.086887131696847e-42 < c < -9.9999999999999996e-226 or 1.00000000000000003e-206 < c < 1.2964207563348648e110

    1. Initial program 17.7

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

      \[\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.8

      \[\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 -4.7154385585253094e-18 < c < -1.086887131696847e-42 or -9.9999999999999996e-226 < c < 1.00000000000000003e-206

    1. Initial program 22.5

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

      \[\leadsto \color{blue}{-1 \cdot \frac{{c}^{2} \cdot b}{{d}^{3}} + \left(\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\right)} \]
    3. Simplified13.1

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c}{d \cdot d} \cdot \left(a - \frac{b \cdot c}{d}\right)} \]

    if 1.2964207563348648e110 < c

    1. Initial program 39.3

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

      \[\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 12.7

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(a + \frac{d \cdot b}{c}\right)} \]
    4. Simplified8.8

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\mathsf{fma}\left(\frac{d}{c}, b, a\right)} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification10.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -2.416359215897456 \cdot 10^{+150}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;c \leq -4.7154385585253094 \cdot 10^{-18}:\\ \;\;\;\;\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 -1.086887131696847 \cdot 10^{-42}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d \cdot d} \cdot \left(a - \frac{c \cdot b}{d}\right)\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-225}:\\ \;\;\;\;\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 10^{-206}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d \cdot d} \cdot \left(a - \frac{c \cdot b}{d}\right)\\ \mathbf{elif}\;c \leq 1.2964207563348648 \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{1}{\mathsf{hypot}\left(c, d\right)} \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \end{array} \]

Reproduce

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