Average Error: 25.9 → 12.5
Time: 12.1s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -8.854189588374422409856349074066788051196 \cdot 10^{171}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 2.946859042858365719350909171488092188425 \cdot 10^{116}:\\ \;\;\;\;\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{-a}{\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}\;d \le -8.854189588374422409856349074066788051196 \cdot 10^{171}:\\
\;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;d \le 2.946859042858365719350909171488092188425 \cdot 10^{116}:\\
\;\;\;\;\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{-a}{\mathsf{hypot}\left(c, d\right)}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r101340 = b;
        double r101341 = c;
        double r101342 = r101340 * r101341;
        double r101343 = a;
        double r101344 = d;
        double r101345 = r101343 * r101344;
        double r101346 = r101342 - r101345;
        double r101347 = r101341 * r101341;
        double r101348 = r101344 * r101344;
        double r101349 = r101347 + r101348;
        double r101350 = r101346 / r101349;
        return r101350;
}

double f(double a, double b, double c, double d) {
        double r101351 = d;
        double r101352 = -8.854189588374422e+171;
        bool r101353 = r101351 <= r101352;
        double r101354 = a;
        double r101355 = c;
        double r101356 = hypot(r101355, r101351);
        double r101357 = r101354 / r101356;
        double r101358 = 2.9468590428583657e+116;
        bool r101359 = r101351 <= r101358;
        double r101360 = -r101351;
        double r101361 = b;
        double r101362 = r101355 * r101361;
        double r101363 = fma(r101354, r101360, r101362);
        double r101364 = r101363 / r101356;
        double r101365 = r101364 / r101356;
        double r101366 = -r101354;
        double r101367 = r101366 / r101356;
        double r101368 = r101359 ? r101365 : r101367;
        double r101369 = r101353 ? r101357 : r101368;
        return r101369;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.9
Target0.5
Herbie12.5
\[\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 d < -8.854189588374422e+171

    1. Initial program 44.5

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt44.5

      \[\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}}}\]
    4. Applied *-un-lft-identity44.5

      \[\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}}\]
    5. Applied times-frac44.5

      \[\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}}}\]
    6. Simplified44.5

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied associate-*r/30.6

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

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

      \[\leadsto \frac{\color{blue}{a}}{\mathsf{hypot}\left(c, d\right)}\]

    if -8.854189588374422e+171 < d < 2.9468590428583657e+116

    1. Initial program 19.6

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt19.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}}}\]
    4. Applied *-un-lft-identity19.6

      \[\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}}\]
    5. Applied times-frac19.6

      \[\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}}}\]
    6. Simplified19.6

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied associate-*r/12.5

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

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, -d, c \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\]
    11. Using strategy rm
    12. Applied clear-num12.5

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

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

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

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

    if 2.9468590428583657e+116 < d

    1. Initial program 39.9

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt39.9

      \[\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}}}\]
    4. Applied *-un-lft-identity39.9

      \[\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}}\]
    5. Applied times-frac39.9

      \[\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}}}\]
    6. Simplified39.9

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied associate-*r/26.5

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

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

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

      \[\leadsto \frac{\color{blue}{-a}}{\mathsf{hypot}\left(c, d\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification12.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -8.854189588374422409856349074066788051196 \cdot 10^{171}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 2.946859042858365719350909171488092188425 \cdot 10^{116}:\\ \;\;\;\;\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{-a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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