Average Error: 25.8 → 12.9
Time: 11.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.4176012612855429 \cdot 10^{94}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-a\right)\\ \mathbf{elif}\;c \le 9.5683211336282513 \cdot 10^{173}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(d, b, a \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -2.4176012612855429 \cdot 10^{94}:\\
\;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-a\right)\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r153829 = a;
        double r153830 = c;
        double r153831 = r153829 * r153830;
        double r153832 = b;
        double r153833 = d;
        double r153834 = r153832 * r153833;
        double r153835 = r153831 + r153834;
        double r153836 = r153830 * r153830;
        double r153837 = r153833 * r153833;
        double r153838 = r153836 + r153837;
        double r153839 = r153835 / r153838;
        return r153839;
}

double f(double a, double b, double c, double d) {
        double r153840 = c;
        double r153841 = -2.417601261285543e+94;
        bool r153842 = r153840 <= r153841;
        double r153843 = 1.0;
        double r153844 = d;
        double r153845 = hypot(r153840, r153844);
        double r153846 = r153843 / r153845;
        double r153847 = a;
        double r153848 = -r153847;
        double r153849 = r153846 * r153848;
        double r153850 = 9.568321133628251e+173;
        bool r153851 = r153840 <= r153850;
        double r153852 = b;
        double r153853 = r153847 * r153840;
        double r153854 = fma(r153844, r153852, r153853);
        double r153855 = r153854 / r153845;
        double r153856 = r153855 / r153845;
        double r153857 = r153847 / r153845;
        double r153858 = r153851 ? r153856 : r153857;
        double r153859 = r153842 ? r153849 : r153858;
        return r153859;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.8
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 < -2.417601261285543e+94

    1. Initial program 37.6

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

      \[\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-sqrt37.6

      \[\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 *-un-lft-identity37.6

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

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

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

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

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

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

    if -2.417601261285543e+94 < c < 9.568321133628251e+173

    1. Initial program 19.8

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

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

      \[\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 *-un-lft-identity19.8

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

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

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}\]
    11. Using strategy rm
    12. Applied *-un-lft-identity12.4

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(c, d\right)}} \cdot \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}\]
    13. Applied *-un-lft-identity12.4

      \[\leadsto \frac{\color{blue}{1 \cdot 1}}{1 \cdot \mathsf{hypot}\left(c, d\right)} \cdot \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}\]
    14. Applied times-frac12.4

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

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

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

    if 9.568321133628251e+173 < c

    1. Initial program 44.1

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

      \[\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-sqrt44.1

      \[\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 *-un-lft-identity44.1

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

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

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}\]
    11. Using strategy rm
    12. Applied *-un-lft-identity29.9

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(c, d\right)}} \cdot \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}\]
    13. Applied *-un-lft-identity29.9

      \[\leadsto \frac{\color{blue}{1 \cdot 1}}{1 \cdot \mathsf{hypot}\left(c, d\right)} \cdot \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}\]
    14. Applied times-frac29.9

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

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

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

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

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

Reproduce

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