Average Error: 25.6 → 16.2
Time: 10.3s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}
double f(double a, double b, double c, double d) {
        double r1935523 = a;
        double r1935524 = c;
        double r1935525 = r1935523 * r1935524;
        double r1935526 = b;
        double r1935527 = d;
        double r1935528 = r1935526 * r1935527;
        double r1935529 = r1935525 + r1935528;
        double r1935530 = r1935524 * r1935524;
        double r1935531 = r1935527 * r1935527;
        double r1935532 = r1935530 + r1935531;
        double r1935533 = r1935529 / r1935532;
        return r1935533;
}

double f(double a, double b, double c, double d) {
        double r1935534 = c;
        double r1935535 = a;
        double r1935536 = d;
        double r1935537 = b;
        double r1935538 = r1935536 * r1935537;
        double r1935539 = fma(r1935534, r1935535, r1935538);
        double r1935540 = hypot(r1935536, r1935534);
        double r1935541 = r1935539 / r1935540;
        double r1935542 = r1935541 / r1935540;
        return r1935542;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.6
Target0.4
Herbie16.2
\[\begin{array}{l} \mathbf{if}\;\left|d\right| \lt \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array}\]

Derivation

  1. Initial program 25.6

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

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
  3. Using strategy rm
  4. Applied add-sqr-sqrt25.6

    \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\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 *-un-lft-identity25.6

    \[\leadsto \frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
  6. Applied times-frac25.6

    \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
  7. Using strategy rm
  8. Applied *-un-lft-identity25.6

    \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\right)} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
  9. Applied associate-*l*25.6

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

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

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

Reproduce

herbie shell --seed 2019154 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, real part"

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))

  (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))