Average Error: 25.8 → 12.0
Time: 13.2s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.328453055309952838396386350346556939505 \cdot 10^{184}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.721692170326450703244997905752579178238 \cdot 10^{180}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(d, b, a \cdot c\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.328453055309952838396386350346556939505 \cdot 10^{184}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \le 1.721692170326450703244997905752579178238 \cdot 10^{180}:\\
\;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(d, b, a \cdot c\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 r73849 = a;
        double r73850 = c;
        double r73851 = r73849 * r73850;
        double r73852 = b;
        double r73853 = d;
        double r73854 = r73852 * r73853;
        double r73855 = r73851 + r73854;
        double r73856 = r73850 * r73850;
        double r73857 = r73853 * r73853;
        double r73858 = r73856 + r73857;
        double r73859 = r73855 / r73858;
        return r73859;
}

double f(double a, double b, double c, double d) {
        double r73860 = c;
        double r73861 = -2.328453055309953e+184;
        bool r73862 = r73860 <= r73861;
        double r73863 = a;
        double r73864 = -r73863;
        double r73865 = d;
        double r73866 = hypot(r73860, r73865);
        double r73867 = r73864 / r73866;
        double r73868 = 1.7216921703264507e+180;
        bool r73869 = r73860 <= r73868;
        double r73870 = 1.0;
        double r73871 = b;
        double r73872 = r73863 * r73860;
        double r73873 = fma(r73865, r73871, r73872);
        double r73874 = r73866 / r73873;
        double r73875 = r73870 / r73874;
        double r73876 = r73875 / r73866;
        double r73877 = r73863 / r73866;
        double r73878 = r73869 ? r73876 : r73877;
        double r73879 = r73862 ? r73867 : r73878;
        return r73879;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.8
Target0.4
Herbie12.0
\[\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.328453055309953e+184

    1. Initial program 45.4

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

      \[\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 *-un-lft-identity45.4

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac45.4

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified45.4

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

      \[\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)}}\]
    8. Using strategy rm
    9. Applied associate-*r/32.6

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

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

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

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

    if -2.328453055309953e+184 < c < 1.7216921703264507e+180

    1. Initial program 20.8

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

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

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac20.8

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified20.8

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. 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)}}\]
    8. Using strategy rm
    9. Applied associate-*r/12.2

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

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

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

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

    if 1.7216921703264507e+180 < c

    1. Initial program 42.8

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

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

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac42.8

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified42.8

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

      \[\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)}}\]
    8. Using strategy rm
    9. Applied associate-*r/29.1

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

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

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

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

Reproduce

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