Average Error: 26.4 → 25.7
Time: 3.5s
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.29064580259523516 \cdot 10^{293}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-1 \cdot 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{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 3.29064580259523516 \cdot 10^{293}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r115953 = a;
        double r115954 = c;
        double r115955 = r115953 * r115954;
        double r115956 = b;
        double r115957 = d;
        double r115958 = r115956 * r115957;
        double r115959 = r115955 + r115958;
        double r115960 = r115954 * r115954;
        double r115961 = r115957 * r115957;
        double r115962 = r115960 + r115961;
        double r115963 = r115959 / r115962;
        return r115963;
}

double f(double a, double b, double c, double d) {
        double r115964 = a;
        double r115965 = c;
        double r115966 = r115964 * r115965;
        double r115967 = b;
        double r115968 = d;
        double r115969 = r115967 * r115968;
        double r115970 = r115966 + r115969;
        double r115971 = r115965 * r115965;
        double r115972 = r115968 * r115968;
        double r115973 = r115971 + r115972;
        double r115974 = r115970 / r115973;
        double r115975 = 3.290645802595235e+293;
        bool r115976 = r115974 <= r115975;
        double r115977 = sqrt(r115973);
        double r115978 = r115977 * r115977;
        double r115979 = r115970 / r115978;
        double r115980 = -1.0;
        double r115981 = r115980 * r115964;
        double r115982 = r115981 / r115977;
        double r115983 = r115976 ? r115979 : r115982;
        return r115983;
}

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.4
Target0.5
Herbie25.7
\[\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.290645802595235e+293

    1. Initial program 14.4

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

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

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

    1. Initial program 63.2

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

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

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

Reproduce

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