\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -2.558733259505715 \cdot 10^{+128}:\\
\;\;\;\;\mathsf{fma}\left(\frac{c}{d}, a, b\right) \cdot \frac{-1}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{elif}\;d \leq -1.0098211784491114 \cdot 10^{-135}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{elif}\;d \leq 1.544901258368836 \cdot 10^{-100}:\\
\;\;\;\;\mathsf{fma}\left(\frac{b}{c}, \frac{d}{c}, \frac{a}{c}\right)\\
\mathbf{elif}\;d \leq 5.388057696506973 \cdot 10^{+98}:\\
\;\;\;\;\frac{\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{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
(if (<= d -2.558733259505715e+128)
(* (fma (/ c d) a b) (/ -1.0 (hypot d c)))
(if (<= d -1.0098211784491114e-135)
(/ (/ (fma a c (* d b)) (hypot d c)) (hypot d c))
(if (<= d 1.544901258368836e-100)
(fma (/ b c) (/ d c) (/ a c))
(if (<= d 5.388057696506973e+98)
(/ (* (/ 1.0 (hypot d c)) (fma d b (* c a))) (hypot d c))
(fma (/ a d) (/ c d) (/ b 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 tmp;
if (d <= -2.558733259505715e+128) {
tmp = fma((c / d), a, b) * (-1.0 / hypot(d, c));
} else if (d <= -1.0098211784491114e-135) {
tmp = (fma(a, c, (d * b)) / hypot(d, c)) / hypot(d, c);
} else if (d <= 1.544901258368836e-100) {
tmp = fma((b / c), (d / c), (a / c));
} else if (d <= 5.388057696506973e+98) {
tmp = ((1.0 / hypot(d, c)) * fma(d, b, (c * a))) / hypot(d, c);
} else {
tmp = fma((a / d), (c / d), (b / d));
}
return tmp;
}




Bits error versus a




Bits error versus b




Bits error versus c




Bits error versus d
| Original | 26.4 |
|---|---|
| Target | 0.5 |
| Herbie | 10.2 |
if d < -2.558733259505715e128Initial program 42.0
Simplified42.0
Applied add-sqr-sqrt_binary6441.9
Applied *-un-lft-identity_binary6441.9
Applied times-frac_binary6442.0
Simplified42.0
Simplified27.1
Taylor expanded in d around -inf 10.9
Simplified7.3
if -2.558733259505715e128 < d < -1.0098211784491114e-135Initial program 16.4
Simplified16.4
Applied add-sqr-sqrt_binary6416.4
Applied *-un-lft-identity_binary6416.4
Applied times-frac_binary6416.4
Simplified16.4
Simplified12.1
Applied associate-*l/_binary6412.0
Simplified12.0
if -1.0098211784491114e-135 < d < 1.5449012583688359e-100Initial program 22.3
Simplified22.3
Applied add-sqr-sqrt_binary6422.3
Applied *-un-lft-identity_binary6422.3
Applied times-frac_binary6422.3
Simplified22.3
Simplified12.4
Taylor expanded in d around 0 10.9
Simplified9.4
if 1.5449012583688359e-100 < d < 5.3880576965069729e98Initial program 16.3
Simplified16.3
Applied add-sqr-sqrt_binary6416.3
Applied *-un-lft-identity_binary6416.3
Applied times-frac_binary6416.3
Simplified16.3
Simplified11.2
Applied associate-*r/_binary6411.2
if 5.3880576965069729e98 < d Initial program 40.9
Simplified40.9
Applied add-sqr-sqrt_binary6440.9
Applied *-un-lft-identity_binary6440.9
Applied times-frac_binary6440.9
Simplified40.9
Simplified27.8
Taylor expanded in d around inf 16.9
Simplified10.7
Final simplification10.2
herbie shell --seed 2022081
(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))))