\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\]
↓
\[\begin{array}{l}
t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{c}} - d \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\right)\\
t_1 := b \cdot c - d \cdot a\\
t_2 := \frac{t_1}{c \cdot c + d \cdot d}\\
\mathbf{if}\;t_2 \leq -\infty:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_2 \leq 5 \cdot 10^{+286}:\\
\;\;\;\;\frac{\frac{t_1}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\]
(FPCore (a b c d)
:precision binary64
(/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
↓
(FPCore (a b c d)
:precision binary64
(let* ((t_0
(*
(/ 1.0 (hypot c d))
(- (/ b (/ (hypot c d) c)) (* d (/ a (hypot c d))))))
(t_1 (- (* b c) (* d a)))
(t_2 (/ t_1 (+ (* c c) (* d d)))))
(if (<= t_2 (- INFINITY))
t_0
(if (<= t_2 5e+286) (/ (/ t_1 (hypot c d)) (hypot c d)) t_0))))
double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
↓
\begin{array}{l}
t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{c}} - d \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\right)\\
t_1 := b \cdot c - d \cdot a\\
t_2 := \frac{t_1}{c \cdot c + d \cdot d}\\
\mathbf{if}\;t_2 \leq -\infty:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_2 \leq 5 \cdot 10^{+286}:\\
\;\;\;\;\frac{\frac{t_1}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
Error
Try it out
Results
Enter valid numbers for all inputs
Target
Original
25.5
Target
0.5
Herbie
0.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}
\]
Derivation
Split input into 2 regimes
if (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (+.f64 (*.f64 c c) (*.f64 d d))) < -inf.0 or 5.0000000000000004e286 < (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (+.f64 (*.f64 c c) (*.f64 d d)))
Initial program 62.9
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\]
Applied egg-rr58.3
\[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}
\]
(-.f64 (*.f64 (/.f64 b (sqrt.f64 (hypot.f64 c d))) (/.f64 c (sqrt.f64 (hypot.f64 c d)))) (*.f64 (/.f64 a (hypot.f64 c d)) d)): 0 points increase in error, 0 points decrease in error
(-.f64 (*.f64 (/.f64 b (sqrt.f64 (hypot.f64 c d))) (/.f64 c (sqrt.f64 (hypot.f64 c d)))) (Rewrite<= associate-/r/_binary64 (/.f64 a (/.f64 (hypot.f64 c d) d)))): 33 points increase in error, 26 points decrease in error
(-.f64 (*.f64 (/.f64 b (sqrt.f64 (hypot.f64 c d))) (/.f64 c (sqrt.f64 (hypot.f64 c d)))) (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 a d) (hypot.f64 c d)))): 41 points increase in error, 12 points decrease in error
(Rewrite<= unsub-neg_binary64 (+.f64 (*.f64 (/.f64 b (sqrt.f64 (hypot.f64 c d))) (/.f64 c (sqrt.f64 (hypot.f64 c d)))) (neg.f64 (/.f64 (*.f64 a d) (hypot.f64 c d))))): 0 points increase in error, 0 points decrease in error
(Rewrite<= fma-udef_binary64 (fma.f64 (/.f64 b (sqrt.f64 (hypot.f64 c d))) (/.f64 c (sqrt.f64 (hypot.f64 c d))) (neg.f64 (/.f64 (*.f64 a d) (hypot.f64 c d))))): 0 points increase in error, 0 points decrease in error
herbie shell --seed 2022335
(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))))