Average Error: 26.4 → 1.0
Time: 15.7s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\left(\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, b, \left(\frac{-1}{\sqrt{\sqrt{\mathsf{hypot}\left(c, d\right)}}} \cdot \frac{d}{\sqrt{\sqrt{\mathsf{hypot}\left(c, d\right)}}}\right) \cdot \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\left(-\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right)\right) \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\left(\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, b, \left(\frac{-1}{\sqrt{\sqrt{\mathsf{hypot}\left(c, d\right)}}} \cdot \frac{d}{\sqrt{\sqrt{\mathsf{hypot}\left(c, d\right)}}}\right) \cdot \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\left(-\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right)\right) \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}
double f(double a, double b, double c, double d) {
        double r101462 = b;
        double r101463 = c;
        double r101464 = r101462 * r101463;
        double r101465 = a;
        double r101466 = d;
        double r101467 = r101465 * r101466;
        double r101468 = r101464 - r101467;
        double r101469 = r101463 * r101463;
        double r101470 = r101466 * r101466;
        double r101471 = r101469 + r101470;
        double r101472 = r101468 / r101471;
        return r101472;
}

double f(double a, double b, double c, double d) {
        double r101473 = c;
        double r101474 = d;
        double r101475 = hypot(r101473, r101474);
        double r101476 = r101473 / r101475;
        double r101477 = b;
        double r101478 = -1.0;
        double r101479 = sqrt(r101475);
        double r101480 = sqrt(r101479);
        double r101481 = r101478 / r101480;
        double r101482 = r101474 / r101480;
        double r101483 = r101481 * r101482;
        double r101484 = a;
        double r101485 = r101484 / r101479;
        double r101486 = r101483 * r101485;
        double r101487 = fma(r101476, r101477, r101486);
        double r101488 = r101474 / r101479;
        double r101489 = -r101485;
        double r101490 = r101489 + r101485;
        double r101491 = r101488 * r101490;
        double r101492 = r101487 + r101491;
        double r101493 = 1.0;
        double r101494 = r101493 / r101475;
        double r101495 = r101492 * r101494;
        return r101495;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.4
Target0.5
Herbie1.0
\[\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. Initial program 26.4

    \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
  2. Simplified26.4

    \[\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-sqrt26.4

    \[\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-identity26.4

    \[\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-frac26.4

    \[\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. Simplified26.4

    \[\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. Simplified17.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. Using strategy rm
  10. Applied div-sub17.3

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\color{blue}{\frac{c}{\frac{\mathsf{hypot}\left(c, d\right)}{b}}} - \frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}\right)\]
  12. Using strategy rm
  13. Applied add-sqr-sqrt10.0

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c}{\frac{\mathsf{hypot}\left(c, d\right)}{b}} - \color{blue}{\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}\right)\]
  15. Applied add-sqr-sqrt23.4

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

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

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, b, -\frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \color{blue}{\frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\left(-\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right)}\right)\]
  19. Using strategy rm
  20. Applied add-sqr-sqrt0.9

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, b, -\frac{d}{\color{blue}{\sqrt{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \sqrt{\sqrt{\mathsf{hypot}\left(c, d\right)}}}} \cdot \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\left(-\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right)\right)\]
  22. Applied *-un-lft-identity1.0

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, b, -\frac{\color{blue}{1 \cdot d}}{\sqrt{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \sqrt{\sqrt{\mathsf{hypot}\left(c, d\right)}}} \cdot \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\left(-\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right)\right)\]
  23. Applied times-frac1.0

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

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

Reproduce

herbie shell --seed 2019322 +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))))