\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\
t_1 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_2 := \mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\
t_3 := \mathsf{fma}\left(\frac{b}{c}, \frac{d}{c}, \frac{a}{c}\right)\\
\mathbf{if}\;d \leq -7.798094340522651 \cdot 10^{+148}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;d \leq -3.4710805651394907 \cdot 10^{+37}:\\
\;\;\;\;\mathsf{fma}\left(b, \frac{d}{t_1}, \frac{a \cdot c}{t_1}\right)\\
\mathbf{elif}\;d \leq -4.34100723376406 \cdot 10^{+29}:\\
\;\;\;\;t_3\\
\mathbf{elif}\;d \leq -3.840391229221447 \cdot 10^{-106}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;d \leq 3.7980760572038457 \cdot 10^{-66}:\\
\;\;\;\;t_3\\
\mathbf{elif}\;d \leq 6.164541131217644 \cdot 10^{+185}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;t_2\\
\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 a (* d b)) (hypot d c)) (hypot d c)))
(t_1 (fma d d (* c c)))
(t_2 (fma (/ a d) (/ c d) (/ b d)))
(t_3 (fma (/ b c) (/ d c) (/ a c))))
(if (<= d -7.798094340522651e+148)
t_2
(if (<= d -3.4710805651394907e+37)
(fma b (/ d t_1) (/ (* a c) t_1))
(if (<= d -4.34100723376406e+29)
t_3
(if (<= d -3.840391229221447e-106)
t_0
(if (<= d 3.7980760572038457e-66)
t_3
(if (<= d 6.164541131217644e+185) t_0 t_2))))))))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, a, (d * b)) / hypot(d, c)) / hypot(d, c);
double t_1 = fma(d, d, (c * c));
double t_2 = fma((a / d), (c / d), (b / d));
double t_3 = fma((b / c), (d / c), (a / c));
double tmp;
if (d <= -7.798094340522651e+148) {
tmp = t_2;
} else if (d <= -3.4710805651394907e+37) {
tmp = fma(b, (d / t_1), ((a * c) / t_1));
} else if (d <= -4.34100723376406e+29) {
tmp = t_3;
} else if (d <= -3.840391229221447e-106) {
tmp = t_0;
} else if (d <= 3.7980760572038457e-66) {
tmp = t_3;
} else if (d <= 6.164541131217644e+185) {
tmp = t_0;
} else {
tmp = t_2;
}
return tmp;
}




Bits error versus a




Bits error versus b




Bits error versus c




Bits error versus d
| Original | 26.4 |
|---|---|
| Target | 0.4 |
| Herbie | 10.8 |
if d < -7.7980943405226511e148 or 6.16454113121764419e185 < d Initial program 44.2
Simplified44.2
Applied add-sqr-sqrt_binary6444.2
Applied *-un-lft-identity_binary6444.2
Applied times-frac_binary6444.2
Simplified44.2
Simplified29.8
Taylor expanded in d around inf 14.9
Simplified6.7
if -7.7980943405226511e148 < d < -3.47108056513949065e37Initial program 19.8
Simplified19.9
Taylor expanded in a around 0 19.9
Simplified13.3
if -3.47108056513949065e37 < d < -4.3410072337640601e29 or -3.8403912292214472e-106 < d < 3.79807605720384573e-66Initial program 21.6
Simplified21.6
Applied add-sqr-sqrt_binary6421.6
Applied *-un-lft-identity_binary6421.6
Applied times-frac_binary6421.6
Simplified21.6
Simplified12.4
Taylor expanded in d around 0 12.3
Simplified11.0
if -4.3410072337640601e29 < d < -3.8403912292214472e-106 or 3.79807605720384573e-66 < d < 6.16454113121764419e185Initial program 20.3
Simplified20.3
Applied add-sqr-sqrt_binary6420.3
Applied *-un-lft-identity_binary6420.3
Applied times-frac_binary6420.3
Simplified20.3
Simplified13.1
Applied add-sqr-sqrt_binary6413.2
Applied *-un-lft-identity_binary6413.2
Applied times-frac_binary6413.3
Applied pow1_binary6413.3
Applied pow1_binary6413.3
Applied pow-prod-down_binary6413.3
Applied pow1_binary6413.3
Applied pow-prod-down_binary6413.3
Simplified12.9
Final simplification10.8
herbie shell --seed 2022125
(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))))