Average Error: 26.2 → 13.1
Time: 17.1s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -4.012444222764070938318713205906098803793 \cdot 10^{97}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 1.124934690533360950950546020938866421399 \cdot 10^{193}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{b \cdot c - a \cdot d}}}{\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 -4.012444222764070938318713205906098803793 \cdot 10^{97}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 1.124934690533360950950546020938866421399 \cdot 10^{193}:\\
\;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{b \cdot c - a \cdot d}}}{\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 r8046898 = b;
        double r8046899 = c;
        double r8046900 = r8046898 * r8046899;
        double r8046901 = a;
        double r8046902 = d;
        double r8046903 = r8046901 * r8046902;
        double r8046904 = r8046900 - r8046903;
        double r8046905 = r8046899 * r8046899;
        double r8046906 = r8046902 * r8046902;
        double r8046907 = r8046905 + r8046906;
        double r8046908 = r8046904 / r8046907;
        return r8046908;
}

double f(double a, double b, double c, double d) {
        double r8046909 = c;
        double r8046910 = -4.012444222764071e+97;
        bool r8046911 = r8046909 <= r8046910;
        double r8046912 = b;
        double r8046913 = -r8046912;
        double r8046914 = d;
        double r8046915 = hypot(r8046914, r8046909);
        double r8046916 = r8046913 / r8046915;
        double r8046917 = 1.124934690533361e+193;
        bool r8046918 = r8046909 <= r8046917;
        double r8046919 = 1.0;
        double r8046920 = r8046912 * r8046909;
        double r8046921 = a;
        double r8046922 = r8046921 * r8046914;
        double r8046923 = r8046920 - r8046922;
        double r8046924 = r8046915 / r8046923;
        double r8046925 = r8046919 / r8046924;
        double r8046926 = r8046925 / r8046915;
        double r8046927 = r8046912 / r8046915;
        double r8046928 = r8046918 ? r8046926 : r8046927;
        double r8046929 = r8046911 ? r8046916 : r8046928;
        return r8046929;
}

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

Original26.2
Target0.4
Herbie13.1
\[\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 < -4.012444222764071e+97

    1. Initial program 39.6

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

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

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

      \[\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 *-un-lft-identity39.6

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

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

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

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

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

      \[\leadsto \left(c \cdot b - d \cdot a\right) \cdot \color{blue}{\frac{\frac{1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}}\]
    13. Using strategy rm
    14. Applied associate-*r/27.6

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

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

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

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

    if -4.012444222764071e+97 < c < 1.124934690533361e+193

    1. Initial program 20.8

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified20.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-sqrt20.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*20.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 *-un-lft-identity20.7

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

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

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

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

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

      \[\leadsto \left(c \cdot b - d \cdot a\right) \cdot \color{blue}{\frac{\frac{1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}}\]
    13. Using strategy rm
    14. Applied associate-*r/12.7

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

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

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

    if 1.124934690533361e+193 < c

    1. Initial program 41.6

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

      \[\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-sqrt41.6

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

      \[\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 *-un-lft-identity41.6

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

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

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

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

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

      \[\leadsto \left(c \cdot b - d \cdot a\right) \cdot \color{blue}{\frac{\frac{1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}}\]
    13. Using strategy rm
    14. Applied associate-*r/30.7

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -4.012444222764070938318713205906098803793 \cdot 10^{97}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 1.124934690533360950950546020938866421399 \cdot 10^{193}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{b \cdot c - a \cdot d}}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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