Average Error: 26.1 → 12.6
Time: 31.6s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -4.78970221325173028480304050242606063264 \cdot 10^{151}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 7.209078410675664983392874709740943245737 \cdot 10^{132}:\\ \;\;\;\;\frac{\frac{b \cdot c}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)} - \frac{\frac{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 -4.78970221325173028480304050242606063264 \cdot 10^{151}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 7.209078410675664983392874709740943245737 \cdot 10^{132}:\\
\;\;\;\;\frac{\frac{b \cdot c}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)} - \frac{\frac{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 r5188663 = b;
        double r5188664 = c;
        double r5188665 = r5188663 * r5188664;
        double r5188666 = a;
        double r5188667 = d;
        double r5188668 = r5188666 * r5188667;
        double r5188669 = r5188665 - r5188668;
        double r5188670 = r5188664 * r5188664;
        double r5188671 = r5188667 * r5188667;
        double r5188672 = r5188670 + r5188671;
        double r5188673 = r5188669 / r5188672;
        return r5188673;
}

double f(double a, double b, double c, double d) {
        double r5188674 = c;
        double r5188675 = -4.78970221325173e+151;
        bool r5188676 = r5188674 <= r5188675;
        double r5188677 = b;
        double r5188678 = -r5188677;
        double r5188679 = d;
        double r5188680 = hypot(r5188679, r5188674);
        double r5188681 = r5188678 / r5188680;
        double r5188682 = 7.209078410675665e+132;
        bool r5188683 = r5188674 <= r5188682;
        double r5188684 = r5188677 * r5188674;
        double r5188685 = r5188684 / r5188680;
        double r5188686 = r5188685 / r5188680;
        double r5188687 = a;
        double r5188688 = r5188679 * r5188687;
        double r5188689 = r5188688 / r5188680;
        double r5188690 = r5188689 / r5188680;
        double r5188691 = r5188686 - r5188690;
        double r5188692 = r5188677 / r5188680;
        double r5188693 = r5188683 ? r5188691 : r5188692;
        double r5188694 = r5188676 ? r5188681 : r5188693;
        return r5188694;
}

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.1
Target0.4
Herbie12.6
\[\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.78970221325173e+151

    1. Initial program 44.6

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified44.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-sqrt44.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*44.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 fma-udef44.6

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

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

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

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

    if -4.78970221325173e+151 < c < 7.209078410675665e+132

    1. Initial program 19.2

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

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

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

      \[\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-udef19.1

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

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

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

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

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

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

    if 7.209078410675665e+132 < c

    1. Initial program 42.3

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -4.78970221325173028480304050242606063264 \cdot 10^{151}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 7.209078410675664983392874709740943245737 \cdot 10^{132}:\\ \;\;\;\;\frac{\frac{b \cdot c}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)} - \frac{\frac{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 2019168 +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))))