Average Error: 25.1 → 25.1
Time: 16.9s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{\frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot \left(b \cdot c - a \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\frac{\frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot \left(b \cdot c - a \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}
double f(double a, double b, double c, double d) {
        double r4810638 = b;
        double r4810639 = c;
        double r4810640 = r4810638 * r4810639;
        double r4810641 = a;
        double r4810642 = d;
        double r4810643 = r4810641 * r4810642;
        double r4810644 = r4810640 - r4810643;
        double r4810645 = r4810639 * r4810639;
        double r4810646 = r4810642 * r4810642;
        double r4810647 = r4810645 + r4810646;
        double r4810648 = r4810644 / r4810647;
        return r4810648;
}

double f(double a, double b, double c, double d) {
        double r4810649 = 1.0;
        double r4810650 = d;
        double r4810651 = c;
        double r4810652 = r4810651 * r4810651;
        double r4810653 = fma(r4810650, r4810650, r4810652);
        double r4810654 = sqrt(r4810653);
        double r4810655 = r4810649 / r4810654;
        double r4810656 = b;
        double r4810657 = r4810656 * r4810651;
        double r4810658 = a;
        double r4810659 = r4810658 * r4810650;
        double r4810660 = r4810657 - r4810659;
        double r4810661 = r4810655 * r4810660;
        double r4810662 = r4810661 / r4810654;
        return r4810662;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.1
Target0.5
Herbie25.1
\[\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. Initial program 25.1

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

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

    \[\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*25.0

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

    \[\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)}}\]
  8. Final simplification25.1

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

Reproduce

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