Average Error: 25.7 → 14.9
Time: 19.8s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le 3.5031564744705207 \cdot 10^{+157}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le 3.5031564744705207 \cdot 10^{+157}:\\
\;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r2992117 = a;
        double r2992118 = c;
        double r2992119 = r2992117 * r2992118;
        double r2992120 = b;
        double r2992121 = d;
        double r2992122 = r2992120 * r2992121;
        double r2992123 = r2992119 + r2992122;
        double r2992124 = r2992118 * r2992118;
        double r2992125 = r2992121 * r2992121;
        double r2992126 = r2992124 + r2992125;
        double r2992127 = r2992123 / r2992126;
        return r2992127;
}

double f(double a, double b, double c, double d) {
        double r2992128 = c;
        double r2992129 = 3.5031564744705207e+157;
        bool r2992130 = r2992128 <= r2992129;
        double r2992131 = 1.0;
        double r2992132 = d;
        double r2992133 = hypot(r2992132, r2992128);
        double r2992134 = a;
        double r2992135 = b;
        double r2992136 = r2992135 * r2992132;
        double r2992137 = fma(r2992134, r2992128, r2992136);
        double r2992138 = r2992133 / r2992137;
        double r2992139 = r2992131 / r2992138;
        double r2992140 = r2992139 / r2992133;
        double r2992141 = r2992134 / r2992133;
        double r2992142 = r2992130 ? r2992140 : r2992141;
        return r2992142;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
Target0.4
Herbie14.9
\[\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 c < 3.5031564744705207e+157

    1. Initial program 23.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified23.1

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt23.1

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*23.1

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    6. Using strategy rm
    7. Applied *-un-lft-identity23.1

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    8. Applied associate-/l*23.1

      \[\leadsto \frac{\color{blue}{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef23.1

      \[\leadsto \frac{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def23.1

      \[\leadsto \frac{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}\]
    12. Using strategy rm
    13. Applied fma-udef23.1

      \[\leadsto \frac{\frac{1}{\frac{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\mathsf{hypot}\left(d, c\right)}\]
    14. Applied hypot-def15.0

      \[\leadsto \frac{\frac{1}{\frac{\color{blue}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\mathsf{hypot}\left(d, c\right)}\]

    if 3.5031564744705207e+157 < c

    1. Initial program 44.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified44.5

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt44.5

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*44.5

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    6. Using strategy rm
    7. Applied *-un-lft-identity44.5

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    8. Applied associate-/l*44.5

      \[\leadsto \frac{\color{blue}{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef44.5

      \[\leadsto \frac{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def44.5

      \[\leadsto \frac{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}\]
    12. Taylor expanded around 0 13.9

      \[\leadsto \frac{\color{blue}{a}}{\mathsf{hypot}\left(d, c\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification14.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le 3.5031564744705207 \cdot 10^{+157}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2019130 +o rules:numerics
(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))))