Average Error: 26.2 → 12.9
Time: 17.8s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.284532853290371 \cdot 10^{+137}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 1.1770311826568578 \cdot 10^{+166}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right) \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -1.284532853290371 \cdot 10^{+137}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 1.1770311826568578 \cdot 10^{+166}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right) \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r3143608 = a;
        double r3143609 = c;
        double r3143610 = r3143608 * r3143609;
        double r3143611 = b;
        double r3143612 = d;
        double r3143613 = r3143611 * r3143612;
        double r3143614 = r3143610 + r3143613;
        double r3143615 = r3143609 * r3143609;
        double r3143616 = r3143612 * r3143612;
        double r3143617 = r3143615 + r3143616;
        double r3143618 = r3143614 / r3143617;
        return r3143618;
}

double f(double a, double b, double c, double d) {
        double r3143619 = c;
        double r3143620 = -1.284532853290371e+137;
        bool r3143621 = r3143619 <= r3143620;
        double r3143622 = a;
        double r3143623 = -r3143622;
        double r3143624 = d;
        double r3143625 = hypot(r3143624, r3143619);
        double r3143626 = r3143623 / r3143625;
        double r3143627 = 1.1770311826568578e+166;
        bool r3143628 = r3143619 <= r3143627;
        double r3143629 = b;
        double r3143630 = r3143629 * r3143624;
        double r3143631 = fma(r3143622, r3143619, r3143630);
        double r3143632 = 1.0;
        double r3143633 = r3143632 / r3143625;
        double r3143634 = r3143631 * r3143633;
        double r3143635 = r3143634 / r3143625;
        double r3143636 = r3143622 / r3143625;
        double r3143637 = r3143628 ? r3143635 : r3143636;
        double r3143638 = r3143621 ? r3143626 : r3143637;
        return r3143638;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.2
Target0.5
Herbie12.9
\[\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. Split input into 3 regimes
  2. if c < -1.284532853290371e+137

    1. Initial program 42.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified42.9

      \[\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-sqrt42.9

      \[\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*42.9

      \[\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 fma-udef42.9

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    8. Applied hypot-def42.9

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}\]
    9. Taylor expanded around -inf 13.6

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\mathsf{hypot}\left(d, c\right)}\]
    10. Simplified13.6

      \[\leadsto \frac{\color{blue}{-a}}{\mathsf{hypot}\left(d, c\right)}\]

    if -1.284532853290371e+137 < c < 1.1770311826568578e+166

    1. Initial program 19.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified19.9

      \[\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-sqrt19.9

      \[\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*19.8

      \[\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 fma-udef19.8

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

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}\]
    9. Using strategy rm
    10. Applied clear-num19.9

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

      \[\leadsto \frac{\frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, d, c \cdot a\right)}}}}{\mathsf{hypot}\left(d, c\right)}\]
    12. Using strategy rm
    13. Applied div-inv12.9

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

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

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

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

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

    if 1.1770311826568578e+166 < c

    1. Initial program 43.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified43.3

      \[\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-sqrt43.3

      \[\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*43.3

      \[\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 fma-udef43.3

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    8. Applied hypot-def43.3

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}\]
    9. Using strategy rm
    10. Applied clear-num43.3

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

      \[\leadsto \frac{\frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, d, c \cdot a\right)}}}}{\mathsf{hypot}\left(d, c\right)}\]
    12. Taylor expanded around 0 13.5

      \[\leadsto \frac{\color{blue}{a}}{\mathsf{hypot}\left(d, c\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification12.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.284532853290371 \cdot 10^{+137}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 1.1770311826568578 \cdot 10^{+166}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right) \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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