Average Error: 26.3 → 0.9
Time: 13.4s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{\mathsf{fma}\left(\frac{\sqrt[3]{c} \cdot \sqrt[3]{c}}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{\sqrt[3]{c}}{\frac{\sqrt{\mathsf{hypot}\left(c, d\right)}}{b}}, \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-a\right)\right)}{\mathsf{hypot}\left(c, d\right)} + \frac{\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\left(-a\right) + a\right)}{\mathsf{hypot}\left(c, d\right)}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\frac{\mathsf{fma}\left(\frac{\sqrt[3]{c} \cdot \sqrt[3]{c}}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{\sqrt[3]{c}}{\frac{\sqrt{\mathsf{hypot}\left(c, d\right)}}{b}}, \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-a\right)\right)}{\mathsf{hypot}\left(c, d\right)} + \frac{\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\left(-a\right) + a\right)}{\mathsf{hypot}\left(c, d\right)}
double f(double a, double b, double c, double d) {
        double r124847 = b;
        double r124848 = c;
        double r124849 = r124847 * r124848;
        double r124850 = a;
        double r124851 = d;
        double r124852 = r124850 * r124851;
        double r124853 = r124849 - r124852;
        double r124854 = r124848 * r124848;
        double r124855 = r124851 * r124851;
        double r124856 = r124854 + r124855;
        double r124857 = r124853 / r124856;
        return r124857;
}

double f(double a, double b, double c, double d) {
        double r124858 = c;
        double r124859 = cbrt(r124858);
        double r124860 = r124859 * r124859;
        double r124861 = d;
        double r124862 = hypot(r124858, r124861);
        double r124863 = sqrt(r124862);
        double r124864 = r124860 / r124863;
        double r124865 = b;
        double r124866 = r124863 / r124865;
        double r124867 = r124859 / r124866;
        double r124868 = r124861 / r124862;
        double r124869 = a;
        double r124870 = -r124869;
        double r124871 = r124868 * r124870;
        double r124872 = fma(r124864, r124867, r124871);
        double r124873 = r124872 / r124862;
        double r124874 = r124870 + r124869;
        double r124875 = r124868 * r124874;
        double r124876 = r124875 / r124862;
        double r124877 = r124873 + r124876;
        return r124877;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.3
Target0.5
Herbie0.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. Initial program 26.3

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

    \[\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.3

    \[\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.3

    \[\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.3

    \[\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.3

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

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

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

    \[\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-cube-cbrt9.9

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

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

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c}{\color{blue}{\frac{\sqrt{\mathsf{hypot}\left(c, d\right)}}{1} \cdot \frac{\sqrt{\mathsf{hypot}\left(c, d\right)}}{b}}} - \left(\sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}} \cdot \sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\right) \cdot \sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\right)\]
  17. Applied add-cube-cbrt10.3

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

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

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(\frac{\sqrt[3]{c} \cdot \sqrt[3]{c}}{\frac{\sqrt{\mathsf{hypot}\left(c, d\right)}}{1}}, \frac{\sqrt[3]{c}}{\frac{\sqrt{\mathsf{hypot}\left(c, d\right)}}{b}}, -\sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}} \cdot \sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\right)\right) + \mathsf{fma}\left(-\sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}}, \sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}} \cdot \sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}}, \sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}} \cdot \sqrt[3]{\frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\right)\right)\right)}\]
  20. Applied distribute-lft-in9.9

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

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

    \[\leadsto \frac{\mathsf{fma}\left(\frac{\sqrt[3]{c} \cdot \sqrt[3]{c}}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{\sqrt[3]{c}}{\frac{\sqrt{\mathsf{hypot}\left(c, d\right)}}{b}}, \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-a\right)\right)}{\mathsf{hypot}\left(c, d\right)} + \color{blue}{\frac{\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\left(-a\right) + a\right)}{\mathsf{hypot}\left(c, d\right)}}\]
  23. Final simplification0.9

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

Reproduce

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