Average Error: 25.9 → 25.6
Time: 1.0m
Precision: 64
Internal Precision: 576
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
↓
\[\begin{array}{l}
\mathbf{if}\;\frac{c \cdot c + d \cdot d}{b \cdot c - a \cdot d} \le -1.6490966697360884 \cdot 10^{-293}:\\
\;\;\;\;\frac{1}{\frac{c \cdot c + d \cdot d}{b \cdot c - a \cdot d}}\\
\mathbf{if}\;\frac{c \cdot c + d \cdot d}{b \cdot c - a \cdot d} \le 5.28628895379133 \cdot 10^{-310}:\\
\;\;\;\;\frac{-b}{\sqrt{d \cdot d + c \cdot c}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\
\end{array}\]
Try it out
Enter valid numbers for all inputs
Target
| Original | 25.9 |
|---|
| Target | 0.4 |
|---|
| Herbie | 25.6 |
|---|
\[\begin{array}{l}
\mathbf{if}\;\left|d\right| \lt \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 3 regimes
if (/ (+ (* c c) (* d d)) (- (* b c) (* a d))) < -1.6490966697360884e-293
Initial program 11.2
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
- Using strategy
rm Applied clear-num11.4
\[\leadsto \color{blue}{\frac{1}{\frac{c \cdot c + d \cdot d}{b \cdot c - a \cdot d}}}\]
if -1.6490966697360884e-293 < (/ (+ (* c c) (* d d)) (- (* b c) (* a d))) < 5.28628895379133e-310
Initial program 57.3
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
- Using strategy
rm Applied add-sqr-sqrt57.3
\[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
Applied *-un-lft-identity57.3
\[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
Applied times-frac57.3
\[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
Taylor expanded around -inf 52.3
\[\leadsto \frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \color{blue}{\left(-1 \cdot b\right)}\]
Applied simplify52.3
\[\leadsto \color{blue}{\frac{-b}{\sqrt{d \cdot d + c \cdot c}}}\]
if 5.28628895379133e-310 < (/ (+ (* c c) (* d d)) (- (* b c) (* a d)))
Initial program 30.9
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
- Recombined 3 regimes into one program.
Runtime
herbie shell --seed 2020178
(FPCore (a b c d)
:name "Complex division, imag part"
: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))))