Average Error: 26.5 → 13.3
Time: 11.1s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.656598338970386986288953354334473315928 \cdot 10^{155}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.884834830237686987891437300171514108378 \cdot 10^{188}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, b, -a \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -1.656598338970386986288953354334473315928 \cdot 10^{155}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \le 1.884834830237686987891437300171514108378 \cdot 10^{188}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(c, b, -a \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r74867 = b;
        double r74868 = c;
        double r74869 = r74867 * r74868;
        double r74870 = a;
        double r74871 = d;
        double r74872 = r74870 * r74871;
        double r74873 = r74869 - r74872;
        double r74874 = r74868 * r74868;
        double r74875 = r74871 * r74871;
        double r74876 = r74874 + r74875;
        double r74877 = r74873 / r74876;
        return r74877;
}

double f(double a, double b, double c, double d) {
        double r74878 = c;
        double r74879 = -1.656598338970387e+155;
        bool r74880 = r74878 <= r74879;
        double r74881 = b;
        double r74882 = -r74881;
        double r74883 = d;
        double r74884 = hypot(r74878, r74883);
        double r74885 = r74882 / r74884;
        double r74886 = 1.884834830237687e+188;
        bool r74887 = r74878 <= r74886;
        double r74888 = a;
        double r74889 = r74888 * r74883;
        double r74890 = -r74889;
        double r74891 = fma(r74878, r74881, r74890);
        double r74892 = r74891 / r74884;
        double r74893 = r74892 / r74884;
        double r74894 = r74881 / r74884;
        double r74895 = r74887 ? r74893 : r74894;
        double r74896 = r74880 ? r74885 : r74895;
        return r74896;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.5
Target0.4
Herbie13.3
\[\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 < -1.656598338970387e+155

    1. Initial program 45.2

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

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

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

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

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

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

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

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

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

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

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

    if -1.656598338970387e+155 < c < 1.884834830237687e+188

    1. Initial program 20.8

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

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

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

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

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

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

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

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

      \[\leadsto 1 \cdot \color{blue}{\frac{\frac{\mathsf{fma}\left(b, c, \left(-d\right) \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}}\]
    12. Using strategy rm
    13. Applied clear-num13.4

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

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

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

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

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

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

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

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

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

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

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

    if 1.884834830237687e+188 < c

    1. Initial program 44.7

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.656598338970386986288953354334473315928 \cdot 10^{155}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.884834830237686987891437300171514108378 \cdot 10^{188}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, b, -a \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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