Average Error: 25.9 → 13.4
Time: 11.3s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -3.629931414478271919204238833322356117894 \cdot 10^{198}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 1.047334547019426632397659133548071084447 \cdot 10^{85}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(-a, d, 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 -3.629931414478271919204238833322356117894 \cdot 10^{198}:\\
\;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;d \le 1.047334547019426632397659133548071084447 \cdot 10^{85}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(-a, d, 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 r70357 = b;
        double r70358 = c;
        double r70359 = r70357 * r70358;
        double r70360 = a;
        double r70361 = d;
        double r70362 = r70360 * r70361;
        double r70363 = r70359 - r70362;
        double r70364 = r70358 * r70358;
        double r70365 = r70361 * r70361;
        double r70366 = r70364 + r70365;
        double r70367 = r70363 / r70366;
        return r70367;
}

double f(double a, double b, double c, double d) {
        double r70368 = d;
        double r70369 = -3.629931414478272e+198;
        bool r70370 = r70368 <= r70369;
        double r70371 = a;
        double r70372 = c;
        double r70373 = hypot(r70372, r70368);
        double r70374 = r70371 / r70373;
        double r70375 = 1.0473345470194266e+85;
        bool r70376 = r70368 <= r70375;
        double r70377 = -r70371;
        double r70378 = b;
        double r70379 = r70372 * r70378;
        double r70380 = fma(r70377, r70368, r70379);
        double r70381 = r70380 / r70373;
        double r70382 = r70381 / r70373;
        double r70383 = -r70374;
        double r70384 = r70376 ? r70382 : r70383;
        double r70385 = r70370 ? r70374 : r70384;
        return r70385;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.9
Target0.4
Herbie13.4
\[\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 < -3.629931414478272e+198

    1. Initial program 41.5

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt41.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 *-un-lft-identity41.5

      \[\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-frac41.5

      \[\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. Simplified41.5

      \[\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. Simplified31.1

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

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

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

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

    if -3.629931414478272e+198 < d < 1.0473345470194266e+85

    1. Initial program 20.6

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

      \[\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.6

      \[\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.6

      \[\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.6

      \[\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.9

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

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

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

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

    if 1.0473345470194266e+85 < d

    1. Initial program 37.7

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

      \[\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-identity37.7

      \[\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-frac37.7

      \[\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. Simplified37.7

      \[\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. Simplified25.2

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -3.629931414478271919204238833322356117894 \cdot 10^{198}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 1.047334547019426632397659133548071084447 \cdot 10^{85}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(-a, d, 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 2019195 +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))))