Average Error: 25.8 → 25.8
Time: 10.9s
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 3.7234357932591811 \cdot 10^{232}:\\ \;\;\;\;\frac{\frac{1}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + d \cdot b}}}{\sqrt{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 3.7234357932591811 \cdot 10^{232}:\\
\;\;\;\;\frac{\frac{1}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + d \cdot b}}}{\sqrt{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 r59607 = a;
        double r59608 = c;
        double r59609 = r59607 * r59608;
        double r59610 = b;
        double r59611 = d;
        double r59612 = r59610 * r59611;
        double r59613 = r59609 + r59612;
        double r59614 = r59608 * r59608;
        double r59615 = r59611 * r59611;
        double r59616 = r59614 + r59615;
        double r59617 = r59613 / r59616;
        return r59617;
}

double f(double a, double b, double c, double d) {
        double r59618 = a;
        double r59619 = c;
        double r59620 = r59618 * r59619;
        double r59621 = b;
        double r59622 = d;
        double r59623 = r59621 * r59622;
        double r59624 = r59620 + r59623;
        double r59625 = r59619 * r59619;
        double r59626 = r59622 * r59622;
        double r59627 = r59625 + r59626;
        double r59628 = r59624 / r59627;
        double r59629 = 3.723435793259181e+232;
        bool r59630 = r59628 <= r59629;
        double r59631 = 1.0;
        double r59632 = sqrt(r59627);
        double r59633 = r59622 * r59621;
        double r59634 = r59620 + r59633;
        double r59635 = r59632 / r59634;
        double r59636 = r59631 / r59635;
        double r59637 = r59636 / r59632;
        double r59638 = -r59618;
        double r59639 = r59638 / r59632;
        double r59640 = r59630 ? r59637 : r59639;
        return r59640;
}

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

Original25.8
Target0.4
Herbie25.8
\[\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))) < 3.723435793259181e+232

    1. Initial program 13.9

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

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

      \[\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. Using strategy rm
    6. Applied clear-num13.8

      \[\leadsto \frac{\color{blue}{\frac{1}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + b \cdot d}}}}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified13.8

      \[\leadsto \frac{\frac{1}{\color{blue}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + d \cdot b}}}}{\sqrt{c \cdot c + d \cdot d}}\]

    if 3.723435793259181e+232 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 60.0

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

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

      \[\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 59.9

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\sqrt{c \cdot c + d \cdot d}}\]
    6. Simplified59.9

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

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

Reproduce

herbie shell --seed 2020045 
(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))))