Average Error: 25.9 → 25.4
Time: 16.9s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -5.577210106748075088124921265633087256206 \cdot 10^{122}:\\ \;\;\;\;-\frac{b}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \le 1.378525602045908791188358930445114843505 \cdot 10^{88}:\\ \;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{d \cdot d + c \cdot c}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \le -5.577210106748075088124921265633087256206 \cdot 10^{122}:\\
\;\;\;\;-\frac{b}{\sqrt{d \cdot d + c \cdot c}}\\

\mathbf{elif}\;d \le 1.378525602045908791188358930445114843505 \cdot 10^{88}:\\
\;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r108735 = a;
        double r108736 = c;
        double r108737 = r108735 * r108736;
        double r108738 = b;
        double r108739 = d;
        double r108740 = r108738 * r108739;
        double r108741 = r108737 + r108740;
        double r108742 = r108736 * r108736;
        double r108743 = r108739 * r108739;
        double r108744 = r108742 + r108743;
        double r108745 = r108741 / r108744;
        return r108745;
}

double f(double a, double b, double c, double d) {
        double r108746 = d;
        double r108747 = -5.577210106748075e+122;
        bool r108748 = r108746 <= r108747;
        double r108749 = b;
        double r108750 = r108746 * r108746;
        double r108751 = c;
        double r108752 = r108751 * r108751;
        double r108753 = r108750 + r108752;
        double r108754 = sqrt(r108753);
        double r108755 = r108749 / r108754;
        double r108756 = -r108755;
        double r108757 = 1.3785256020459088e+88;
        bool r108758 = r108746 <= r108757;
        double r108759 = a;
        double r108760 = r108759 * r108751;
        double r108761 = r108749 * r108746;
        double r108762 = r108760 + r108761;
        double r108763 = r108762 / r108754;
        double r108764 = r108763 / r108754;
        double r108765 = r108758 ? r108764 : r108755;
        double r108766 = r108748 ? r108756 : r108765;
        return r108766;
}

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.9
Target0.4
Herbie25.4
\[\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 < -5.577210106748075e+122

    1. Initial program 42.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt42.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*42.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. Simplified42.5

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

      \[\leadsto \frac{\color{blue}{-1 \cdot b}}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified40.9

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

    if -5.577210106748075e+122 < d < 1.3785256020459088e+88

    1. Initial program 18.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt18.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*17.9

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

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

    if 1.3785256020459088e+88 < d

    1. Initial program 38.6

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -5.577210106748075088124921265633087256206 \cdot 10^{122}:\\ \;\;\;\;-\frac{b}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \le 1.378525602045908791188358930445114843505 \cdot 10^{88}:\\ \;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{d \cdot d + c \cdot c}}\\ \end{array}\]

Reproduce

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