Average Error: 26.4 → 12.8
Time: 13.7s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -2.556682598034695703330250466492269315027 \cdot 10^{120}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 5.654413025831701701732390598974065197851 \cdot 10^{178}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(-d, a, c \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\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}\;d \le -2.556682598034695703330250466492269315027 \cdot 10^{120}:\\
\;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r78737 = b;
        double r78738 = c;
        double r78739 = r78737 * r78738;
        double r78740 = a;
        double r78741 = d;
        double r78742 = r78740 * r78741;
        double r78743 = r78739 - r78742;
        double r78744 = r78738 * r78738;
        double r78745 = r78741 * r78741;
        double r78746 = r78744 + r78745;
        double r78747 = r78743 / r78746;
        return r78747;
}

double f(double a, double b, double c, double d) {
        double r78748 = d;
        double r78749 = -2.5566825980346957e+120;
        bool r78750 = r78748 <= r78749;
        double r78751 = a;
        double r78752 = c;
        double r78753 = hypot(r78752, r78748);
        double r78754 = r78751 / r78753;
        double r78755 = 5.654413025831702e+178;
        bool r78756 = r78748 <= r78755;
        double r78757 = -r78748;
        double r78758 = b;
        double r78759 = r78752 * r78758;
        double r78760 = fma(r78757, r78751, r78759);
        double r78761 = r78760 / r78753;
        double r78762 = r78761 / r78753;
        double r78763 = -r78751;
        double r78764 = r78763 / r78753;
        double r78765 = r78756 ? r78762 : r78764;
        double r78766 = r78750 ? r78754 : r78765;
        return r78766;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.4
Target0.5
Herbie12.8
\[\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 d < -2.5566825980346957e+120

    1. Initial program 40.8

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt40.8

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity40.8

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac40.8

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified40.8

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(-a, d, b \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity27.0

      \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}\right)} \cdot \frac{\mathsf{fma}\left(-a, d, b \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}\]
    10. Applied associate-*l*27.0

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

      \[\leadsto 1 \cdot \color{blue}{\frac{\frac{\mathsf{fma}\left(-d, a, b \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}}\]
    12. Using strategy rm
    13. Applied *-un-lft-identity27.0

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

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

    if -2.5566825980346957e+120 < d < 5.654413025831702e+178

    1. Initial program 20.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt20.1

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity20.1

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac20.1

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified20.1

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(-a, d, b \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity12.3

      \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}\right)} \cdot \frac{\mathsf{fma}\left(-a, d, b \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}\]
    10. Applied associate-*l*12.3

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

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

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

    if 5.654413025831702e+178 < d

    1. Initial program 45.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt45.3

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity45.3

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac45.3

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified45.3

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(-a, d, b \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity32.6

      \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}\right)} \cdot \frac{\mathsf{fma}\left(-a, d, b \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}\]
    10. Applied associate-*l*32.6

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

      \[\leadsto 1 \cdot \color{blue}{\frac{\frac{\mathsf{fma}\left(-d, a, b \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}}\]
    12. Using strategy rm
    13. Applied *-un-lft-identity32.6

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -2.556682598034695703330250466492269315027 \cdot 10^{120}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 5.654413025831701701732390598974065197851 \cdot 10^{178}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(-d, a, c \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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