Average Error: 26.5 → 17.6
Time: 37.0s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.304579087607779 \cdot 10^{+161}:\\ \;\;\;\;\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le -7.011154002702933 \cdot 10^{+91}:\\ \;\;\;\;\frac{-b}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{1}{\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 -1.304579087607779 \cdot 10^{+161}:\\
\;\;\;\;\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le -7.011154002702933 \cdot 10^{+91}:\\
\;\;\;\;\frac{-b}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r5548867 = b;
        double r5548868 = c;
        double r5548869 = r5548867 * r5548868;
        double r5548870 = a;
        double r5548871 = d;
        double r5548872 = r5548870 * r5548871;
        double r5548873 = r5548869 - r5548872;
        double r5548874 = r5548868 * r5548868;
        double r5548875 = r5548871 * r5548871;
        double r5548876 = r5548874 + r5548875;
        double r5548877 = r5548873 / r5548876;
        return r5548877;
}

double f(double a, double b, double c, double d) {
        double r5548878 = c;
        double r5548879 = -1.304579087607779e+161;
        bool r5548880 = r5548878 <= r5548879;
        double r5548881 = b;
        double r5548882 = r5548881 * r5548878;
        double r5548883 = d;
        double r5548884 = a;
        double r5548885 = r5548883 * r5548884;
        double r5548886 = r5548882 - r5548885;
        double r5548887 = hypot(r5548883, r5548878);
        double r5548888 = r5548886 / r5548887;
        double r5548889 = 1.0;
        double r5548890 = r5548889 / r5548887;
        double r5548891 = r5548888 * r5548890;
        double r5548892 = -7.011154002702933e+91;
        bool r5548893 = r5548878 <= r5548892;
        double r5548894 = -r5548881;
        double r5548895 = r5548878 * r5548878;
        double r5548896 = fma(r5548883, r5548883, r5548895);
        double r5548897 = sqrt(r5548896);
        double r5548898 = r5548894 / r5548897;
        double r5548899 = r5548893 ? r5548898 : r5548891;
        double r5548900 = r5548880 ? r5548891 : r5548899;
        return r5548900;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.5
Target0.5
Herbie17.6
\[\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 2 regimes
  2. if c < -1.304579087607779e+161 or -7.011154002702933e+91 < c

    1. Initial program 26.5

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

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(b, c, -a \cdot d\right)}}{\mathsf{fma}\left(d, d, c \cdot c\right)}\]
    5. Using strategy rm
    6. Applied clear-num26.6

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

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

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{1 \cdot \left(b \cdot c - d \cdot a\right)}}}\]
    10. Applied add-sqr-sqrt26.6

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

      \[\leadsto \frac{1}{\color{blue}{\frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{1} \cdot \frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{b \cdot c - d \cdot a}}}\]
    12. Applied add-cube-cbrt26.6

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

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

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

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

    if -1.304579087607779e+161 < c < -7.011154002702933e+91

    1. Initial program 27.0

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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 associate-/r*26.9

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

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

      \[\leadsto \frac{\color{blue}{-b}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification17.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.304579087607779 \cdot 10^{+161}:\\ \;\;\;\;\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le -7.011154002702933 \cdot 10^{+91}:\\ \;\;\;\;\frac{-b}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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