Average Error: 25.8 → 25.2
Time: 33.9s
Precision: 64
Internal Precision: 384
\[\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.728614260823253 \cdot 10^{-308}:\\
\;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\
\mathbf{if}\;\frac{c \cdot c + d \cdot d}{b \cdot c - a \cdot d} \le 0.0:\\
\;\;\;\;\frac{-b}{\sqrt{c \cdot c + d \cdot d}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\
\end{array}\]
Target
| Original | 25.8 |
|---|
| Target | 0.5 |
|---|
| Herbie | 25.2 |
|---|
\[\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 2 regimes
if (/ (+ (* c c) (* d d)) (- (* b c) (* a d))) < -1.728614260823253e-308 or 0.0 < (/ (+ (* c c) (* d d)) (- (* b c) (* a d)))
Initial program 23.3
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
- Using strategy
rm Applied add-sqr-sqrt23.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 associate-/r*23.2
\[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
if -1.728614260823253e-308 < (/ (+ (* c c) (* d d)) (- (* b c) (* a d))) < 0.0
Initial program 60.6
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
- Using strategy
rm Applied add-sqr-sqrt60.6
\[\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 associate-/r*60.6
\[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
Taylor expanded around -inf 52.9
\[\leadsto \frac{\color{blue}{-1 \cdot b}}{\sqrt{c \cdot c + d \cdot d}}\]
Applied simplify52.9
\[\leadsto \color{blue}{\frac{-b}{\sqrt{c \cdot c + d \cdot d}}}\]
- Recombined 2 regimes into one program.
Runtime
herbie shell --seed '#(1070227846 1561819246 480764335 4016816270 2602869839 2117310382)'
(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))))