Average Error: 25.9 → 12.9
Time: 16.7s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.47788539242579584930401717666761308705 \cdot 10^{104}:\\ \;\;\;\;-\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 1.336053818650175824019993309646752305462 \cdot 10^{147}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\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 -1.47788539242579584930401717666761308705 \cdot 10^{104}:\\
\;\;\;\;-\frac{b}{\mathsf{hypot}\left(d, c\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r5295990 = b;
        double r5295991 = c;
        double r5295992 = r5295990 * r5295991;
        double r5295993 = a;
        double r5295994 = d;
        double r5295995 = r5295993 * r5295994;
        double r5295996 = r5295992 - r5295995;
        double r5295997 = r5295991 * r5295991;
        double r5295998 = r5295994 * r5295994;
        double r5295999 = r5295997 + r5295998;
        double r5296000 = r5295996 / r5295999;
        return r5296000;
}

double f(double a, double b, double c, double d) {
        double r5296001 = c;
        double r5296002 = -1.477885392425796e+104;
        bool r5296003 = r5296001 <= r5296002;
        double r5296004 = b;
        double r5296005 = d;
        double r5296006 = hypot(r5296005, r5296001);
        double r5296007 = r5296004 / r5296006;
        double r5296008 = -r5296007;
        double r5296009 = 1.3360538186501758e+147;
        bool r5296010 = r5296001 <= r5296009;
        double r5296011 = r5296004 * r5296001;
        double r5296012 = a;
        double r5296013 = r5296005 * r5296012;
        double r5296014 = r5296011 - r5296013;
        double r5296015 = r5296014 / r5296006;
        double r5296016 = r5296015 / r5296006;
        double r5296017 = r5296010 ? r5296016 : r5296007;
        double r5296018 = r5296003 ? r5296008 : r5296017;
        return r5296018;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original25.9
Target0.4
Herbie12.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. Split input into 3 regimes
  2. if c < -1.477885392425796e+104

    1. Initial program 39.8

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

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

      \[\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*39.7

      \[\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. Using strategy rm
    7. Applied fma-udef39.7

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

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef39.7

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def27.0

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

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

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

    if -1.477885392425796e+104 < c < 1.3360538186501758e+147

    1. Initial program 19.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified19.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-sqrt19.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*18.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. Using strategy rm
    7. Applied fma-udef18.9

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

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

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

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

    if 1.3360538186501758e+147 < c

    1. Initial program 42.8

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

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

      \[\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*42.8

      \[\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. Using strategy rm
    7. Applied fma-udef42.8

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

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef42.8

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def27.8

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.47788539242579584930401717666761308705 \cdot 10^{104}:\\ \;\;\;\;-\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 1.336053818650175824019993309646752305462 \cdot 10^{147}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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