Average Error: 25.6 → 16.1
Time: 7.3s
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, b \cdot d\right)}{\mathsf{hypot}\left(d, c\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, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}
double f(double a, double b, double c, double d) {
        double r2025218 = a;
        double r2025219 = c;
        double r2025220 = r2025218 * r2025219;
        double r2025221 = b;
        double r2025222 = d;
        double r2025223 = r2025221 * r2025222;
        double r2025224 = r2025220 + r2025223;
        double r2025225 = r2025219 * r2025219;
        double r2025226 = r2025222 * r2025222;
        double r2025227 = r2025225 + r2025226;
        double r2025228 = r2025224 / r2025227;
        return r2025228;
}

double f(double a, double b, double c, double d) {
        double r2025229 = 1.0;
        double r2025230 = d;
        double r2025231 = c;
        double r2025232 = hypot(r2025230, r2025231);
        double r2025233 = r2025229 / r2025232;
        double r2025234 = a;
        double r2025235 = b;
        double r2025236 = r2025235 * r2025230;
        double r2025237 = fma(r2025231, r2025234, r2025236);
        double r2025238 = r2025237 / r2025232;
        double r2025239 = r2025233 * r2025238;
        return r2025239;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{\frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{1}} \cdot \frac{\sqrt[3]{1}}{\frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{\mathsf{fma}\left(a, c, b \cdot d\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, c \cdot c\right)}}{\mathsf{fma}\left(a, c, b \cdot d\right)}}\]
  12. Simplified16.1

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

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

Reproduce

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