Average Error: 26.0 → 0.8
Time: 4.5s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\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)}{\mathsf{hypot}\left(c, d\right)} + \left(\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}
\frac{\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\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)}{\mathsf{hypot}\left(c, d\right)} + \left(\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 r116622 = b;
        double r116623 = c;
        double r116624 = r116622 * r116623;
        double r116625 = a;
        double r116626 = d;
        double r116627 = r116625 * r116626;
        double r116628 = r116624 - r116627;
        double r116629 = r116623 * r116623;
        double r116630 = r116626 * r116626;
        double r116631 = r116629 + r116630;
        double r116632 = r116628 / r116631;
        return r116632;
}

double f(double a, double b, double c, double d) {
        double r116633 = b;
        double r116634 = c;
        double r116635 = d;
        double r116636 = hypot(r116634, r116635);
        double r116637 = sqrt(r116636);
        double r116638 = r116633 / r116637;
        double r116639 = r116634 / r116637;
        double r116640 = r116635 / r116637;
        double r116641 = a;
        double r116642 = r116641 / r116637;
        double r116643 = r116640 * r116642;
        double r116644 = -r116643;
        double r116645 = fma(r116638, r116639, r116644);
        double r116646 = r116645 / r116636;
        double r116647 = -r116642;
        double r116648 = r116647 + r116642;
        double r116649 = r116640 * r116648;
        double r116650 = 1.0;
        double r116651 = r116650 / r116636;
        double r116652 = r116649 * r116651;
        double r116653 = r116646 + r116652;
        return r116653;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.3
Herbie0.8
\[\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.0

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

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

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

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

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
  8. Using strategy rm
  9. Applied div-sub16.8

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \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)}\]
  10. Using strategy rm
  11. Applied add-sqr-sqrt16.8

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

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

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

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\left(\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\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) + \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)}\]
  16. Applied distribute-lft-in0.9

    \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\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) + \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \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)}\]
  17. Simplified0.8

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\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)}{\mathsf{hypot}\left(c, d\right)}} + \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \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)\]
  18. Simplified0.8

    \[\leadsto \frac{\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\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)}{\mathsf{hypot}\left(c, d\right)} + \color{blue}{\left(\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)}}\]
  19. Final simplification0.8

    \[\leadsto \frac{\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\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)}{\mathsf{hypot}\left(c, d\right)} + \left(\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 2019362 +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))))