Average Error: 26.3 → 25.6
Time: 10.4s
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.125315539437018287343450480014902782958 \cdot 10^{293}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\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.125315539437018287343450480014902782958 \cdot 10^{293}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r153477 = a;
        double r153478 = c;
        double r153479 = r153477 * r153478;
        double r153480 = b;
        double r153481 = d;
        double r153482 = r153480 * r153481;
        double r153483 = r153479 + r153482;
        double r153484 = r153478 * r153478;
        double r153485 = r153481 * r153481;
        double r153486 = r153484 + r153485;
        double r153487 = r153483 / r153486;
        return r153487;
}

double f(double a, double b, double c, double d) {
        double r153488 = a;
        double r153489 = c;
        double r153490 = r153488 * r153489;
        double r153491 = b;
        double r153492 = d;
        double r153493 = r153491 * r153492;
        double r153494 = r153490 + r153493;
        double r153495 = r153489 * r153489;
        double r153496 = r153492 * r153492;
        double r153497 = r153495 + r153496;
        double r153498 = r153494 / r153497;
        double r153499 = 1.1253155394370183e+293;
        bool r153500 = r153498 <= r153499;
        double r153501 = sqrt(r153497);
        double r153502 = r153491 / r153501;
        double r153503 = r153500 ? r153498 : r153502;
        return r153503;
}

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.3
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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < 1.1253155394370183e+293

    1. Initial program 14.3

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

    if 1.1253155394370183e+293 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 63.1

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

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

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

      \[\leadsto \frac{\color{blue}{b}}{\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.125315539437018287343450480014902782958 \cdot 10^{293}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{c \cdot c + d \cdot d}}\\ \end{array}\]

Reproduce

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