Average Error: 25.7 → 25.0
Time: 20.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;\frac{b \cdot d + a \cdot c}{c \cdot c + d \cdot d} = -\infty:\\ \;\;\;\;\frac{b}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;\frac{b \cdot d + a \cdot c}{c \cdot c + d \cdot d} \le 7.764290901790722 \cdot 10^{+281}:\\ \;\;\;\;\frac{\frac{b \cdot d + a \cdot c}{\sqrt{c \cdot c + d \cdot d}}}{\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{b \cdot d + a \cdot c}{c \cdot c + d \cdot d} = -\infty:\\
\;\;\;\;\frac{b}{\sqrt{c \cdot c + d \cdot d}}\\

\mathbf{elif}\;\frac{b \cdot d + a \cdot c}{c \cdot c + d \cdot d} \le 7.764290901790722 \cdot 10^{+281}:\\
\;\;\;\;\frac{\frac{b \cdot d + a \cdot c}{\sqrt{c \cdot c + d \cdot d}}}{\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 r28698772 = a;
        double r28698773 = c;
        double r28698774 = r28698772 * r28698773;
        double r28698775 = b;
        double r28698776 = d;
        double r28698777 = r28698775 * r28698776;
        double r28698778 = r28698774 + r28698777;
        double r28698779 = r28698773 * r28698773;
        double r28698780 = r28698776 * r28698776;
        double r28698781 = r28698779 + r28698780;
        double r28698782 = r28698778 / r28698781;
        return r28698782;
}

double f(double a, double b, double c, double d) {
        double r28698783 = b;
        double r28698784 = d;
        double r28698785 = r28698783 * r28698784;
        double r28698786 = a;
        double r28698787 = c;
        double r28698788 = r28698786 * r28698787;
        double r28698789 = r28698785 + r28698788;
        double r28698790 = r28698787 * r28698787;
        double r28698791 = r28698784 * r28698784;
        double r28698792 = r28698790 + r28698791;
        double r28698793 = r28698789 / r28698792;
        double r28698794 = -inf.0;
        bool r28698795 = r28698793 <= r28698794;
        double r28698796 = sqrt(r28698792);
        double r28698797 = r28698783 / r28698796;
        double r28698798 = 7.764290901790722e+281;
        bool r28698799 = r28698793 <= r28698798;
        double r28698800 = r28698789 / r28698796;
        double r28698801 = r28698800 / r28698796;
        double r28698802 = r28698786 / r28698796;
        double r28698803 = r28698799 ? r28698801 : r28698802;
        double r28698804 = r28698795 ? r28698797 : r28698803;
        return r28698804;
}

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.7
Target0.4
Herbie25.0
\[\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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < -inf.0

    1. Initial program 60.5

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

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

    if -inf.0 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < 7.764290901790722e+281

    1. Initial program 11.1

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

    if 7.764290901790722e+281 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 61.2

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{b \cdot d + a \cdot c}{c \cdot c + d \cdot d} = -\infty:\\ \;\;\;\;\frac{b}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;\frac{b \cdot d + a \cdot c}{c \cdot c + d \cdot d} \le 7.764290901790722 \cdot 10^{+281}:\\ \;\;\;\;\frac{\frac{b \cdot d + a \cdot c}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\sqrt{c \cdot c + d \cdot d}}\\ \end{array}\]

Reproduce

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