Average Error: 25.7 → 12.6
Time: 16.5s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -7.729339549361261 \cdot 10^{+115}:\\ \;\;\;\;-\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.512622379546738 \cdot 10^{+155}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, a \cdot \left(-d\right)\right)}{\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 -7.729339549361261 \cdot 10^{+115}:\\
\;\;\;\;-\frac{b}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \le 1.512622379546738 \cdot 10^{+155}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, a \cdot \left(-d\right)\right)}{\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 r4665703 = b;
        double r4665704 = c;
        double r4665705 = r4665703 * r4665704;
        double r4665706 = a;
        double r4665707 = d;
        double r4665708 = r4665706 * r4665707;
        double r4665709 = r4665705 - r4665708;
        double r4665710 = r4665704 * r4665704;
        double r4665711 = r4665707 * r4665707;
        double r4665712 = r4665710 + r4665711;
        double r4665713 = r4665709 / r4665712;
        return r4665713;
}

double f(double a, double b, double c, double d) {
        double r4665714 = c;
        double r4665715 = -7.729339549361261e+115;
        bool r4665716 = r4665714 <= r4665715;
        double r4665717 = b;
        double r4665718 = d;
        double r4665719 = hypot(r4665714, r4665718);
        double r4665720 = r4665717 / r4665719;
        double r4665721 = -r4665720;
        double r4665722 = 1.512622379546738e+155;
        bool r4665723 = r4665714 <= r4665722;
        double r4665724 = a;
        double r4665725 = -r4665718;
        double r4665726 = r4665724 * r4665725;
        double r4665727 = fma(r4665717, r4665714, r4665726);
        double r4665728 = r4665727 / r4665719;
        double r4665729 = r4665728 / r4665719;
        double r4665730 = r4665723 ? r4665729 : r4665720;
        double r4665731 = r4665716 ? r4665721 : r4665730;
        return r4665731;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
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 < -7.729339549361261e+115

    1. Initial program 40.9

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

      \[\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 associate-/r*40.9

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def40.9

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Using strategy rm
    8. Applied hypot-def26.5

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

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

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

    if -7.729339549361261e+115 < c < 1.512622379546738e+155

    1. Initial program 18.5

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

      \[\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 associate-/r*18.4

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def18.4

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Using strategy rm
    8. Applied hypot-def11.4

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

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

    if 1.512622379546738e+155 < c

    1. Initial program 45.9

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

      \[\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 associate-/r*45.9

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def45.9

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Using strategy rm
    8. Applied hypot-def27.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -7.729339549361261 \cdot 10^{+115}:\\ \;\;\;\;-\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.512622379546738 \cdot 10^{+155}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, a \cdot \left(-d\right)\right)}{\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 2019162 +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))))