Average Error: 25.9 → 25.2
Time: 10.0s
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.766759969790304202832221196437063478932 \cdot 10^{296}:\\ \;\;\;\;\frac{\frac{1}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + b \cdot d}}}{\sqrt{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 3.766759969790304202832221196437063478932 \cdot 10^{296}:\\
\;\;\;\;\frac{\frac{1}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + b \cdot d}}}{\sqrt{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 r124505 = a;
        double r124506 = c;
        double r124507 = r124505 * r124506;
        double r124508 = b;
        double r124509 = d;
        double r124510 = r124508 * r124509;
        double r124511 = r124507 + r124510;
        double r124512 = r124506 * r124506;
        double r124513 = r124509 * r124509;
        double r124514 = r124512 + r124513;
        double r124515 = r124511 / r124514;
        return r124515;
}

double f(double a, double b, double c, double d) {
        double r124516 = a;
        double r124517 = c;
        double r124518 = r124516 * r124517;
        double r124519 = b;
        double r124520 = d;
        double r124521 = r124519 * r124520;
        double r124522 = r124518 + r124521;
        double r124523 = r124517 * r124517;
        double r124524 = r124520 * r124520;
        double r124525 = r124523 + r124524;
        double r124526 = r124522 / r124525;
        double r124527 = 3.766759969790304e+296;
        bool r124528 = r124526 <= r124527;
        double r124529 = 1.0;
        double r124530 = sqrt(r124525);
        double r124531 = r124530 / r124522;
        double r124532 = r124529 / r124531;
        double r124533 = r124532 / r124530;
        double r124534 = r124519 / r124530;
        double r124535 = r124528 ? r124533 : r124534;
        return r124535;
}

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.2
\[\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.766759969790304e+296

    1. Initial program 14.2

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

      \[\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*14.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. Using strategy rm
    6. Applied clear-num14.2

      \[\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}}\]

    if 3.766759969790304e+296 < (/ (+ (* 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 0 60.4

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

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

Reproduce

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