Average Error: 26.4 → 12.9
Time: 24.5s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.50079102587126544 \cdot 10^{162}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(c, d\right)} \cdot b\\ \mathbf{elif}\;c \le 2.9196597497857521 \cdot 10^{126}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, -d, c \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -1.50079102587126544 \cdot 10^{162}:\\
\;\;\;\;\frac{-1}{\mathsf{hypot}\left(c, d\right)} \cdot b\\

\mathbf{elif}\;c \le 2.9196597497857521 \cdot 10^{126}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, -d, c \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r121305 = b;
        double r121306 = c;
        double r121307 = r121305 * r121306;
        double r121308 = a;
        double r121309 = d;
        double r121310 = r121308 * r121309;
        double r121311 = r121307 - r121310;
        double r121312 = r121306 * r121306;
        double r121313 = r121309 * r121309;
        double r121314 = r121312 + r121313;
        double r121315 = r121311 / r121314;
        return r121315;
}

double f(double a, double b, double c, double d) {
        double r121316 = c;
        double r121317 = -1.5007910258712654e+162;
        bool r121318 = r121316 <= r121317;
        double r121319 = -1.0;
        double r121320 = d;
        double r121321 = hypot(r121316, r121320);
        double r121322 = r121319 / r121321;
        double r121323 = b;
        double r121324 = r121322 * r121323;
        double r121325 = 2.919659749785752e+126;
        bool r121326 = r121316 <= r121325;
        double r121327 = a;
        double r121328 = -r121320;
        double r121329 = r121316 * r121323;
        double r121330 = fma(r121327, r121328, r121329);
        double r121331 = r121330 / r121321;
        double r121332 = r121331 / r121321;
        double r121333 = r121323 / r121321;
        double r121334 = r121326 ? r121332 : r121333;
        double r121335 = r121318 ? r121324 : r121334;
        return r121335;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.4
Target0.5
Herbie12.9
\[\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

  1. Split input into 3 regimes
  2. if c < -1.5007910258712654e+162

    1. Initial program 44.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified44.7

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt44.7

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity44.7

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac44.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    7. Simplified44.7

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    8. Simplified30.3

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
    9. Taylor expanded around -inf 13.9

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(-1 \cdot b\right)}\]
    10. Simplified13.9

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(-b\right)}\]

    if -1.5007910258712654e+162 < c < 2.919659749785752e+126

    1. Initial program 19.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified19.7

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt19.7

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity19.7

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac19.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    7. Simplified19.7

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    8. Simplified12.5

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
    9. Using strategy rm
    10. Applied *-un-lft-identity12.5

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(c, d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\]
    11. Applied add-sqr-sqrt12.5

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{1 \cdot \mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\]
    12. Applied times-frac12.5

      \[\leadsto \color{blue}{\left(\frac{\sqrt{1}}{1} \cdot \frac{\sqrt{1}}{\mathsf{hypot}\left(c, d\right)}\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\]
    13. Applied associate-*l*12.5

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{1} \cdot \left(\frac{\sqrt{1}}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\right)}\]
    14. Simplified12.4

      \[\leadsto \frac{\sqrt{1}}{1} \cdot \color{blue}{\frac{\frac{\mathsf{fma}\left(a, -d, c \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}}\]

    if 2.919659749785752e+126 < c

    1. Initial program 41.9

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified41.9

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt41.9

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity41.9

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac41.9

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    7. Simplified41.9

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    8. Simplified28.6

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
    9. Using strategy rm
    10. Applied *-un-lft-identity28.6

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(c, d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\]
    11. Applied add-sqr-sqrt28.6

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{1 \cdot \mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\]
    12. Applied times-frac28.6

      \[\leadsto \color{blue}{\left(\frac{\sqrt{1}}{1} \cdot \frac{\sqrt{1}}{\mathsf{hypot}\left(c, d\right)}\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\]
    13. Applied associate-*l*28.6

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{1} \cdot \left(\frac{\sqrt{1}}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\right)}\]
    14. Simplified28.6

      \[\leadsto \frac{\sqrt{1}}{1} \cdot \color{blue}{\frac{\frac{\mathsf{fma}\left(a, -d, c \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}}\]
    15. Taylor expanded around 0 14.3

      \[\leadsto \frac{\sqrt{1}}{1} \cdot \frac{\color{blue}{b}}{\mathsf{hypot}\left(c, d\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification12.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.50079102587126544 \cdot 10^{162}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(c, d\right)} \cdot b\\ \mathbf{elif}\;c \le 2.9196597497857521 \cdot 10^{126}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, -d, c \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2019199 +o rules:numerics
(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))))