Average Error: 26.2 → 12.7
Time: 27.3s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -1.1964723101605788 \cdot 10^{+201}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot b\\ \mathbf{elif}\;d \le 3.85213929759163 \cdot 10^{+150}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, d, c \cdot a\right)}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{1}{\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}\;d \le -1.1964723101605788 \cdot 10^{+201}:\\
\;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot b\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r2523509 = a;
        double r2523510 = c;
        double r2523511 = r2523509 * r2523510;
        double r2523512 = b;
        double r2523513 = d;
        double r2523514 = r2523512 * r2523513;
        double r2523515 = r2523511 + r2523514;
        double r2523516 = r2523510 * r2523510;
        double r2523517 = r2523513 * r2523513;
        double r2523518 = r2523516 + r2523517;
        double r2523519 = r2523515 / r2523518;
        return r2523519;
}

double f(double a, double b, double c, double d) {
        double r2523520 = d;
        double r2523521 = -1.1964723101605788e+201;
        bool r2523522 = r2523520 <= r2523521;
        double r2523523 = -1.0;
        double r2523524 = c;
        double r2523525 = hypot(r2523520, r2523524);
        double r2523526 = r2523523 / r2523525;
        double r2523527 = b;
        double r2523528 = r2523526 * r2523527;
        double r2523529 = 3.85213929759163e+150;
        bool r2523530 = r2523520 <= r2523529;
        double r2523531 = a;
        double r2523532 = r2523524 * r2523531;
        double r2523533 = fma(r2523527, r2523520, r2523532);
        double r2523534 = r2523533 / r2523525;
        double r2523535 = 1.0;
        double r2523536 = r2523535 / r2523525;
        double r2523537 = r2523534 * r2523536;
        double r2523538 = r2523527 * r2523536;
        double r2523539 = r2523530 ? r2523537 : r2523538;
        double r2523540 = r2523522 ? r2523528 : r2523539;
        return r2523540;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.2
Target0.4
Herbie12.7
\[\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 d < -1.1964723101605788e+201

    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 clear-num43.3

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

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

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

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

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

    if -1.1964723101605788e+201 < d < 3.85213929759163e+150

    1. Initial program 20.5

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

      \[\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-sqrt20.5

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

      \[\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 clear-num20.7

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

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

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

    if 3.85213929759163e+150 < d

    1. Initial program 45.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified45.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-sqrt45.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*45.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 clear-num45.3

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

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

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

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

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

Reproduce

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