Average Error: 25.7 → 13.0
Time: 32.5s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -7.67400831190257 \cdot 10^{+96}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \le 4.711008584896772 \cdot 10^{+178}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\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}\;d \le -7.67400831190257 \cdot 10^{+96}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r2642041 = a;
        double r2642042 = c;
        double r2642043 = r2642041 * r2642042;
        double r2642044 = b;
        double r2642045 = d;
        double r2642046 = r2642044 * r2642045;
        double r2642047 = r2642043 + r2642046;
        double r2642048 = r2642042 * r2642042;
        double r2642049 = r2642045 * r2642045;
        double r2642050 = r2642048 + r2642049;
        double r2642051 = r2642047 / r2642050;
        return r2642051;
}

double f(double a, double b, double c, double d) {
        double r2642052 = d;
        double r2642053 = -7.67400831190257e+96;
        bool r2642054 = r2642052 <= r2642053;
        double r2642055 = b;
        double r2642056 = -r2642055;
        double r2642057 = c;
        double r2642058 = hypot(r2642052, r2642057);
        double r2642059 = r2642056 / r2642058;
        double r2642060 = 4.711008584896772e+178;
        bool r2642061 = r2642052 <= r2642060;
        double r2642062 = a;
        double r2642063 = r2642055 * r2642052;
        double r2642064 = fma(r2642057, r2642062, r2642063);
        double r2642065 = r2642064 / r2642058;
        double r2642066 = r2642065 / r2642058;
        double r2642067 = r2642055 / r2642058;
        double r2642068 = r2642061 ? r2642066 : r2642067;
        double r2642069 = r2642054 ? r2642059 : r2642068;
        return r2642069;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
Target0.4
Herbie13.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 d < -7.67400831190257e+96

    1. Initial program 38.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified38.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-sqrt38.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*38.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-num38.9

      \[\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. Simplified26.3

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

      \[\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)}}\]
    11. Applied associate-/r*26.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)}}\]
    12. Taylor expanded around -inf 17.2

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

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

    if -7.67400831190257e+96 < d < 4.711008584896772e+178

    1. Initial program 19.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified19.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-sqrt19.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*19.5

      \[\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-num19.7

      \[\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. Simplified12.5

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

      \[\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)}}\]
    11. Applied associate-/r*12.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)}}\]
    12. Using strategy rm
    13. Applied *-un-lft-identity12.1

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

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

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

    if 4.711008584896772e+178 < d

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

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

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

      \[\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)}}\]
    11. Applied associate-/r*30.6

      \[\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)}}\]
    12. Taylor expanded around inf 11.9

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

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

Reproduce

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