Average Error: 25.7 → 12.5
Time: 11.3s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -5.9876509466672635 \cdot 10^{162}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)} \cdot \left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right)\\ \mathbf{elif}\;c \le 2.5019882417216274 \cdot 10^{136}:\\ \;\;\;\;\left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, c, -d \cdot a\right)}}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -5.9876509466672635 \cdot 10^{162}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)} \cdot \left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right)\\

\mathbf{elif}\;c \le 2.5019882417216274 \cdot 10^{136}:\\
\;\;\;\;\left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, c, -d \cdot a\right)}}}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{else}:\\
\;\;\;\;\left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r124979 = b;
        double r124980 = c;
        double r124981 = r124979 * r124980;
        double r124982 = a;
        double r124983 = d;
        double r124984 = r124982 * r124983;
        double r124985 = r124981 - r124984;
        double r124986 = r124980 * r124980;
        double r124987 = r124983 * r124983;
        double r124988 = r124986 + r124987;
        double r124989 = r124985 / r124988;
        return r124989;
}

double f(double a, double b, double c, double d) {
        double r124990 = c;
        double r124991 = -5.9876509466672635e+162;
        bool r124992 = r124990 <= r124991;
        double r124993 = b;
        double r124994 = -r124993;
        double r124995 = d;
        double r124996 = hypot(r124995, r124990);
        double r124997 = r124994 / r124996;
        double r124998 = 1.0;
        double r124999 = cbrt(r124998);
        double r125000 = r124999 * r124999;
        double r125001 = r124997 * r125000;
        double r125002 = 2.5019882417216274e+136;
        bool r125003 = r124990 <= r125002;
        double r125004 = a;
        double r125005 = r124995 * r125004;
        double r125006 = -r125005;
        double r125007 = fma(r124993, r124990, r125006);
        double r125008 = r124996 / r125007;
        double r125009 = r124998 / r125008;
        double r125010 = r125009 / r124996;
        double r125011 = r125000 * r125010;
        double r125012 = r124993 / r124996;
        double r125013 = r125000 * r125012;
        double r125014 = r125003 ? r125011 : r125013;
        double r125015 = r124992 ? r125001 : r125014;
        return r125015;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
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 c < -5.9876509466672635e+162

    1. Initial program 43.8

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified43.8

      \[\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-sqrt43.8

      \[\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-identity43.8

      \[\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-frac43.8

      \[\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. Simplified43.8

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

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

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(d, c\right)}} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    11. Applied add-cube-cbrt29.5

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

      \[\leadsto \color{blue}{\left(\frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{1} \cdot \frac{\sqrt[3]{1}}{\mathsf{hypot}\left(d, c\right)}\right)} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    13. Applied associate-*l*29.5

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

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

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

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

    if -5.9876509466672635e+162 < c < 2.5019882417216274e+136

    1. Initial program 19.2

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

      \[\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-sqrt19.2

      \[\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-identity19.2

      \[\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-frac19.2

      \[\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. Simplified19.2

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

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

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(d, c\right)}} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    11. Applied add-cube-cbrt12.1

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

      \[\leadsto \color{blue}{\left(\frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{1} \cdot \frac{\sqrt[3]{1}}{\mathsf{hypot}\left(d, c\right)}\right)} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    13. Applied associate-*l*12.1

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

      \[\leadsto \frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{1} \cdot \color{blue}{\frac{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}}\]
    15. Using strategy rm
    16. Applied clear-num12.1

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

    if 2.5019882417216274e+136 < c

    1. Initial program 42.2

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

      \[\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-sqrt42.2

      \[\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-identity42.2

      \[\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-frac42.2

      \[\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. Simplified42.2

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

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

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

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

      \[\leadsto \color{blue}{\left(\frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{1} \cdot \frac{\sqrt[3]{1}}{\mathsf{hypot}\left(d, c\right)}\right)} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    13. Applied associate-*l*27.3

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.9876509466672635 \cdot 10^{162}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)} \cdot \left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right)\\ \mathbf{elif}\;c \le 2.5019882417216274 \cdot 10^{136}:\\ \;\;\;\;\left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, c, -d \cdot a\right)}}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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