Average Error: 25.9 → 12.7
Time: 34.4s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -5.58972528481860216307208015835708105017 \cdot 10^{116}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 4.790153388469599519369386020665188076147 \cdot 10^{114}:\\ \;\;\;\;\frac{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \frac{a \cdot d}{\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 -5.58972528481860216307208015835708105017 \cdot 10^{116}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \le 4.790153388469599519369386020665188076147 \cdot 10^{114}:\\
\;\;\;\;\frac{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \frac{a \cdot d}{\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 r5147800 = b;
        double r5147801 = c;
        double r5147802 = r5147800 * r5147801;
        double r5147803 = a;
        double r5147804 = d;
        double r5147805 = r5147803 * r5147804;
        double r5147806 = r5147802 - r5147805;
        double r5147807 = r5147801 * r5147801;
        double r5147808 = r5147804 * r5147804;
        double r5147809 = r5147807 + r5147808;
        double r5147810 = r5147806 / r5147809;
        return r5147810;
}

double f(double a, double b, double c, double d) {
        double r5147811 = c;
        double r5147812 = -5.589725284818602e+116;
        bool r5147813 = r5147811 <= r5147812;
        double r5147814 = b;
        double r5147815 = -r5147814;
        double r5147816 = d;
        double r5147817 = hypot(r5147811, r5147816);
        double r5147818 = r5147815 / r5147817;
        double r5147819 = 4.7901533884695995e+114;
        bool r5147820 = r5147811 <= r5147819;
        double r5147821 = r5147814 * r5147811;
        double r5147822 = r5147821 / r5147817;
        double r5147823 = a;
        double r5147824 = r5147823 * r5147816;
        double r5147825 = r5147824 / r5147817;
        double r5147826 = r5147822 - r5147825;
        double r5147827 = r5147826 / r5147817;
        double r5147828 = r5147814 / r5147817;
        double r5147829 = r5147820 ? r5147827 : r5147828;
        double r5147830 = r5147813 ? r5147818 : r5147829;
        return r5147830;
}

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.7
\[\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 < -5.589725284818602e+116

    1. Initial program 41.3

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

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

      \[\leadsto \frac{c \cdot b - d \cdot a}{\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.3

      \[\leadsto \color{blue}{\frac{\frac{c \cdot b - d \cdot a}{\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 clear-num41.5

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

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

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

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

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

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

    if -5.589725284818602e+116 < c < 4.7901533884695995e+114

    1. Initial program 18.6

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

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

      \[\leadsto \frac{c \cdot b - d \cdot a}{\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.5

      \[\leadsto \color{blue}{\frac{\frac{c \cdot b - d \cdot a}{\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 clear-num18.7

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

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

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

      \[\leadsto \frac{\color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\]
    12. Using strategy rm
    13. Applied div-sub11.3

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

    if 4.7901533884695995e+114 < c

    1. Initial program 40.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified40.4

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

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

      \[\leadsto \color{blue}{\frac{\frac{c \cdot b - d \cdot a}{\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 clear-num40.4

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.58972528481860216307208015835708105017 \cdot 10^{116}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 4.790153388469599519369386020665188076147 \cdot 10^{114}:\\ \;\;\;\;\frac{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \frac{a \cdot d}{\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 2019200 +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))))