Average Error: 25.2 → 12.5
Time: 20.9s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -1.0326333584696313 \cdot 10^{+176}:\\ \;\;\;\;\frac{\sqrt{1}}{\sqrt{1}} \cdot \left(-\frac{b}{\mathsf{hypot}\left(d, c\right)}\right)\\ \mathbf{elif}\;d \le 3.0030170552383878 \cdot 10^{+144}:\\ \;\;\;\;\frac{\sqrt{1}}{\sqrt{1}} \cdot \frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right) \cdot \sqrt{1}}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\sqrt{1}}{\sqrt{1}} \cdot \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 -1.0326333584696313 \cdot 10^{+176}:\\
\;\;\;\;\frac{\sqrt{1}}{\sqrt{1}} \cdot \left(-\frac{b}{\mathsf{hypot}\left(d, c\right)}\right)\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r4723214 = a;
        double r4723215 = c;
        double r4723216 = r4723214 * r4723215;
        double r4723217 = b;
        double r4723218 = d;
        double r4723219 = r4723217 * r4723218;
        double r4723220 = r4723216 + r4723219;
        double r4723221 = r4723215 * r4723215;
        double r4723222 = r4723218 * r4723218;
        double r4723223 = r4723221 + r4723222;
        double r4723224 = r4723220 / r4723223;
        return r4723224;
}

double f(double a, double b, double c, double d) {
        double r4723225 = d;
        double r4723226 = -1.0326333584696313e+176;
        bool r4723227 = r4723225 <= r4723226;
        double r4723228 = 1.0;
        double r4723229 = sqrt(r4723228);
        double r4723230 = r4723229 / r4723229;
        double r4723231 = b;
        double r4723232 = c;
        double r4723233 = hypot(r4723225, r4723232);
        double r4723234 = r4723231 / r4723233;
        double r4723235 = -r4723234;
        double r4723236 = r4723230 * r4723235;
        double r4723237 = 3.0030170552383878e+144;
        bool r4723238 = r4723225 <= r4723237;
        double r4723239 = a;
        double r4723240 = r4723225 * r4723231;
        double r4723241 = fma(r4723232, r4723239, r4723240);
        double r4723242 = r4723241 * r4723229;
        double r4723243 = r4723242 / r4723233;
        double r4723244 = r4723243 / r4723233;
        double r4723245 = r4723230 * r4723244;
        double r4723246 = r4723230 * r4723234;
        double r4723247 = r4723238 ? r4723245 : r4723246;
        double r4723248 = r4723227 ? r4723236 : r4723247;
        return r4723248;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.2
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 < -1.0326333584696313e+176

    1. Initial program 45.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified45.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-sqrt45.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-identity45.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-frac45.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 *-un-lft-identity45.2

      \[\leadsto \frac{1}{\sqrt{\color{blue}{1 \cdot \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)}}\]
    9. Applied sqrt-prod45.2

      \[\leadsto \frac{1}{\color{blue}{\sqrt{1} \cdot \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)}}\]
    10. Applied add-sqr-sqrt45.2

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{\sqrt{1} \cdot \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)}}\]
    11. Applied times-frac45.2

      \[\leadsto \color{blue}{\left(\frac{\sqrt{1}}{\sqrt{1}} \cdot \frac{\sqrt{1}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\right)} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    12. Applied associate-*l*45.2

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\sqrt{1}} \cdot \left(\frac{\sqrt{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)}}\right)}\]
    13. Simplified30.3

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

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

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

    if -1.0326333584696313e+176 < d < 3.0030170552383878e+144

    1. Initial program 19.1

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

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

      \[\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 *-un-lft-identity19.1

      \[\leadsto \frac{1}{\sqrt{\color{blue}{1 \cdot \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)}}\]
    9. Applied sqrt-prod19.1

      \[\leadsto \frac{1}{\color{blue}{\sqrt{1} \cdot \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)}}\]
    10. Applied add-sqr-sqrt19.1

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{\sqrt{1} \cdot \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)}}\]
    11. Applied times-frac19.1

      \[\leadsto \color{blue}{\left(\frac{\sqrt{1}}{\sqrt{1}} \cdot \frac{\sqrt{1}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\right)} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    12. Applied associate-*l*19.1

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\sqrt{1}} \cdot \left(\frac{\sqrt{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)}}\right)}\]
    13. Simplified12.2

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

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

    if 3.0030170552383878e+144 < d

    1. Initial program 41.7

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

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

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

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

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

      \[\leadsto \frac{1}{\sqrt{\color{blue}{1 \cdot \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)}}\]
    9. Applied sqrt-prod41.7

      \[\leadsto \frac{1}{\color{blue}{\sqrt{1} \cdot \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)}}\]
    10. Applied add-sqr-sqrt41.7

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{\sqrt{1} \cdot \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)}}\]
    11. Applied times-frac41.7

      \[\leadsto \color{blue}{\left(\frac{\sqrt{1}}{\sqrt{1}} \cdot \frac{\sqrt{1}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\right)} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    12. Applied associate-*l*41.7

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\sqrt{1}} \cdot \left(\frac{\sqrt{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)}}\right)}\]
    13. Simplified26.5

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

      \[\leadsto \frac{\sqrt{1}}{\sqrt{1}} \cdot \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 -1.0326333584696313 \cdot 10^{+176}:\\ \;\;\;\;\frac{\sqrt{1}}{\sqrt{1}} \cdot \left(-\frac{b}{\mathsf{hypot}\left(d, c\right)}\right)\\ \mathbf{elif}\;d \le 3.0030170552383878 \cdot 10^{+144}:\\ \;\;\;\;\frac{\sqrt{1}}{\sqrt{1}} \cdot \frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right) \cdot \sqrt{1}}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\sqrt{1}}{\sqrt{1}} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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