Average Error: 25.9 → 12.7
Time: 29.3s
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 r5439330 = b;
        double r5439331 = c;
        double r5439332 = r5439330 * r5439331;
        double r5439333 = a;
        double r5439334 = d;
        double r5439335 = r5439333 * r5439334;
        double r5439336 = r5439332 - r5439335;
        double r5439337 = r5439331 * r5439331;
        double r5439338 = r5439334 * r5439334;
        double r5439339 = r5439337 + r5439338;
        double r5439340 = r5439336 / r5439339;
        return r5439340;
}

double f(double a, double b, double c, double d) {
        double r5439341 = c;
        double r5439342 = -5.589725284818602e+116;
        bool r5439343 = r5439341 <= r5439342;
        double r5439344 = b;
        double r5439345 = -r5439344;
        double r5439346 = d;
        double r5439347 = hypot(r5439341, r5439346);
        double r5439348 = r5439345 / r5439347;
        double r5439349 = 4.7901533884695995e+114;
        bool r5439350 = r5439341 <= r5439349;
        double r5439351 = r5439344 * r5439341;
        double r5439352 = r5439351 / r5439347;
        double r5439353 = a;
        double r5439354 = r5439353 * r5439346;
        double r5439355 = r5439354 / r5439347;
        double r5439356 = r5439352 - r5439355;
        double r5439357 = r5439356 / r5439347;
        double r5439358 = r5439344 / r5439347;
        double r5439359 = r5439350 ? r5439357 : r5439358;
        double r5439360 = r5439343 ? r5439348 : r5439359;
        return r5439360;
}

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))))