Average Error: 26.3 → 26.3
Time: 18.4s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le 1.937670017341036534919481795915749319496 \cdot 10^{62}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot \left(b \cdot c - d \cdot a\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \le 1.937670017341036534919481795915749319496 \cdot 10^{62}:\\
\;\;\;\;\frac{\frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot \left(b \cdot c - d \cdot a\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r5460110 = b;
        double r5460111 = c;
        double r5460112 = r5460110 * r5460111;
        double r5460113 = a;
        double r5460114 = d;
        double r5460115 = r5460113 * r5460114;
        double r5460116 = r5460112 - r5460115;
        double r5460117 = r5460111 * r5460111;
        double r5460118 = r5460114 * r5460114;
        double r5460119 = r5460117 + r5460118;
        double r5460120 = r5460116 / r5460119;
        return r5460120;
}

double f(double a, double b, double c, double d) {
        double r5460121 = d;
        double r5460122 = 1.9376700173410365e+62;
        bool r5460123 = r5460121 <= r5460122;
        double r5460124 = 1.0;
        double r5460125 = c;
        double r5460126 = r5460125 * r5460125;
        double r5460127 = fma(r5460121, r5460121, r5460126);
        double r5460128 = sqrt(r5460127);
        double r5460129 = r5460124 / r5460128;
        double r5460130 = b;
        double r5460131 = r5460130 * r5460125;
        double r5460132 = a;
        double r5460133 = r5460121 * r5460132;
        double r5460134 = r5460131 - r5460133;
        double r5460135 = r5460129 * r5460134;
        double r5460136 = r5460135 / r5460128;
        double r5460137 = -r5460132;
        double r5460138 = r5460137 / r5460128;
        double r5460139 = r5460123 ? r5460136 : r5460138;
        return r5460139;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.3
Target0.5
Herbie26.3
\[\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 2 regimes
  2. if d < 1.9376700173410365e+62

    1. Initial program 23.4

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

      \[\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-sqrt23.4

      \[\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*23.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 div-inv23.4

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

    if 1.9376700173410365e+62 < d

    1. Initial program 37.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified37.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-sqrt37.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*37.2

      \[\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. Taylor expanded around 0 37.5

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    7. Simplified37.5

      \[\leadsto \frac{\color{blue}{-a}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification26.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le 1.937670017341036534919481795915749319496 \cdot 10^{62}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot \left(b \cdot c - d \cdot a\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\ \end{array}\]

Reproduce

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