Average Error: 25.4 → 16.5
Time: 12.8s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le 3.315223608026935 \cdot 10^{+87}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \le 1.263326239685677 \cdot 10^{+154}:\\ \;\;\;\;\frac{-a}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{\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}\;d \le 3.315223608026935 \cdot 10^{+87}:\\
\;\;\;\;\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{b \cdot c - a \cdot d}{\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 r2562172 = b;
        double r2562173 = c;
        double r2562174 = r2562172 * r2562173;
        double r2562175 = a;
        double r2562176 = d;
        double r2562177 = r2562175 * r2562176;
        double r2562178 = r2562174 - r2562177;
        double r2562179 = r2562173 * r2562173;
        double r2562180 = r2562176 * r2562176;
        double r2562181 = r2562179 + r2562180;
        double r2562182 = r2562178 / r2562181;
        return r2562182;
}

double f(double a, double b, double c, double d) {
        double r2562183 = d;
        double r2562184 = 3.315223608026935e+87;
        bool r2562185 = r2562183 <= r2562184;
        double r2562186 = b;
        double r2562187 = c;
        double r2562188 = r2562186 * r2562187;
        double r2562189 = a;
        double r2562190 = r2562189 * r2562183;
        double r2562191 = r2562188 - r2562190;
        double r2562192 = hypot(r2562183, r2562187);
        double r2562193 = r2562191 / r2562192;
        double r2562194 = 1.0;
        double r2562195 = r2562194 / r2562192;
        double r2562196 = r2562193 * r2562195;
        double r2562197 = 1.263326239685677e+154;
        bool r2562198 = r2562183 <= r2562197;
        double r2562199 = -r2562189;
        double r2562200 = r2562187 * r2562187;
        double r2562201 = fma(r2562183, r2562183, r2562200);
        double r2562202 = sqrt(r2562201);
        double r2562203 = r2562199 / r2562202;
        double r2562204 = r2562198 ? r2562203 : r2562196;
        double r2562205 = r2562185 ? r2562196 : r2562204;
        return r2562205;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.4
Target0.5
Herbie16.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 2 regimes
  2. if d < 3.315223608026935e+87 or 1.263326239685677e+154 < d

    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{b \cdot c - a \cdot d}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied clear-num25.7

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

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

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

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

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

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

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

      \[\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 3.315223608026935e+87 < d < 1.263326239685677e+154

    1. Initial program 22.5

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*22.4

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

      \[\leadsto \frac{\color{blue}{\left(b \cdot c - a \cdot d\right) \cdot \frac{1}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    8. Taylor expanded around 0 21.4

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

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

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

Reproduce

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