Average Error: 25.3 → 12.5
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 -5.593465816654883 \cdot 10^{+120}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 5.890254338149143 \cdot 10^{+202}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\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 -5.593465816654883 \cdot 10^{+120}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 5.890254338149143 \cdot 10^{+202}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\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 r3697601 = a;
        double r3697602 = c;
        double r3697603 = r3697601 * r3697602;
        double r3697604 = b;
        double r3697605 = d;
        double r3697606 = r3697604 * r3697605;
        double r3697607 = r3697603 + r3697606;
        double r3697608 = r3697602 * r3697602;
        double r3697609 = r3697605 * r3697605;
        double r3697610 = r3697608 + r3697609;
        double r3697611 = r3697607 / r3697610;
        return r3697611;
}

double f(double a, double b, double c, double d) {
        double r3697612 = c;
        double r3697613 = -5.593465816654883e+120;
        bool r3697614 = r3697612 <= r3697613;
        double r3697615 = a;
        double r3697616 = -r3697615;
        double r3697617 = d;
        double r3697618 = hypot(r3697617, r3697612);
        double r3697619 = r3697616 / r3697618;
        double r3697620 = 5.890254338149143e+202;
        bool r3697621 = r3697612 <= r3697620;
        double r3697622 = b;
        double r3697623 = r3697617 * r3697622;
        double r3697624 = fma(r3697612, r3697615, r3697623);
        double r3697625 = r3697624 / r3697618;
        double r3697626 = r3697625 / r3697618;
        double r3697627 = r3697615 / r3697618;
        double r3697628 = r3697621 ? r3697626 : r3697627;
        double r3697629 = r3697614 ? r3697619 : r3697628;
        return r3697629;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.3
Target0.4
Herbie12.5
\[\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 < -5.593465816654883e+120

    1. Initial program 40.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified40.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 add-sqr-sqrt40.2

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

      \[\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-num40.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. Simplified25.6

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

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

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

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

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

    if -5.593465816654883e+120 < c < 5.890254338149143e+202

    1. Initial program 20.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified20.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-sqrt20.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*20.2

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

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

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

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

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

    if 5.890254338149143e+202 < c

    1. Initial program 41.7

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

      \[\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-sqrt41.7

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.593465816654883 \cdot 10^{+120}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 5.890254338149143 \cdot 10^{+202}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\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 2019158 +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))))