Average Error: 25.7 → 12.6
Time: 15.7s
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 r4713114 = b;
        double r4713115 = c;
        double r4713116 = r4713114 * r4713115;
        double r4713117 = a;
        double r4713118 = d;
        double r4713119 = r4713117 * r4713118;
        double r4713120 = r4713116 - r4713119;
        double r4713121 = r4713115 * r4713115;
        double r4713122 = r4713118 * r4713118;
        double r4713123 = r4713121 + r4713122;
        double r4713124 = r4713120 / r4713123;
        return r4713124;
}

double f(double a, double b, double c, double d) {
        double r4713125 = c;
        double r4713126 = -7.729339549361261e+115;
        bool r4713127 = r4713125 <= r4713126;
        double r4713128 = b;
        double r4713129 = d;
        double r4713130 = hypot(r4713125, r4713129);
        double r4713131 = r4713128 / r4713130;
        double r4713132 = -r4713131;
        double r4713133 = 1.512622379546738e+155;
        bool r4713134 = r4713125 <= r4713133;
        double r4713135 = a;
        double r4713136 = -r4713129;
        double r4713137 = r4713135 * r4713136;
        double r4713138 = fma(r4713128, r4713125, r4713137);
        double r4713139 = r4713138 / r4713130;
        double r4713140 = r4713139 / r4713130;
        double r4713141 = r4713134 ? r4713140 : r4713131;
        double r4713142 = r4713127 ? r4713132 : r4713141;
        return r4713142;
}

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