Average Error: 26.5 → 11.3
Time: 6.1s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{if}\;d \leq -1.1870115366769325 \cdot 10^{+154}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -3.283521020638581 \cdot 10^{-106}:\\ \;\;\;\;\mathsf{fma}\left(b, \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}, c \cdot \frac{a}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}\right)\\ \mathbf{elif}\;d \leq 2.670610561569208 \cdot 10^{-71}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;d \leq 1.2722428837589733 \cdot 10^{+140}:\\ \;\;\;\;\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}:\\ \;\;\;\;t_0\\ \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 d) (/ c d) (/ b d))))
   (if (<= d -1.1870115366769325e+154)
     t_0
     (if (<= d -3.283521020638581e-106)
       (fma b (/ d (fma d d (* c c))) (* c (/ a (pow (hypot d c) 2.0))))
       (if (<= d 2.670610561569208e-71)
         (fma (/ d c) (/ b c) (/ a c))
         (if (<= d 1.2722428837589733e+140)
           (/ (/ (fma a c (* d b)) (hypot c d)) (hypot c d))
           t_0))))))
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 / d), (c / d), (b / d));
	double tmp;
	if (d <= -1.1870115366769325e+154) {
		tmp = t_0;
	} else if (d <= -3.283521020638581e-106) {
		tmp = fma(b, (d / fma(d, d, (c * c))), (c * (a / pow(hypot(d, c), 2.0))));
	} else if (d <= 2.670610561569208e-71) {
		tmp = fma((d / c), (b / c), (a / c));
	} else if (d <= 1.2722428837589733e+140) {
		tmp = (fma(a, c, (d * b)) / hypot(c, d)) / hypot(c, d);
	} else {
		tmp = t_0;
	}
	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(a / d), Float64(c / d), Float64(b / d))
	tmp = 0.0
	if (d <= -1.1870115366769325e+154)
		tmp = t_0;
	elseif (d <= -3.283521020638581e-106)
		tmp = fma(b, Float64(d / fma(d, d, Float64(c * c))), Float64(c * Float64(a / (hypot(d, c) ^ 2.0))));
	elseif (d <= 2.670610561569208e-71)
		tmp = fma(Float64(d / c), Float64(b / c), Float64(a / c));
	elseif (d <= 1.2722428837589733e+140)
		tmp = Float64(Float64(fma(a, c, Float64(d * b)) / hypot(c, d)) / hypot(c, d));
	else
		tmp = t_0;
	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[(a / d), $MachinePrecision] * N[(c / d), $MachinePrecision] + N[(b / d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -1.1870115366769325e+154], t$95$0, If[LessEqual[d, -3.283521020638581e-106], N[(b * N[(d / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c * N[(a / N[Power[N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 2.670610561569208e-71], N[(N[(d / c), $MachinePrecision] * N[(b / c), $MachinePrecision] + N[(a / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.2722428837589733e+140], 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], t$95$0]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\
\mathbf{if}\;d \leq -1.1870115366769325 \cdot 10^{+154}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq -3.283521020638581 \cdot 10^{-106}:\\
\;\;\;\;\mathsf{fma}\left(b, \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}, c \cdot \frac{a}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}\right)\\

\mathbf{elif}\;d \leq 2.670610561569208 \cdot 10^{-71}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\

\mathbf{elif}\;d \leq 1.2722428837589733 \cdot 10^{+140}:\\
\;\;\;\;\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}:\\
\;\;\;\;t_0\\


\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.5
Target0.5
Herbie11.3
\[\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 < -1.1870115366769325e154 or 1.2722428837589733e140 < d

    1. Initial program 44.2

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

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

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

    if -1.1870115366769325e154 < d < -3.283521020638581e-106

    1. Initial program 16.9

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

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

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

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

    if -3.283521020638581e-106 < d < 2.67061056156920781e-71

    1. Initial program 21.7

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

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

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

    if 2.67061056156920781e-71 < d < 1.2722428837589733e140

    1. Initial program 18.8

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

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

      \[\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)}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification11.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.1870115366769325 \cdot 10^{+154}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;d \leq -3.283521020638581 \cdot 10^{-106}:\\ \;\;\;\;\mathsf{fma}\left(b, \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}, c \cdot \frac{a}{{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}\right)\\ \mathbf{elif}\;d \leq 2.670610561569208 \cdot 10^{-71}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;d \leq 1.2722428837589733 \cdot 10^{+140}:\\ \;\;\;\;\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}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \end{array} \]

Reproduce

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