Average Error: 26.7 → 10.4
Time: 5.0s
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)}\\ \mathbf{if}\;d \leq -3.5 \cdot 10^{+79}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-85}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.9 \cdot 10^{-131}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{+106}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \mathsf{fma}\left(\frac{c}{d}, a, b\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)))))
   (if (<= d -3.5e+79)
     (fma (/ c d) (/ a d) (/ b d))
     (if (<= d -3.5e-85)
       t_0
       (if (<= d 3.9e-131)
         (+ (/ a c) (/ (* d b) (pow c 2.0)))
         (if (<= d 3.4e+106)
           t_0
           (* (/ 1.0 (hypot c d)) (fma (/ c d) a b))))))))
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 (d <= -3.5e+79) {
		tmp = fma((c / d), (a / d), (b / d));
	} else if (d <= -3.5e-85) {
		tmp = t_0;
	} else if (d <= 3.9e-131) {
		tmp = (a / c) + ((d * b) / pow(c, 2.0));
	} else if (d <= 3.4e+106) {
		tmp = t_0;
	} else {
		tmp = (1.0 / hypot(c, d)) * fma((c / d), a, b);
	}
	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(Float64(-fma(a, c, Float64(d * b))) / hypot(c, d)) / Float64(-hypot(c, d)))
	tmp = 0.0
	if (d <= -3.5e+79)
		tmp = fma(Float64(c / d), Float64(a / d), Float64(b / d));
	elseif (d <= -3.5e-85)
		tmp = t_0;
	elseif (d <= 3.9e-131)
		tmp = Float64(Float64(a / c) + Float64(Float64(d * b) / (c ^ 2.0)));
	elseif (d <= 3.4e+106)
		tmp = t_0;
	else
		tmp = Float64(Float64(1.0 / hypot(c, d)) * fma(Float64(c / d), a, b));
	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[d, -3.5e+79], N[(N[(c / d), $MachinePrecision] * N[(a / d), $MachinePrecision] + N[(b / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -3.5e-85], t$95$0, If[LessEqual[d, 3.9e-131], N[(N[(a / c), $MachinePrecision] + N[(N[(d * b), $MachinePrecision] / N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 3.4e+106], t$95$0, N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(c / d), $MachinePrecision] * a + b), $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}\;d \leq -3.5 \cdot 10^{+79}:\\
\;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)\\

\mathbf{elif}\;d \leq -3.5 \cdot 10^{-85}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq 3.9 \cdot 10^{-131}:\\
\;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\

\mathbf{elif}\;d \leq 3.4 \cdot 10^{+106}:\\
\;\;\;\;t_0\\

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


\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.7
Target0.4
Herbie10.4
\[\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 d < -3.4999999999999998e79

    1. Initial program 38.8

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr26.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)}} \]
    3. Taylor expanded in c around 0 16.5

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

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

    if -3.4999999999999998e79 < d < -3.49999999999999978e-85 or 3.9000000000000002e-131 < d < 3.39999999999999994e106

    1. Initial program 15.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr10.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-rr10.1

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

    if -3.49999999999999978e-85 < d < 3.9000000000000002e-131

    1. Initial program 23.7

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

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

    if 3.39999999999999994e106 < d

    1. Initial program 41.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr28.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)}} \]
    3. Taylor expanded in c around 0 13.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.5 \cdot 10^{+79}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-85}:\\ \;\;\;\;\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 3.9 \cdot 10^{-131}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{+106}:\\ \;\;\;\;\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{c}{d}, a, b\right)\\ \end{array} \]

Reproduce

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