Average Error: 25.6 → 25.5
Time: 28.5s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le 6.443252805869156 \cdot 10^{+104}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le 6.443252805869156 \cdot 10^{+104}:\\
\;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r26858403 = b;
        double r26858404 = c;
        double r26858405 = r26858403 * r26858404;
        double r26858406 = a;
        double r26858407 = d;
        double r26858408 = r26858406 * r26858407;
        double r26858409 = r26858405 - r26858408;
        double r26858410 = r26858404 * r26858404;
        double r26858411 = r26858407 * r26858407;
        double r26858412 = r26858410 + r26858411;
        double r26858413 = r26858409 / r26858412;
        return r26858413;
}

double f(double a, double b, double c, double d) {
        double r26858414 = c;
        double r26858415 = 6.443252805869156e+104;
        bool r26858416 = r26858414 <= r26858415;
        double r26858417 = b;
        double r26858418 = r26858417 * r26858414;
        double r26858419 = d;
        double r26858420 = a;
        double r26858421 = r26858419 * r26858420;
        double r26858422 = r26858418 - r26858421;
        double r26858423 = r26858414 * r26858414;
        double r26858424 = fma(r26858419, r26858419, r26858423);
        double r26858425 = sqrt(r26858424);
        double r26858426 = r26858422 / r26858425;
        double r26858427 = r26858426 / r26858425;
        double r26858428 = r26858417 / r26858425;
        double r26858429 = r26858416 ? r26858427 : r26858428;
        return r26858429;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.6
Target0.4
Herbie25.5
\[\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 c < 6.443252805869156e+104

    1. Initial program 22.4

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

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt22.4

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied *-un-lft-identity22.4

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    6. Applied times-frac22.4

      \[\leadsto \color{blue}{\frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    7. Using strategy rm
    8. Applied associate-*l/22.3

      \[\leadsto \color{blue}{\frac{1 \cdot \frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]

    if 6.443252805869156e+104 < c

    1. Initial program 41.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified41.1

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt41.1

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*41.0

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Taylor expanded around inf 40.7

      \[\leadsto \frac{\color{blue}{b}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification25.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le 6.443252805869156 \cdot 10^{+104}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\ \end{array}\]

Reproduce

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