\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \mathsf{fma}\left(\frac{c}{d}, a, b\right)\\
\mathbf{if}\;d \leq -1.2126922600481525 \cdot 10^{+99}:\\
\;\;\;\;\frac{-t_0}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{elif}\;d \leq -4.171294695866961 \cdot 10^{-113}:\\
\;\;\;\;\begin{array}{l}
t_1 := \mathsf{fma}\left(c, c, d \cdot d\right)\\
\mathsf{fma}\left(\frac{c}{t_1}, a, \frac{d \cdot b}{t_1}\right)
\end{array}\\
\mathbf{elif}\;d \leq -1.657834802134664 \cdot 10^{-306}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\
\mathbf{elif}\;d \leq 4.637490032372509 \cdot 10^{+77}:\\
\;\;\;\;\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 (/ c d) a b)))
(if (<= d -1.2126922600481525e+99)
(/ (- t_0) (hypot d c))
(if (<= d -4.171294695866961e-113)
(let* ((t_1 (fma c c (* d d)))) (fma (/ c t_1) a (/ (* d b) t_1)))
(if (<= d -1.657834802134664e-306)
(fma (/ d c) (/ b c) (/ a c))
(if (<= d 4.637490032372509e+77)
(/ (/ (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((c / d), a, b);
double tmp;
if (d <= -1.2126922600481525e+99) {
tmp = -t_0 / hypot(d, c);
} else if (d <= -4.171294695866961e-113) {
double t_1 = fma(c, c, (d * d));
tmp = fma((c / t_1), a, ((d * b) / t_1));
} else if (d <= -1.657834802134664e-306) {
tmp = fma((d / c), (b / c), (a / c));
} else if (d <= 4.637490032372509e+77) {
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 | 25.0 |
|---|---|
| Target | 0.3 |
| Herbie | 10.0 |
if d < -1.21269226004815246e99Initial program 38.7
Applied add-sqr-sqrt_binary6438.7
Applied *-un-lft-identity_binary6438.7
Applied times-frac_binary6438.7
Simplified38.7
Simplified25.0
Applied associate-*l/_binary6424.9
Simplified24.9
Taylor expanded in d around -inf 12.4
Simplified8.6
if -1.21269226004815246e99 < d < -4.17129469586696093e-113Initial program 15.1
Applied add-sqr-sqrt_binary6415.1
Applied *-un-lft-identity_binary6415.1
Applied times-frac_binary6415.1
Simplified15.1
Simplified10.4
Applied add-sqr-sqrt_binary6410.6
Taylor expanded in b around 0 15.2
Simplified12.7
if -4.17129469586696093e-113 < d < -1.6578348021346639e-306Initial program 20.7
Taylor expanded in c around inf 12.3
Simplified9.8
if -1.6578348021346639e-306 < d < 4.6374900323725094e77Initial program 16.2
Applied add-sqr-sqrt_binary6416.2
Applied *-un-lft-identity_binary6416.2
Applied times-frac_binary6416.2
Simplified16.2
Simplified9.6
Applied associate-*l/_binary649.5
Simplified9.5
if 4.6374900323725094e77 < d Initial program 39.5
Applied add-sqr-sqrt_binary6439.5
Applied *-un-lft-identity_binary6439.5
Applied times-frac_binary6439.5
Simplified39.5
Simplified26.4
Applied associate-*l/_binary6426.3
Simplified26.3
Taylor expanded in c around 0 13.2
Simplified9.9
Final simplification10.0
herbie shell --seed 2022088
(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))))