Average Error: 25.8 → 10.4
Time: 8.3s
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}\;c \leq -3.951719848587527 \cdot 10^{+140}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;c \leq -2.1020937544048643 \cdot 10^{-195}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 9.158472105652272 \cdot 10^{-177}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 7.712883155351153 \cdot 10^{+76}:\\ \;\;\;\;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)))))
   (if (<= c -3.951719848587527e+140)
     (fma (/ d c) (/ b c) (/ a c))
     (if (<= c -2.1020937544048643e-195)
       t_0
       (if (<= c 9.158472105652272e-177)
         (+ (/ b d) (/ (* c a) (pow d 2.0)))
         (if (<= c 7.712883155351153e+76)
           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 tmp;
	if (c <= -3.951719848587527e+140) {
		tmp = fma((d / c), (b / c), (a / c));
	} else if (c <= -2.1020937544048643e-195) {
		tmp = t_0;
	} else if (c <= 9.158472105652272e-177) {
		tmp = (b / d) + ((c * a) / pow(d, 2.0));
	} else if (c <= 7.712883155351153e+76) {
		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(Float64(-fma(a, c, Float64(d * b))) / hypot(c, d)) / Float64(-hypot(c, d)))
	tmp = 0.0
	if (c <= -3.951719848587527e+140)
		tmp = fma(Float64(d / c), Float64(b / c), Float64(a / c));
	elseif (c <= -2.1020937544048643e-195)
		tmp = t_0;
	elseif (c <= 9.158472105652272e-177)
		tmp = Float64(Float64(b / d) + Float64(Float64(c * a) / (d ^ 2.0)));
	elseif (c <= 7.712883155351153e+76)
		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]}, If[LessEqual[c, -3.951719848587527e+140], N[(N[(d / c), $MachinePrecision] * N[(b / c), $MachinePrecision] + N[(a / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -2.1020937544048643e-195], t$95$0, If[LessEqual[c, 9.158472105652272e-177], N[(N[(b / d), $MachinePrecision] + N[(N[(c * a), $MachinePrecision] / N[Power[d, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 7.712883155351153e+76], 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)}\\
\mathbf{if}\;c \leq -3.951719848587527 \cdot 10^{+140}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\

\mathbf{elif}\;c \leq -2.1020937544048643 \cdot 10^{-195}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;c \leq 7.712883155351153 \cdot 10^{+76}:\\
\;\;\;\;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

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.8
Target0.5
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 c < -3.9517198485875273e140

    1. Initial program 44.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified44.4

      \[\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-rr28.6

      \[\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. Taylor expanded in c around inf 15.1

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

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

    if -3.9517198485875273e140 < c < -2.1020937544048643e-195 or 9.15847210565227231e-177 < c < 7.71288315535115255e76

    1. Initial program 16.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified16.3

      \[\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-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)}} \]
    4. Applied egg-rr11.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 -2.1020937544048643e-195 < c < 9.15847210565227231e-177

    1. Initial program 23.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified23.3

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Taylor expanded in c around 0 9.7

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

    if 7.71288315535115255e76 < c

    1. Initial program 37.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified37.4

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

      \[\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. Taylor expanded in c around inf 14.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -3.951719848587527 \cdot 10^{+140}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;c \leq -2.1020937544048643 \cdot 10^{-195}:\\ \;\;\;\;\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 9.158472105652272 \cdot 10^{-177}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 7.712883155351153 \cdot 10^{+76}:\\ \;\;\;\;\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 2022153 
(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))))