\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
↓
\[\begin{array}{l}
\mathbf{if}\;d \leq -1.1437132202364462 \cdot 10^{+93}:\\
\;\;\;\;\frac{-a}{d}\\
\mathbf{elif}\;d \leq -7.2230969502839745 \cdot 10^{-140}:\\
\;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\
\mathbf{elif}\;d \leq -1.3434382879141502 \cdot 10^{-278}:\\
\;\;\;\;\frac{b}{c} - \frac{a}{\frac{c \cdot c}{d}}\\
\mathbf{elif}\;d \leq 1.0231125141194154 \cdot 10^{-128}:\\
\;\;\;\;\frac{b}{c}\\
\mathbf{elif}\;d \leq 2.405852373616342 \cdot 10^{+98}:\\
\;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\
\end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
↓
\begin{array}{l}
\mathbf{if}\;d \leq -1.1437132202364462 \cdot 10^{+93}:\\
\;\;\;\;\frac{-a}{d}\\
\mathbf{elif}\;d \leq -7.2230969502839745 \cdot 10^{-140}:\\
\;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\
\mathbf{elif}\;d \leq -1.3434382879141502 \cdot 10^{-278}:\\
\;\;\;\;\frac{b}{c} - \frac{a}{\frac{c \cdot c}{d}}\\
\mathbf{elif}\;d \leq 1.0231125141194154 \cdot 10^{-128}:\\
\;\;\;\;\frac{b}{c}\\
\mathbf{elif}\;d \leq 2.405852373616342 \cdot 10^{+98}:\\
\;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\
\end{array}
(FPCore (a b c d)
:precision binary64
(/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
↓
(FPCore (a b c d)
:precision binary64
(if (<= d -1.1437132202364462e+93)
(/ (- a) d)
(if (<= d -7.2230969502839745e-140)
(/
(/ (- (* b c) (* d a)) (sqrt (+ (* c c) (* d d))))
(sqrt (+ (* c c) (* d d))))
(if (<= d -1.3434382879141502e-278)
(- (/ b c) (/ a (/ (* c c) d)))
(if (<= d 1.0231125141194154e-128)
(/ b c)
(if (<= d 2.405852373616342e+98)
(/
(/ (- (* b c) (* d a)) (sqrt (+ (* c c) (* d d))))
(sqrt (+ (* c c) (* d d))))
(- (/ b (/ (* d d) c)) (/ a d))))))))
double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
↓
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.1437132202364462e+93) {
tmp = -a / d;
} else if (d <= -7.2230969502839745e-140) {
tmp = (((b * c) - (d * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
} else if (d <= -1.3434382879141502e-278) {
tmp = (b / c) - (a / ((c * c) / d));
} else if (d <= 1.0231125141194154e-128) {
tmp = b / c;
} else if (d <= 2.405852373616342e+98) {
tmp = (((b * c) - (d * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
} else {
tmp = (b / ((d * d) / c)) - (a / d);
}
return tmp;
}
Error
Bits error versus a
Bits error versus b
Bits error versus c
Bits error versus d
Try it out
Results
Enter valid numbers for all inputs
Target
Original
25.7
Target
0.5
Herbie
14.8
\[\begin{array}{l}
\mathbf{if}\;\left|d\right| < \left|c\right|:\\
\;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(-a\right) + b \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\
\end{array}\]
Alternatives
Alternative 1
Error
26.3
Cost
41408
\[\frac{\sqrt[3]{b \cdot c - a \cdot d} \cdot \sqrt[3]{b \cdot c - a \cdot d}}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}} \cdot \frac{\sqrt[3]{b \cdot c - a \cdot d}}{\sqrt[3]{c \cdot c + d \cdot d}}\]
Alternative 2
Error
45.0
Cost
34496
\[\frac{\sqrt{b \cdot c - a \cdot d}}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}} \cdot \frac{\sqrt{b \cdot c - a \cdot d}}{\sqrt[3]{c \cdot c + d \cdot d}}\]
Alternative 3
Error
26.2
Cost
34496
\[\frac{\sqrt[3]{b \cdot c - a \cdot d} \cdot \sqrt[3]{b \cdot c - a \cdot d}}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{\sqrt[3]{b \cdot c - a \cdot d}}{\sqrt{c \cdot c + d \cdot d}}\]
Alternative 4
Error
44.8
Cost
27584
\[\frac{\sqrt{b \cdot c - a \cdot d}}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{\sqrt{b \cdot c - a \cdot d}}{\sqrt{c \cdot c + d \cdot d}}\]
Alternative 5
Error
27.5
Cost
27072
\[\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{e^{\log \left(\sqrt{c \cdot c + d \cdot d}\right)}}\]
Alternative 6
Error
26.2
Cost
22208
\[\sqrt[3]{\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}} \cdot \left(\sqrt[3]{\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}} \cdot \sqrt[3]{\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}}\right)\]
Alternative 7
Error
26.2
Cost
21312
\[\frac{1}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt[3]{c \cdot c + d \cdot d}}\]
Alternative 8
Error
26.2
Cost
21184
\[\left(\sqrt[3]{b \cdot c - a \cdot d} \cdot \sqrt[3]{b \cdot c - a \cdot d}\right) \cdot \frac{\sqrt[3]{b \cdot c - a \cdot d}}{c \cdot c + d \cdot d}\]
\[\leadsto \begin{array}{l}
\mathbf{if}\;d \leq -1.1437132202364462 \cdot 10^{+93}:\\
\;\;\;\;\frac{-a}{d}\\
\mathbf{elif}\;d \leq -7.2230969502839745 \cdot 10^{-140}:\\
\;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\
\mathbf{elif}\;d \leq -1.3434382879141502 \cdot 10^{-278}:\\
\;\;\;\;\frac{b}{c} - \frac{a}{\frac{c \cdot c}{d}}\\
\mathbf{elif}\;d \leq 1.0231125141194154 \cdot 10^{-128}:\\
\;\;\;\;\frac{b}{c}\\
\mathbf{elif}\;d \leq 2.405852373616342 \cdot 10^{+98}:\\
\;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\
\end{array}\]
Reproduce
herbie shell --seed 2021042
(FPCore (a b c d)
:name "Complex division, imag part"
:precision binary64
:herbie-target
(if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d)))))
(/ (- (* b c) (* a d)) (+ (* c c) (* d d))))