Average Error: 26.5 → 25.6
Time: 3.3s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 1.80145861097624246 \cdot 10^{302}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\sqrt{c \cdot c + d \cdot d}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 1.80145861097624246 \cdot 10^{302}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{\sqrt{c \cdot c + d \cdot d}}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r101731 = a;
        double r101732 = c;
        double r101733 = r101731 * r101732;
        double r101734 = b;
        double r101735 = d;
        double r101736 = r101734 * r101735;
        double r101737 = r101733 + r101736;
        double r101738 = r101732 * r101732;
        double r101739 = r101735 * r101735;
        double r101740 = r101738 + r101739;
        double r101741 = r101737 / r101740;
        return r101741;
}

double f(double a, double b, double c, double d) {
        double r101742 = a;
        double r101743 = c;
        double r101744 = r101742 * r101743;
        double r101745 = b;
        double r101746 = d;
        double r101747 = r101745 * r101746;
        double r101748 = r101744 + r101747;
        double r101749 = r101743 * r101743;
        double r101750 = r101746 * r101746;
        double r101751 = r101749 + r101750;
        double r101752 = r101748 / r101751;
        double r101753 = 1.8014586109762425e+302;
        bool r101754 = r101752 <= r101753;
        double r101755 = sqrt(r101751);
        double r101756 = r101742 / r101755;
        double r101757 = r101754 ? r101752 : r101756;
        return r101757;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.5
Target0.4
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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < 1.8014586109762425e+302

    1. Initial program 14.0

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

    if 1.8014586109762425e+302 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 63.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt63.5

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied associate-/r*63.5

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Taylor expanded around inf 60.0

      \[\leadsto \frac{\color{blue}{a}}{\sqrt{c \cdot c + d \cdot d}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification25.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 1.80145861097624246 \cdot 10^{302}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\sqrt{c \cdot c + d \cdot d}}\\ \end{array}\]

Reproduce

herbie shell --seed 2020018 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :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))))