Average Error: 26.1 → 13.3
Time: 3.7s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.8763806766248217 \cdot 10^{136}:\\ \;\;\;\;1 \cdot \frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 6.60217994199644213 \cdot 10^{202}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \frac{\mathsf{fma}\left(b, c, -a \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \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 -2.8763806766248217 \cdot 10^{136}:\\
\;\;\;\;1 \cdot \frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\

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

\mathbf{else}:\\
\;\;\;\;1 \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r107367 = b;
        double r107368 = c;
        double r107369 = r107367 * r107368;
        double r107370 = a;
        double r107371 = d;
        double r107372 = r107370 * r107371;
        double r107373 = r107369 - r107372;
        double r107374 = r107368 * r107368;
        double r107375 = r107371 * r107371;
        double r107376 = r107374 + r107375;
        double r107377 = r107373 / r107376;
        return r107377;
}

double f(double a, double b, double c, double d) {
        double r107378 = c;
        double r107379 = -2.8763806766248217e+136;
        bool r107380 = r107378 <= r107379;
        double r107381 = 1.0;
        double r107382 = -1.0;
        double r107383 = b;
        double r107384 = r107382 * r107383;
        double r107385 = d;
        double r107386 = hypot(r107378, r107385);
        double r107387 = r107384 / r107386;
        double r107388 = r107381 * r107387;
        double r107389 = 6.602179941996442e+202;
        bool r107390 = r107378 <= r107389;
        double r107391 = r107386 * r107381;
        double r107392 = r107381 / r107391;
        double r107393 = a;
        double r107394 = r107393 * r107385;
        double r107395 = -r107394;
        double r107396 = fma(r107383, r107378, r107395);
        double r107397 = r107396 / r107386;
        double r107398 = r107392 * r107397;
        double r107399 = r107383 / r107386;
        double r107400 = r107381 * r107399;
        double r107401 = r107390 ? r107398 : r107400;
        double r107402 = r107380 ? r107388 : r107401;
        return r107402;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.5
Herbie13.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 3 regimes
  2. if c < -2.8763806766248217e+136

    1. Initial program 41.7

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity27.3

      \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1}\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\]
    10. Applied associate-*l*27.3

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

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

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

    if -2.8763806766248217e+136 < c < 6.602179941996442e+202

    1. Initial program 21.3

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

      \[\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-identity21.3

      \[\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-frac21.3

      \[\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. Simplified21.3

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

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

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

    if 6.602179941996442e+202 < c

    1. Initial program 41.0

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

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

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

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity29.3

      \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1}\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\]
    10. Applied associate-*l*29.3

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -2.8763806766248217 \cdot 10^{136}:\\ \;\;\;\;1 \cdot \frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 6.60217994199644213 \cdot 10^{202}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \frac{\mathsf{fma}\left(b, c, -a \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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