Average Error: 26.8 → 13.5
Time: 17.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.045846669237430472528864761609559364154 \cdot 10^{123}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 8.747123436813267982750810114699097243072 \cdot 10^{191}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{a}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -2.045846669237430472528864761609559364154 \cdot 10^{123}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \le 8.747123436813267982750810114699097243072 \cdot 10^{191}:\\
\;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}{\mathsf{hypot}\left(c, d\right)}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r8259699 = a;
        double r8259700 = c;
        double r8259701 = r8259699 * r8259700;
        double r8259702 = b;
        double r8259703 = d;
        double r8259704 = r8259702 * r8259703;
        double r8259705 = r8259701 + r8259704;
        double r8259706 = r8259700 * r8259700;
        double r8259707 = r8259703 * r8259703;
        double r8259708 = r8259706 + r8259707;
        double r8259709 = r8259705 / r8259708;
        return r8259709;
}

double f(double a, double b, double c, double d) {
        double r8259710 = c;
        double r8259711 = -2.0458466692374305e+123;
        bool r8259712 = r8259710 <= r8259711;
        double r8259713 = a;
        double r8259714 = -r8259713;
        double r8259715 = d;
        double r8259716 = hypot(r8259710, r8259715);
        double r8259717 = r8259714 / r8259716;
        double r8259718 = 8.747123436813268e+191;
        bool r8259719 = r8259710 <= r8259718;
        double r8259720 = 1.0;
        double r8259721 = b;
        double r8259722 = r8259721 * r8259715;
        double r8259723 = fma(r8259713, r8259710, r8259722);
        double r8259724 = r8259716 / r8259723;
        double r8259725 = r8259720 / r8259724;
        double r8259726 = r8259725 / r8259716;
        double r8259727 = r8259716 / r8259713;
        double r8259728 = r8259720 / r8259727;
        double r8259729 = r8259719 ? r8259726 : r8259728;
        double r8259730 = r8259712 ? r8259717 : r8259729;
        return r8259730;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.8
Target0.4
Herbie13.5
\[\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 < -2.0458466692374305e+123

    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(c, c, d \cdot d\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(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied associate-/r*41.1

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

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

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

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

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

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

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

    if -2.0458466692374305e+123 < c < 8.747123436813268e+191

    1. Initial program 21.4

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

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

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

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

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

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

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

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

    if 8.747123436813268e+191 < c

    1. Initial program 43.0

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -2.045846669237430472528864761609559364154 \cdot 10^{123}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 8.747123436813267982750810114699097243072 \cdot 10^{191}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{a}}\\ \end{array}\]

Reproduce

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