Average Error: 25.6 → 12.9
Time: 18.4s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -5.086126503497258 \cdot 10^{+188}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 6.204871618360054 \cdot 10^{+176}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\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 -5.086126503497258 \cdot 10^{+188}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 6.204871618360054 \cdot 10^{+176}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\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 r4858290 = a;
        double r4858291 = c;
        double r4858292 = r4858290 * r4858291;
        double r4858293 = b;
        double r4858294 = d;
        double r4858295 = r4858293 * r4858294;
        double r4858296 = r4858292 + r4858295;
        double r4858297 = r4858291 * r4858291;
        double r4858298 = r4858294 * r4858294;
        double r4858299 = r4858297 + r4858298;
        double r4858300 = r4858296 / r4858299;
        return r4858300;
}

double f(double a, double b, double c, double d) {
        double r4858301 = c;
        double r4858302 = -5.086126503497258e+188;
        bool r4858303 = r4858301 <= r4858302;
        double r4858304 = a;
        double r4858305 = -r4858304;
        double r4858306 = d;
        double r4858307 = hypot(r4858306, r4858301);
        double r4858308 = r4858305 / r4858307;
        double r4858309 = 6.204871618360054e+176;
        bool r4858310 = r4858301 <= r4858309;
        double r4858311 = b;
        double r4858312 = r4858306 * r4858311;
        double r4858313 = fma(r4858304, r4858301, r4858312);
        double r4858314 = r4858313 / r4858307;
        double r4858315 = r4858314 / r4858307;
        double r4858316 = r4858304 / r4858307;
        double r4858317 = r4858310 ? r4858315 : r4858316;
        double r4858318 = r4858303 ? r4858308 : r4858317;
        return r4858318;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.6
Target0.4
Herbie12.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 3 regimes
  2. if c < -5.086126503497258e+188

    1. Initial program 41.2

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

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

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

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    6. Using strategy rm
    7. Applied clear-num41.2

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}}}\]
    8. Simplified29.6

      \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, d, c \cdot a\right)} \cdot \mathsf{hypot}\left(d, c\right)}}\]
    9. Using strategy rm
    10. Applied associate-/r*29.0

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

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}}{\mathsf{hypot}\left(d, c\right)}\]
    12. Taylor expanded around -inf 12.0

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\mathsf{hypot}\left(d, c\right)}\]
    13. Simplified12.0

      \[\leadsto \frac{\color{blue}{-a}}{\mathsf{hypot}\left(d, c\right)}\]

    if -5.086126503497258e+188 < c < 6.204871618360054e+176

    1. Initial program 20.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified20.9

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}}}\]
    8. Simplified13.6

      \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, d, c \cdot a\right)} \cdot \mathsf{hypot}\left(d, c\right)}}\]
    9. Using strategy rm
    10. Applied associate-/r*13.1

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

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

    if 6.204871618360054e+176 < c

    1. Initial program 44.6

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

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}}}\]
    8. Simplified31.9

      \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, d, c \cdot a\right)} \cdot \mathsf{hypot}\left(d, c\right)}}\]
    9. Using strategy rm
    10. Applied associate-/r*31.4

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.086126503497258 \cdot 10^{+188}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 6.204871618360054 \cdot 10^{+176}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2019162 +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))))