Average Error: 25.6 → 16.3
Time: 18.9s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\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(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}
double f(double a, double b, double c, double d) {
        double r5029375 = a;
        double r5029376 = c;
        double r5029377 = r5029375 * r5029376;
        double r5029378 = b;
        double r5029379 = d;
        double r5029380 = r5029378 * r5029379;
        double r5029381 = r5029377 + r5029380;
        double r5029382 = r5029376 * r5029376;
        double r5029383 = r5029379 * r5029379;
        double r5029384 = r5029382 + r5029383;
        double r5029385 = r5029381 / r5029384;
        return r5029385;
}

double f(double a, double b, double c, double d) {
        double r5029386 = a;
        double r5029387 = c;
        double r5029388 = b;
        double r5029389 = d;
        double r5029390 = r5029388 * r5029389;
        double r5029391 = fma(r5029386, r5029387, r5029390);
        double r5029392 = hypot(r5029389, r5029387);
        double r5029393 = r5029391 / r5029392;
        double r5029394 = r5029393 / r5029392;
        return r5029394;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.6
Target0.4
Herbie16.3
\[\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 associate-/r*25.5

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

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

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

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

    \[\leadsto \frac{\frac{\color{blue}{\mathsf{fma}\left(b, d, c \cdot a\right)}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
  11. Using strategy rm
  12. Applied clear-num25.7

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

    \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(c, a, b \cdot d\right)} \cdot \mathsf{hypot}\left(d, c\right)}}\]
  14. Using strategy rm
  15. Applied associate-/r*16.4

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

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

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

Reproduce

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