\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
↓
\[\begin{array}{l}
\mathbf{if}\;d \leq -2.623594142603243 \cdot 10^{+102}:\\
\;\;\;\;\frac{-a}{d}\\
\mathbf{elif}\;d \leq -2.178304667980649 \cdot 10^{-161}:\\
\;\;\;\;\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 4.0526762739374186 \cdot 10^{-107}:\\
\;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\
\mathbf{elif}\;d \leq 5.943546987088161 \cdot 10^{+45}:\\
\;\;\;\;\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 \cdot c}{d \cdot d} - \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 -2.623594142603243 \cdot 10^{+102}:\\
\;\;\;\;\frac{-a}{d}\\
\mathbf{elif}\;d \leq -2.178304667980649 \cdot 10^{-161}:\\
\;\;\;\;\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 4.0526762739374186 \cdot 10^{-107}:\\
\;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\
\mathbf{elif}\;d \leq 5.943546987088161 \cdot 10^{+45}:\\
\;\;\;\;\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 \cdot c}{d \cdot d} - \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 -2.623594142603243e+102)
(/ (- a) d)
(if (<= d -2.178304667980649e-161)
(/
(/ (- (* b c) (* d a)) (sqrt (+ (* c c) (* d d))))
(sqrt (+ (* c c) (* d d))))
(if (<= d 4.0526762739374186e-107)
(- (/ b c) (/ (* d a) (* c c)))
(if (<= d 5.943546987088161e+45)
(/
(/ (- (* b c) (* d a)) (sqrt (+ (* c c) (* d d))))
(sqrt (+ (* c c) (* d d))))
(- (/ (* b c) (* d d)) (/ 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 <= -2.623594142603243e+102) {
tmp = -a / d;
} else if (d <= -2.178304667980649e-161) {
tmp = (((b * c) - (d * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
} else if (d <= 4.0526762739374186e-107) {
tmp = (b / c) - ((d * a) / (c * c));
} else if (d <= 5.943546987088161e+45) {
tmp = (((b * c) - (d * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
} else {
tmp = ((b * c) / (d * d)) - (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
26.6
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
27.1
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.3
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
27.0
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
26.7
Cost
34112
\[\frac{\frac{1}{\sqrt{\sqrt{c \cdot c + d \cdot d}}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\sqrt{c \cdot c + d \cdot d}}}}{\sqrt{c \cdot c + d \cdot d}}\]
Alternative 5
Error
26.6
Cost
33984
\[\frac{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{\sqrt{c \cdot c + d \cdot d}}}}{\sqrt{\sqrt{c \cdot c + d \cdot d}}}}{\sqrt{c \cdot c + d \cdot d}}\]
Alternative 6
Error
27.0
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
27.0
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
27.0
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}\]
Alternative 9
Error
27.0
Cost
21184
\[\frac{\frac{b \cdot c - a \cdot d}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}}}{\sqrt[3]{c \cdot c + d \cdot d}}\]
\[\leadsto \begin{array}{l}
\mathbf{if}\;d \leq -2.623594142603243 \cdot 10^{+102}:\\
\;\;\;\;\frac{-a}{d}\\
\mathbf{elif}\;d \leq -2.178304667980649 \cdot 10^{-161}:\\
\;\;\;\;\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 4.0526762739374186 \cdot 10^{-107}:\\
\;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\
\mathbf{elif}\;d \leq 5.943546987088161 \cdot 10^{+45}:\\
\;\;\;\;\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 \cdot c}{d \cdot d} - \frac{a}{d}\\
\end{array}\]
Reproduce
herbie shell --seed 2021022
(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))))