Average Error: 25.4 → 12.6
Time: 16.4s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -2.352328066947031545696719548869417237498 \cdot 10^{129}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \le 3.537716084017609608802200540186130786223 \cdot 10^{123}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(-a, d, c \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\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}\;d \le -2.352328066947031545696719548869417237498 \cdot 10^{129}:\\
\;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r128253 = b;
        double r128254 = c;
        double r128255 = r128253 * r128254;
        double r128256 = a;
        double r128257 = d;
        double r128258 = r128256 * r128257;
        double r128259 = r128255 - r128258;
        double r128260 = r128254 * r128254;
        double r128261 = r128257 * r128257;
        double r128262 = r128260 + r128261;
        double r128263 = r128259 / r128262;
        return r128263;
}

double f(double a, double b, double c, double d) {
        double r128264 = d;
        double r128265 = -2.3523280669470315e+129;
        bool r128266 = r128264 <= r128265;
        double r128267 = a;
        double r128268 = c;
        double r128269 = hypot(r128264, r128268);
        double r128270 = r128267 / r128269;
        double r128271 = 3.5377160840176096e+123;
        bool r128272 = r128264 <= r128271;
        double r128273 = -r128267;
        double r128274 = b;
        double r128275 = r128268 * r128274;
        double r128276 = fma(r128273, r128264, r128275);
        double r128277 = r128276 / r128269;
        double r128278 = r128277 / r128269;
        double r128279 = r128273 / r128269;
        double r128280 = r128272 ? r128278 : r128279;
        double r128281 = r128266 ? r128270 : r128280;
        return r128281;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.4
Target0.5
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 d < -2.3523280669470315e+129

    1. Initial program 40.8

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

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt40.8

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity40.8

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

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(d, c\right)}}\]
    9. Using strategy rm
    10. Applied associate-*r/26.9

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

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

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

    if -2.3523280669470315e+129 < d < 3.5377160840176096e+123

    1. Initial program 18.5

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

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt18.5

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity18.5

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

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(d, c\right)}}\]
    9. Using strategy rm
    10. Applied associate-*r/11.5

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

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

    if 3.5377160840176096e+123 < d

    1. Initial program 40.8

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

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt40.8

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity40.8

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

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(d, c\right)}}\]
    9. Using strategy rm
    10. Applied associate-*r/26.3

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

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

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

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

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

Reproduce

herbie shell --seed 2019212 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, imag part"
  :precision binary64

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