Average Error: 26.0 → 14.8
Time: 15.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le 7.643741764178763599768402058466858347704 \cdot 10^{138}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, d, a \cdot c\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot a\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le 7.643741764178763599768402058466858347704 \cdot 10^{138}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(b, d, a \cdot c\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r5422610 = a;
        double r5422611 = c;
        double r5422612 = r5422610 * r5422611;
        double r5422613 = b;
        double r5422614 = d;
        double r5422615 = r5422613 * r5422614;
        double r5422616 = r5422612 + r5422615;
        double r5422617 = r5422611 * r5422611;
        double r5422618 = r5422614 * r5422614;
        double r5422619 = r5422617 + r5422618;
        double r5422620 = r5422616 / r5422619;
        return r5422620;
}

double f(double a, double b, double c, double d) {
        double r5422621 = c;
        double r5422622 = 7.643741764178764e+138;
        bool r5422623 = r5422621 <= r5422622;
        double r5422624 = b;
        double r5422625 = d;
        double r5422626 = a;
        double r5422627 = r5422626 * r5422621;
        double r5422628 = fma(r5422624, r5422625, r5422627);
        double r5422629 = hypot(r5422625, r5422621);
        double r5422630 = r5422628 / r5422629;
        double r5422631 = r5422630 / r5422629;
        double r5422632 = 1.0;
        double r5422633 = r5422632 / r5422629;
        double r5422634 = r5422633 * r5422626;
        double r5422635 = r5422623 ? r5422631 : r5422634;
        return r5422635;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.4
Herbie14.8
\[\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 2 regimes
  2. if c < 7.643741764178764e+138

    1. Initial program 23.2

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

      \[\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-num23.4

      \[\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-identity23.4

      \[\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-sqrt23.4

      \[\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-frac23.4

      \[\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-cbrt23.4

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

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

      \[\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. Simplified15.1

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

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

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

    if 7.643741764178764e+138 < c

    1. Initial program 42.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified42.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 clear-num42.3

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le 7.643741764178763599768402058466858347704 \cdot 10^{138}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, d, a \cdot c\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot a\\ \end{array}\]

Reproduce

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