Average Error: 26.0 → 12.5
Time: 36.5s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -2.052205868896771 \cdot 10^{+142}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \le 6.134295887350108 \cdot 10^{+151}:\\ \;\;\;\;\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)}\\ \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 -2.052205868896771 \cdot 10^{+142}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;d \le 6.134295887350108 \cdot 10^{+151}:\\
\;\;\;\;\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)}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r3262977 = a;
        double r3262978 = c;
        double r3262979 = r3262977 * r3262978;
        double r3262980 = b;
        double r3262981 = d;
        double r3262982 = r3262980 * r3262981;
        double r3262983 = r3262979 + r3262982;
        double r3262984 = r3262978 * r3262978;
        double r3262985 = r3262981 * r3262981;
        double r3262986 = r3262984 + r3262985;
        double r3262987 = r3262983 / r3262986;
        return r3262987;
}

double f(double a, double b, double c, double d) {
        double r3262988 = d;
        double r3262989 = -2.052205868896771e+142;
        bool r3262990 = r3262988 <= r3262989;
        double r3262991 = b;
        double r3262992 = -r3262991;
        double r3262993 = c;
        double r3262994 = hypot(r3262988, r3262993);
        double r3262995 = r3262992 / r3262994;
        double r3262996 = 6.134295887350108e+151;
        bool r3262997 = r3262988 <= r3262996;
        double r3262998 = 1.0;
        double r3262999 = a;
        double r3263000 = r3262993 * r3262999;
        double r3263001 = fma(r3262991, r3262988, r3263000);
        double r3263002 = r3262994 / r3263001;
        double r3263003 = r3262998 / r3263002;
        double r3263004 = r3263003 / r3262994;
        double r3263005 = r3262991 / r3262994;
        double r3263006 = r3262997 ? r3263004 : r3263005;
        double r3263007 = r3262990 ? r3262995 : r3263006;
        return r3263007;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.5
Herbie12.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 d < -2.052205868896771e+142

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

      \[\leadsto \frac{\color{blue}{1 \cdot \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)}}\]
    8. Applied associate-/l*44.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)}}}}}\]
    9. Simplified29.8

      \[\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)}}}}\]
    10. Using strategy rm
    11. Applied associate-/r/29.8

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

      \[\leadsto \frac{\color{blue}{-1 \cdot b}}{\mathsf{hypot}\left(d, c\right)}\]
    14. Simplified13.3

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

    if -2.052205868896771e+142 < d < 6.134295887350108e+151

    1. Initial program 18.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified18.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-sqrt18.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*18.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 *-un-lft-identity18.8

      \[\leadsto \frac{\color{blue}{1 \cdot \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)}}\]
    8. Applied associate-/l*19.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)}}}}}\]
    9. Simplified12.6

      \[\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)}}}}\]
    10. Using strategy rm
    11. Applied associate-/r/12.6

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

      \[\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)}}\]

    if 6.134295887350108e+151 < d

    1. Initial program 44.5

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

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

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

      \[\leadsto \frac{\color{blue}{1 \cdot \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)}}\]
    8. Applied associate-/l*44.5

      \[\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)}}}}}\]
    9. Simplified29.2

      \[\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)}}}}\]
    10. Using strategy rm
    11. Applied associate-/r/29.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -2.052205868896771 \cdot 10^{+142}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \le 6.134295887350108 \cdot 10^{+151}:\\ \;\;\;\;\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)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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