Average Error: 25.8 → 26.3
Time: 12.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le 28148546215012511376658377374508777472:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot d + c \cdot a}{\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}\;d \le 28148546215012511376658377374508777472:\\
\;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot d + c \cdot a}{\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 r5962949 = a;
        double r5962950 = c;
        double r5962951 = r5962949 * r5962950;
        double r5962952 = b;
        double r5962953 = d;
        double r5962954 = r5962952 * r5962953;
        double r5962955 = r5962951 + r5962954;
        double r5962956 = r5962950 * r5962950;
        double r5962957 = r5962953 * r5962953;
        double r5962958 = r5962956 + r5962957;
        double r5962959 = r5962955 / r5962958;
        return r5962959;
}

double f(double a, double b, double c, double d) {
        double r5962960 = d;
        double r5962961 = 2.814854621501251e+37;
        bool r5962962 = r5962960 <= r5962961;
        double r5962963 = 1.0;
        double r5962964 = c;
        double r5962965 = r5962964 * r5962964;
        double r5962966 = r5962960 * r5962960;
        double r5962967 = r5962965 + r5962966;
        double r5962968 = sqrt(r5962967);
        double r5962969 = r5962963 / r5962968;
        double r5962970 = b;
        double r5962971 = r5962970 * r5962960;
        double r5962972 = a;
        double r5962973 = r5962964 * r5962972;
        double r5962974 = r5962971 + r5962973;
        double r5962975 = r5962974 / r5962968;
        double r5962976 = r5962969 * r5962975;
        double r5962977 = r5962970 / r5962968;
        double r5962978 = r5962962 ? r5962976 : r5962977;
        return r5962978;
}

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.8
Target0.4
Herbie26.3
\[\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 d < 2.814854621501251e+37

    1. Initial program 22.8

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

      \[\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 *-un-lft-identity22.8

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac22.8

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

    if 2.814854621501251e+37 < d

    1. Initial program 35.3

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

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

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

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

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

Reproduce

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