Average Error: 26.3 → 27.2
Time: 4.0s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;b \le -1.83333069166780128 \cdot 10^{209}:\\ \;\;\;\;\frac{b}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{1}{\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}\;b \le -1.83333069166780128 \cdot 10^{209}:\\
\;\;\;\;\frac{b}{\sqrt{c \cdot c + d \cdot d}}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r122417 = a;
        double r122418 = c;
        double r122419 = r122417 * r122418;
        double r122420 = b;
        double r122421 = d;
        double r122422 = r122420 * r122421;
        double r122423 = r122419 + r122422;
        double r122424 = r122418 * r122418;
        double r122425 = r122421 * r122421;
        double r122426 = r122424 + r122425;
        double r122427 = r122423 / r122426;
        return r122427;
}

double f(double a, double b, double c, double d) {
        double r122428 = b;
        double r122429 = -1.8333306916678013e+209;
        bool r122430 = r122428 <= r122429;
        double r122431 = c;
        double r122432 = r122431 * r122431;
        double r122433 = d;
        double r122434 = r122433 * r122433;
        double r122435 = r122432 + r122434;
        double r122436 = sqrt(r122435);
        double r122437 = r122428 / r122436;
        double r122438 = a;
        double r122439 = r122438 * r122431;
        double r122440 = r122428 * r122433;
        double r122441 = r122439 + r122440;
        double r122442 = r122441 / r122436;
        double r122443 = 1.0;
        double r122444 = r122443 / r122436;
        double r122445 = r122442 * r122444;
        double r122446 = r122430 ? r122437 : r122445;
        return r122446;
}

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
Herbie27.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 b < -1.8333306916678013e+209

    1. Initial program 40.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt40.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*40.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. Taylor expanded around 0 52.0

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

    if -1.8333306916678013e+209 < b

    1. Initial program 25.2

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

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

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

Reproduce

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