Average Error: 26.0 → 13.4
Time: 30.9s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.984621041690948382860905188525758471732 \cdot 10^{253}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 4.040688054161811545697476400410059624914 \cdot 10^{173}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\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.984621041690948382860905188525758471732 \cdot 10^{253}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \le 4.040688054161811545697476400410059624914 \cdot 10^{173}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\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 r4943941 = a;
        double r4943942 = c;
        double r4943943 = r4943941 * r4943942;
        double r4943944 = b;
        double r4943945 = d;
        double r4943946 = r4943944 * r4943945;
        double r4943947 = r4943943 + r4943946;
        double r4943948 = r4943942 * r4943942;
        double r4943949 = r4943945 * r4943945;
        double r4943950 = r4943948 + r4943949;
        double r4943951 = r4943947 / r4943950;
        return r4943951;
}

double f(double a, double b, double c, double d) {
        double r4943952 = c;
        double r4943953 = -2.9846210416909484e+253;
        bool r4943954 = r4943952 <= r4943953;
        double r4943955 = a;
        double r4943956 = -r4943955;
        double r4943957 = d;
        double r4943958 = hypot(r4943952, r4943957);
        double r4943959 = r4943956 / r4943958;
        double r4943960 = 4.0406880541618115e+173;
        bool r4943961 = r4943952 <= r4943960;
        double r4943962 = b;
        double r4943963 = r4943962 * r4943957;
        double r4943964 = fma(r4943955, r4943952, r4943963);
        double r4943965 = r4943964 / r4943958;
        double r4943966 = r4943965 / r4943958;
        double r4943967 = r4943955 / r4943958;
        double r4943968 = r4943961 ? r4943966 : r4943967;
        double r4943969 = r4943954 ? r4943959 : r4943968;
        return r4943969;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.5
Herbie13.4
\[\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.9846210416909484e+253

    1. Initial program 40.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt40.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 associate-/r*40.4

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def40.4

      \[\leadsto \frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}\]
    7. Taylor expanded around -inf 7.9

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

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

    if -2.9846210416909484e+253 < c < 4.0406880541618115e+173

    1. Initial program 22.7

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

      \[\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 associate-/r*22.6

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def22.6

      \[\leadsto \frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}\]
    7. Using strategy rm
    8. Applied hypot-def14.0

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

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

    if 4.0406880541618115e+173 < c

    1. Initial program 44.0

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

      \[\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 associate-/r*44.0

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def44.0

      \[\leadsto \frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}\]
    7. Taylor expanded around inf 11.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -2.984621041690948382860905188525758471732 \cdot 10^{253}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 4.040688054161811545697476400410059624914 \cdot 10^{173}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\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 2019200 +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))))