Average Error: 25.9 → 13.0
Time: 14.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -3.4568057680652865 \cdot 10^{+145}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 4.1067418235703216 \cdot 10^{+95}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\right)}{\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 -3.4568057680652865 \cdot 10^{+145}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 4.1067418235703216 \cdot 10^{+95}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\right)}{\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 r2927560 = a;
        double r2927561 = c;
        double r2927562 = r2927560 * r2927561;
        double r2927563 = b;
        double r2927564 = d;
        double r2927565 = r2927563 * r2927564;
        double r2927566 = r2927562 + r2927565;
        double r2927567 = r2927561 * r2927561;
        double r2927568 = r2927564 * r2927564;
        double r2927569 = r2927567 + r2927568;
        double r2927570 = r2927566 / r2927569;
        return r2927570;
}

double f(double a, double b, double c, double d) {
        double r2927571 = c;
        double r2927572 = -3.4568057680652865e+145;
        bool r2927573 = r2927571 <= r2927572;
        double r2927574 = a;
        double r2927575 = -r2927574;
        double r2927576 = d;
        double r2927577 = hypot(r2927576, r2927571);
        double r2927578 = r2927575 / r2927577;
        double r2927579 = 4.1067418235703216e+95;
        bool r2927580 = r2927571 <= r2927579;
        double r2927581 = b;
        double r2927582 = r2927576 * r2927581;
        double r2927583 = fma(r2927574, r2927571, r2927582);
        double r2927584 = r2927583 / r2927577;
        double r2927585 = r2927584 / r2927577;
        double r2927586 = r2927574 / r2927577;
        double r2927587 = r2927580 ? r2927585 : r2927586;
        double r2927588 = r2927573 ? r2927578 : r2927587;
        return r2927588;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.9
Target0.4
Herbie13.0
\[\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 < -3.4568057680652865e+145

    1. Initial program 43.5

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

      \[\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 add-sqr-sqrt43.5

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\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)}}}\]
    5. Applied associate-/r*43.5

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\mathsf{hypot}\left(d, c\right)}\]
    13. Simplified14.0

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

    if -3.4568057680652865e+145 < c < 4.1067418235703216e+95

    1. Initial program 18.7

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

      \[\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 add-sqr-sqrt18.7

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\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)}}}\]
    5. Applied associate-/r*18.6

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

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

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

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

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

    if 4.1067418235703216e+95 < c

    1. Initial program 39.0

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

      \[\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 add-sqr-sqrt39.0

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\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)}}}\]
    5. Applied associate-/r*39.0

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -3.4568057680652865 \cdot 10^{+145}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 4.1067418235703216 \cdot 10^{+95}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\right)}{\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 2019129 +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))))