Average Error: 26.1 → 6.0
Time: 5.4s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \mathbf{if}\;c \leq -9.581051578847133 \cdot 10^{+165}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{c} \cdot \left(d \cdot \frac{d}{c}\right), 0.5, {\left(\frac{d}{c}\right)}^{3} \cdot \left(0.5 \cdot b\right) - t_0\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 2.2976467137315726 \cdot 10^{+100}:\\ \;\;\;\;\frac{\mathsf{fma}\left(c \cdot a, \frac{1}{\mathsf{hypot}\left(c, d\right)}, {\left(\sqrt[3]{\frac{d}{\frac{\mathsf{hypot}\left(d, c\right)}{b}}}\right)}^{3}\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{\mathsf{hypot}\left(c, d\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 (/ d c) b a)))
   (if (<= c -9.581051578847133e+165)
     (/
      (fma
       (* (/ a c) (* d (/ d c)))
       0.5
       (- (* (pow (/ d c) 3.0) (* 0.5 b)) t_0))
      (hypot c d))
     (if (<= c 2.2976467137315726e+100)
       (/
        (fma
         (* c a)
         (/ 1.0 (hypot c d))
         (pow (cbrt (/ d (/ (hypot d c) b))) 3.0))
        (hypot c d))
       (/ t_0 (hypot c d))))))
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((d / c), b, a);
	double tmp;
	if (c <= -9.581051578847133e+165) {
		tmp = fma(((a / c) * (d * (d / c))), 0.5, ((pow((d / c), 3.0) * (0.5 * b)) - t_0)) / hypot(c, d);
	} else if (c <= 2.2976467137315726e+100) {
		tmp = fma((c * a), (1.0 / hypot(c, d)), pow(cbrt((d / (hypot(d, c) / b))), 3.0)) / hypot(c, d);
	} else {
		tmp = t_0 / hypot(c, d);
	}
	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 = fma(Float64(d / c), b, a)
	tmp = 0.0
	if (c <= -9.581051578847133e+165)
		tmp = Float64(fma(Float64(Float64(a / c) * Float64(d * Float64(d / c))), 0.5, Float64(Float64((Float64(d / c) ^ 3.0) * Float64(0.5 * b)) - t_0)) / hypot(c, d));
	elseif (c <= 2.2976467137315726e+100)
		tmp = Float64(fma(Float64(c * a), Float64(1.0 / hypot(c, d)), (cbrt(Float64(d / Float64(hypot(d, c) / b))) ^ 3.0)) / hypot(c, d));
	else
		tmp = Float64(t_0 / hypot(c, d));
	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[(d / c), $MachinePrecision] * b + a), $MachinePrecision]}, If[LessEqual[c, -9.581051578847133e+165], N[(N[(N[(N[(a / c), $MachinePrecision] * N[(d * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 0.5 + N[(N[(N[Power[N[(d / c), $MachinePrecision], 3.0], $MachinePrecision] * N[(0.5 * b), $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.2976467137315726e+100], N[(N[(N[(c * a), $MachinePrecision] * N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] + N[Power[N[Power[N[(d / N[(N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], N[(t$95$0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\
\mathbf{if}\;c \leq -9.581051578847133 \cdot 10^{+165}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{c} \cdot \left(d \cdot \frac{d}{c}\right), 0.5, {\left(\frac{d}{c}\right)}^{3} \cdot \left(0.5 \cdot b\right) - t_0\right)}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \leq 2.2976467137315726 \cdot 10^{+100}:\\
\;\;\;\;\frac{\mathsf{fma}\left(c \cdot a, \frac{1}{\mathsf{hypot}\left(c, d\right)}, {\left(\sqrt[3]{\frac{d}{\frac{\mathsf{hypot}\left(d, c\right)}{b}}}\right)}^{3}\right)}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{\mathsf{hypot}\left(c, d\right)}\\


\end{array}

Error

Target

Original26.1
Target0.5
Herbie6.0
\[\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 c < -9.58105157884713253e165

    1. Initial program 45.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified45.1

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Applied egg-rr31.5

      \[\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)}} \]
    4. Applied egg-rr31.5

      \[\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)}} \]
    5. Taylor expanded in c around -inf 25.0

      \[\leadsto \frac{\color{blue}{0.5 \cdot \frac{a \cdot {d}^{2}}{{c}^{2}} + \left(-1 \cdot \frac{d \cdot b}{c} + \left(0.5 \cdot \frac{{d}^{3} \cdot b}{{c}^{3}} + -1 \cdot a\right)\right)}}{\mathsf{hypot}\left(c, d\right)} \]
    6. Simplified6.5

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

    if -9.58105157884713253e165 < c < 2.2976467137315726e100

    1. Initial program 19.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified19.5

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Applied egg-rr12.5

      \[\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)}} \]
    4. Applied egg-rr12.4

      \[\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)}} \]
    5. Applied egg-rr12.5

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(a \cdot c, \frac{1}{\mathsf{hypot}\left(c, d\right)}, \left(b \cdot d\right) \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}\right)}}{\mathsf{hypot}\left(c, d\right)} \]
    6. Applied egg-rr4.8

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

    if 2.2976467137315726e100 < c

    1. Initial program 38.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified38.5

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Applied egg-rr25.7

      \[\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)}} \]
    4. Applied egg-rr25.6

      \[\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)}} \]
    5. Taylor expanded in c around inf 12.7

      \[\leadsto \frac{\color{blue}{a + \frac{d \cdot b}{c}}}{\mathsf{hypot}\left(c, d\right)} \]
    6. Simplified10.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -9.581051578847133 \cdot 10^{+165}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{c} \cdot \left(d \cdot \frac{d}{c}\right), 0.5, {\left(\frac{d}{c}\right)}^{3} \cdot \left(0.5 \cdot b\right) - \mathsf{fma}\left(\frac{d}{c}, b, a\right)\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 2.2976467137315726 \cdot 10^{+100}:\\ \;\;\;\;\frac{\mathsf{fma}\left(c \cdot a, \frac{1}{\mathsf{hypot}\left(c, d\right)}, {\left(\sqrt[3]{\frac{d}{\frac{\mathsf{hypot}\left(d, c\right)}{b}}}\right)}^{3}\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]

Reproduce

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