Average Error: 26.2 → 25.4
Time: 4.3s
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 7.355127360692946109502946832371947729711 \cdot 10^{283}:\\ \;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\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 7.355127360692946109502946832371947729711 \cdot 10^{283}:\\
\;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\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 r106110 = a;
        double r106111 = c;
        double r106112 = r106110 * r106111;
        double r106113 = b;
        double r106114 = d;
        double r106115 = r106113 * r106114;
        double r106116 = r106112 + r106115;
        double r106117 = r106111 * r106111;
        double r106118 = r106114 * r106114;
        double r106119 = r106117 + r106118;
        double r106120 = r106116 / r106119;
        return r106120;
}

double f(double a, double b, double c, double d) {
        double r106121 = a;
        double r106122 = c;
        double r106123 = r106121 * r106122;
        double r106124 = b;
        double r106125 = d;
        double r106126 = r106124 * r106125;
        double r106127 = r106123 + r106126;
        double r106128 = r106122 * r106122;
        double r106129 = r106125 * r106125;
        double r106130 = r106128 + r106129;
        double r106131 = r106127 / r106130;
        double r106132 = 7.355127360692946e+283;
        bool r106133 = r106131 <= r106132;
        double r106134 = sqrt(r106130);
        double r106135 = r106127 / r106134;
        double r106136 = r106135 / r106134;
        double r106137 = -1.0;
        double r106138 = r106137 * r106121;
        double r106139 = r106138 / r106134;
        double r106140 = r106133 ? r106136 : r106139;
        return r106140;
}

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.2
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))) < 7.355127360692946e+283

    1. Initial program 14.2

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

    if 7.355127360692946e+283 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 62.9

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

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

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

      \[\leadsto \frac{\color{blue}{-1 \cdot 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 7.355127360692946109502946832371947729711 \cdot 10^{283}:\\ \;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\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 2020001 
(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))))