\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 -2.1840157650846506 \cdot 10^{+101}:\\
\;\;\;\;\frac{-t_0}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{elif}\;c \leq -3.6283479029046922 \cdot 10^{-140}:\\
\;\;\;\;\begin{array}{l}
t_1 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
\mathsf{fma}\left(b, \frac{d}{t_1}, \frac{c \cdot a}{t_1}\right)
\end{array}\\
\mathbf{elif}\;c \leq 8.272481472784187 \cdot 10^{-130}:\\
\;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\
\mathbf{elif}\;c \leq 1.7908903707439708 \cdot 10^{+98}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{t_0}{\mathsf{hypot}\left(d, c\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 -2.1840157650846506e+101)
(/ (- t_0) (hypot d c))
(if (<= c -3.6283479029046922e-140)
(let* ((t_1 (fma d d (* c c)))) (fma b (/ d t_1) (/ (* c a) t_1)))
(if (<= c 8.272481472784187e-130)
(fma (/ a d) (/ c d) (/ b d))
(if (<= c 1.7908903707439708e+98)
(/ (/ (fma a c (* d b)) (hypot d c)) (hypot d c))
(/ t_0 (hypot d c))))))))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 <= -2.1840157650846506e+101) {
tmp = -t_0 / hypot(d, c);
} else if (c <= -3.6283479029046922e-140) {
double t_1 = fma(d, d, (c * c));
tmp = fma(b, (d / t_1), ((c * a) / t_1));
} else if (c <= 8.272481472784187e-130) {
tmp = fma((a / d), (c / d), (b / d));
} else if (c <= 1.7908903707439708e+98) {
tmp = (fma(a, c, (d * b)) / hypot(d, c)) / hypot(d, c);
} else {
tmp = t_0 / hypot(d, c);
}
return tmp;
}




Bits error versus a




Bits error versus b




Bits error versus c




Bits error versus d
| Original | 24.6 |
|---|---|
| Target | 0.4 |
| Herbie | 9.8 |
if c < -2.18401576508465057e101Initial program 39.4
Simplified39.4
Applied add-sqr-sqrt_binary6439.4
Applied *-un-lft-identity_binary6439.4
Applied times-frac_binary6439.4
Simplified39.4
Simplified26.2
Applied associate-*l/_binary6426.1
Simplified26.1
Taylor expanded in c around -inf 12.9
Simplified9.4
if -2.18401576508465057e101 < c < -3.6283479029046922e-140Initial program 15.2
Simplified15.1
Taylor expanded in a around 0 15.2
Simplified13.4
if -3.6283479029046922e-140 < c < 8.272481472784187e-130Initial program 19.7
Simplified19.7
Applied add-sqr-sqrt_binary6419.7
Applied *-un-lft-identity_binary6419.7
Applied times-frac_binary6419.7
Simplified19.7
Simplified10.2
Taylor expanded in d around inf 11.3
Simplified7.7
if 8.272481472784187e-130 < c < 1.79089037074397082e98Initial program 14.4
Simplified14.4
Applied add-sqr-sqrt_binary6414.4
Applied *-un-lft-identity_binary6414.4
Applied times-frac_binary6414.5
Simplified14.5
Simplified9.6
Applied associate-*l/_binary649.4
Simplified9.4
if 1.79089037074397082e98 < c Initial program 40.4
Simplified40.4
Applied add-sqr-sqrt_binary6440.4
Applied *-un-lft-identity_binary6440.4
Applied times-frac_binary6440.4
Simplified40.4
Simplified25.3
Applied associate-*l/_binary6425.3
Simplified25.2
Taylor expanded in c around inf 13.4
Simplified9.8
Final simplification9.8
herbie shell --seed 2022068
(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))))