Average Error: 26.0 → 10.3
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 := \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \mathbf{if}\;c \leq -1.3 \cdot 10^{+132}:\\ \;\;\;\;\frac{-t_1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -3.3 \cdot 10^{-83}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{-106}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq 1.08 \cdot 10^{+55}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1}{\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 a c (* d b)) (hypot c d)) (hypot c d)))
        (t_1 (fma (/ d c) b a)))
   (if (<= c -1.3e+132)
     (/ (- t_1) (hypot c d))
     (if (<= c -3.3e-83)
       t_0
       (if (<= c 1.9e-106)
         (fma (/ a d) (/ c d) (/ b d))
         (if (<= c 1.08e+55) t_0 (/ t_1 (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(a, c, (d * b)) / hypot(c, d)) / hypot(c, d);
	double t_1 = fma((d / c), b, a);
	double tmp;
	if (c <= -1.3e+132) {
		tmp = -t_1 / hypot(c, d);
	} else if (c <= -3.3e-83) {
		tmp = t_0;
	} else if (c <= 1.9e-106) {
		tmp = fma((a / d), (c / d), (b / d));
	} else if (c <= 1.08e+55) {
		tmp = t_0;
	} else {
		tmp = t_1 / 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 = Float64(Float64(fma(a, c, Float64(d * b)) / hypot(c, d)) / hypot(c, d))
	t_1 = fma(Float64(d / c), b, a)
	tmp = 0.0
	if (c <= -1.3e+132)
		tmp = Float64(Float64(-t_1) / hypot(c, d));
	elseif (c <= -3.3e-83)
		tmp = t_0;
	elseif (c <= 1.9e-106)
		tmp = fma(Float64(a / d), Float64(c / d), Float64(b / d));
	elseif (c <= 1.08e+55)
		tmp = t_0;
	else
		tmp = Float64(t_1 / 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[(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[(d / c), $MachinePrecision] * b + a), $MachinePrecision]}, If[LessEqual[c, -1.3e+132], N[((-t$95$1) / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -3.3e-83], t$95$0, If[LessEqual[c, 1.9e-106], N[(N[(a / d), $MachinePrecision] * N[(c / d), $MachinePrecision] + N[(b / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1.08e+55], t$95$0, N[(t$95$1 / 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 := \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 := \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\
\mathbf{if}\;c \leq -1.3 \cdot 10^{+132}:\\
\;\;\;\;\frac{-t_1}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \leq -3.3 \cdot 10^{-83}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;c \leq 1.08 \cdot 10^{+55}:\\
\;\;\;\;t_0\\

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


\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.5
Herbie10.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 c < -1.3e132

    1. Initial program 42.2

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

      \[\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. Applied egg-rr28.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 13.5

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

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

    if -1.3e132 < c < -3.2999999999999999e-83 or 1.9e-106 < c < 1.08000000000000004e55

    1. Initial program 15.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified15.9

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

      \[\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-rr10.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)}} \]

    if -3.2999999999999999e-83 < c < 1.9e-106

    1. Initial program 22.2

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

      \[\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.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 0 11.0

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

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

    if 1.08000000000000004e55 < c

    1. Initial program 35.4

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

      \[\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-rr24.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -1.3 \cdot 10^{+132}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -3.3 \cdot 10^{-83}:\\ \;\;\;\;\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.9 \cdot 10^{-106}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq 1.08 \cdot 10^{+55}:\\ \;\;\;\;\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{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]

Reproduce

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