Average Error: 25.6 → 16.1
Time: 35.8s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{\mathsf{fma}\left(c, a, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(c, d\right)}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{\mathsf{fma}\left(c, a, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(c, d\right)}
double f(double a, double b, double c, double d) {
        double r8345552 = a;
        double r8345553 = c;
        double r8345554 = r8345552 * r8345553;
        double r8345555 = b;
        double r8345556 = d;
        double r8345557 = r8345555 * r8345556;
        double r8345558 = r8345554 + r8345557;
        double r8345559 = r8345553 * r8345553;
        double r8345560 = r8345556 * r8345556;
        double r8345561 = r8345559 + r8345560;
        double r8345562 = r8345558 / r8345561;
        return r8345562;
}

double f(double a, double b, double c, double d) {
        double r8345563 = 1.0;
        double r8345564 = d;
        double r8345565 = c;
        double r8345566 = hypot(r8345564, r8345565);
        double r8345567 = r8345563 / r8345566;
        double r8345568 = a;
        double r8345569 = b;
        double r8345570 = r8345569 * r8345564;
        double r8345571 = fma(r8345565, r8345568, r8345570);
        double r8345572 = hypot(r8345565, r8345564);
        double r8345573 = r8345571 / r8345572;
        double r8345574 = r8345567 * r8345573;
        return r8345574;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.6
Target0.5
Herbie16.1
\[\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, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
  3. Using strategy rm
  4. Applied clear-num25.8

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

    \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}\]
  7. Applied add-sqr-sqrt25.8

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

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

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

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

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

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

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

Reproduce

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