Average Error: 25.7 → 25.6
Time: 26.6s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.9064590122309793 \cdot 10^{+107}:\\ \;\;\;\;\frac{-a}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\ \mathbf{else}:\\ \;\;\;\;\frac{(a \cdot c + \left(d \cdot b\right))_* \cdot \frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -2.9064590122309793 \cdot 10^{+107}:\\
\;\;\;\;\frac{-a}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\

\mathbf{else}:\\
\;\;\;\;\frac{(a \cdot c + \left(d \cdot b\right))_* \cdot \frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r8535689 = a;
        double r8535690 = c;
        double r8535691 = r8535689 * r8535690;
        double r8535692 = b;
        double r8535693 = d;
        double r8535694 = r8535692 * r8535693;
        double r8535695 = r8535691 + r8535694;
        double r8535696 = r8535690 * r8535690;
        double r8535697 = r8535693 * r8535693;
        double r8535698 = r8535696 + r8535697;
        double r8535699 = r8535695 / r8535698;
        return r8535699;
}

double f(double a, double b, double c, double d) {
        double r8535700 = c;
        double r8535701 = -2.9064590122309793e+107;
        bool r8535702 = r8535700 <= r8535701;
        double r8535703 = a;
        double r8535704 = -r8535703;
        double r8535705 = d;
        double r8535706 = r8535700 * r8535700;
        double r8535707 = fma(r8535705, r8535705, r8535706);
        double r8535708 = sqrt(r8535707);
        double r8535709 = r8535704 / r8535708;
        double r8535710 = b;
        double r8535711 = r8535705 * r8535710;
        double r8535712 = fma(r8535703, r8535700, r8535711);
        double r8535713 = 1.0;
        double r8535714 = r8535713 / r8535708;
        double r8535715 = r8535712 * r8535714;
        double r8535716 = r8535715 / r8535708;
        double r8535717 = r8535702 ? r8535709 : r8535716;
        return r8535717;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
Target0.5
Herbie25.6
\[\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 < -2.9064590122309793e+107

    1. Initial program 41.1

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

      \[\leadsto \color{blue}{\frac{(a \cdot c + \left(b \cdot d\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt41.1

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*41.1

      \[\leadsto \color{blue}{\frac{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Taylor expanded around -inf 40.4

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    7. Simplified40.4

      \[\leadsto \frac{\color{blue}{-a}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]

    if -2.9064590122309793e+107 < c

    1. Initial program 22.6

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

      \[\leadsto \color{blue}{\frac{(a \cdot c + \left(b \cdot d\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt22.6

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*22.5

      \[\leadsto \color{blue}{\frac{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Using strategy rm
    7. Applied div-inv22.6

      \[\leadsto \frac{\color{blue}{(a \cdot c + \left(b \cdot d\right))_* \cdot \frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification25.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -2.9064590122309793 \cdot 10^{+107}:\\ \;\;\;\;\frac{-a}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\ \mathbf{else}:\\ \;\;\;\;\frac{(a \cdot c + \left(d \cdot b\right))_* \cdot \frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\\ \end{array}\]

Reproduce

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