Average Error: 26.0 → 13.0
Time: 18.4s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -3.3286819924775857 \cdot 10^{+127}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot a\\ \mathbf{elif}\;c \le 1.0136212948540185 \cdot 10^{+209}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, d, a \cdot c\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\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}\;c \le -3.3286819924775857 \cdot 10^{+127}:\\
\;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot a\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r4113086 = a;
        double r4113087 = c;
        double r4113088 = r4113086 * r4113087;
        double r4113089 = b;
        double r4113090 = d;
        double r4113091 = r4113089 * r4113090;
        double r4113092 = r4113088 + r4113091;
        double r4113093 = r4113087 * r4113087;
        double r4113094 = r4113090 * r4113090;
        double r4113095 = r4113093 + r4113094;
        double r4113096 = r4113092 / r4113095;
        return r4113096;
}

double f(double a, double b, double c, double d) {
        double r4113097 = c;
        double r4113098 = -3.3286819924775857e+127;
        bool r4113099 = r4113097 <= r4113098;
        double r4113100 = -1.0;
        double r4113101 = d;
        double r4113102 = hypot(r4113101, r4113097);
        double r4113103 = r4113100 / r4113102;
        double r4113104 = a;
        double r4113105 = r4113103 * r4113104;
        double r4113106 = 1.0136212948540185e+209;
        bool r4113107 = r4113097 <= r4113106;
        double r4113108 = b;
        double r4113109 = r4113104 * r4113097;
        double r4113110 = fma(r4113108, r4113101, r4113109);
        double r4113111 = r4113110 / r4113102;
        double r4113112 = r4113111 / r4113102;
        double r4113113 = r4113104 / r4113102;
        double r4113114 = r4113107 ? r4113112 : r4113113;
        double r4113115 = r4113099 ? r4113105 : r4113114;
        return r4113115;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.5
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 c < -3.3286819924775857e+127

    1. Initial program 41.2

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

      \[\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-sqrt41.2

      \[\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 *-un-lft-identity41.2

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    7. Using strategy rm
    8. Applied fma-udef41.2

      \[\leadsto \frac{1}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    9. Applied hypot-def41.2

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

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

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

    if -3.3286819924775857e+127 < c < 1.0136212948540185e+209

    1. Initial program 20.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified20.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-sqrt20.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 *-un-lft-identity20.9

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    7. Using strategy rm
    8. Applied fma-udef20.9

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

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

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

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

    if 1.0136212948540185e+209 < c

    1. Initial program 43.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified43.0

      \[\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-sqrt43.0

      \[\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 *-un-lft-identity43.0

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

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

      \[\leadsto \frac{1}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    9. Applied hypot-def43.0

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -3.3286819924775857 \cdot 10^{+127}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot a\\ \mathbf{elif}\;c \le 1.0136212948540185 \cdot 10^{+209}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, d, a \cdot c\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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