Average Error: 25.1 → 25.0
Time: 15.4s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{\frac{c \cdot b - d \cdot a}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\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{c \cdot b - d \cdot a}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}
double f(double a, double b, double c, double d) {
        double r3605184 = b;
        double r3605185 = c;
        double r3605186 = r3605184 * r3605185;
        double r3605187 = a;
        double r3605188 = d;
        double r3605189 = r3605187 * r3605188;
        double r3605190 = r3605186 - r3605189;
        double r3605191 = r3605185 * r3605185;
        double r3605192 = r3605188 * r3605188;
        double r3605193 = r3605191 + r3605192;
        double r3605194 = r3605190 / r3605193;
        return r3605194;
}

double f(double a, double b, double c, double d) {
        double r3605195 = c;
        double r3605196 = b;
        double r3605197 = r3605195 * r3605196;
        double r3605198 = d;
        double r3605199 = a;
        double r3605200 = r3605198 * r3605199;
        double r3605201 = r3605197 - r3605200;
        double r3605202 = r3605195 * r3605195;
        double r3605203 = fma(r3605198, r3605198, r3605202);
        double r3605204 = sqrt(r3605203);
        double r3605205 = r3605201 / r3605204;
        double r3605206 = r3605205 / r3605204;
        return r3605206;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.1
Target0.5
Herbie25.0
\[\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. Using strategy rm
  9. Applied associate-*r/25.0

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

    \[\leadsto \frac{\frac{\color{blue}{c \cdot b - d \cdot a}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
  11. Final simplification25.0

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

Reproduce

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