Average Error: 26.0 → 25.4
Time: 4.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.37555360408531301 \cdot 10^{294}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{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{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 3.37555360408531301 \cdot 10^{294}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{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 r101672 = a;
        double r101673 = c;
        double r101674 = r101672 * r101673;
        double r101675 = b;
        double r101676 = d;
        double r101677 = r101675 * r101676;
        double r101678 = r101674 + r101677;
        double r101679 = r101673 * r101673;
        double r101680 = r101676 * r101676;
        double r101681 = r101679 + r101680;
        double r101682 = r101678 / r101681;
        return r101682;
}

double f(double a, double b, double c, double d) {
        double r101683 = a;
        double r101684 = c;
        double r101685 = r101683 * r101684;
        double r101686 = b;
        double r101687 = d;
        double r101688 = r101686 * r101687;
        double r101689 = r101685 + r101688;
        double r101690 = r101684 * r101684;
        double r101691 = r101687 * r101687;
        double r101692 = r101690 + r101691;
        double r101693 = r101689 / r101692;
        double r101694 = 3.375553604085313e+294;
        bool r101695 = r101693 <= r101694;
        double r101696 = sqrt(r101692);
        double r101697 = r101683 / r101696;
        double r101698 = r101695 ? r101693 : r101697;
        return r101698;
}

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.0
Target0.4
Herbie25.4
\[\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.375553604085313e+294

    1. Initial program 14.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]

    if 3.375553604085313e+294 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 63.1

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

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

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

Reproduce

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