Average Error: 25.6 → 0.4
Time: 20.1s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \mathsf{fma}\left(-\frac{a}{\frac{\mathsf{hypot}\left(d, c\right)}{d}}, 1, \frac{a}{\frac{\mathsf{hypot}\left(d, c\right)}{d}}\right) + \frac{\mathsf{fma}\left(b, \frac{c}{\mathsf{hypot}\left(d, c\right)}, -\frac{a}{\frac{\mathsf{hypot}\left(d, c\right)}{d}}\right)}{\mathsf{hypot}\left(d, c\right)}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \mathsf{fma}\left(-\frac{a}{\frac{\mathsf{hypot}\left(d, c\right)}{d}}, 1, \frac{a}{\frac{\mathsf{hypot}\left(d, c\right)}{d}}\right) + \frac{\mathsf{fma}\left(b, \frac{c}{\mathsf{hypot}\left(d, c\right)}, -\frac{a}{\frac{\mathsf{hypot}\left(d, c\right)}{d}}\right)}{\mathsf{hypot}\left(d, c\right)}
double f(double a, double b, double c, double d) {
        double r165109 = b;
        double r165110 = c;
        double r165111 = r165109 * r165110;
        double r165112 = a;
        double r165113 = d;
        double r165114 = r165112 * r165113;
        double r165115 = r165111 - r165114;
        double r165116 = r165110 * r165110;
        double r165117 = r165113 * r165113;
        double r165118 = r165116 + r165117;
        double r165119 = r165115 / r165118;
        return r165119;
}

double f(double a, double b, double c, double d) {
        double r165120 = 1.0;
        double r165121 = d;
        double r165122 = c;
        double r165123 = hypot(r165121, r165122);
        double r165124 = r165120 / r165123;
        double r165125 = a;
        double r165126 = r165123 / r165121;
        double r165127 = r165125 / r165126;
        double r165128 = -r165127;
        double r165129 = fma(r165128, r165120, r165127);
        double r165130 = r165124 * r165129;
        double r165131 = b;
        double r165132 = r165122 / r165123;
        double r165133 = fma(r165131, r165132, r165128);
        double r165134 = r165133 / r165123;
        double r165135 = r165130 + r165134;
        return r165135;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.6
Target0.4
Herbie0.4
\[\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 25.6

    \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
  2. Simplified25.6

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

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

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

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

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

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

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

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \left(\frac{b \cdot c}{\mathsf{hypot}\left(d, c\right)} - \frac{a}{\frac{\mathsf{hypot}\left(d, c\right)}{\color{blue}{1 \cdot d}}}\right)\]
  14. Applied *-un-lft-identity9.2

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \left(\frac{b \cdot c}{\mathsf{hypot}\left(d, c\right)} - \frac{a}{\color{blue}{\frac{1}{1} \cdot \frac{\mathsf{hypot}\left(d, c\right)}{d}}}\right)\]
  16. Applied *-un-lft-identity9.2

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \left(\frac{b \cdot c}{\mathsf{hypot}\left(d, c\right)} - \color{blue}{\frac{1}{\frac{1}{1}} \cdot \frac{a}{\frac{\mathsf{hypot}\left(d, c\right)}{d}}}\right)\]
  18. Applied *-un-lft-identity9.2

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

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

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

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

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

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

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

Reproduce

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