Average Error: 26.4 → 12.8
Time: 3.9s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.9459980091776752 \cdot 10^{158}:\\ \;\;\;\;\frac{-1 \cdot a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{elif}\;c \le -86223460658051.5:\\ \;\;\;\;\left(\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)} \cdot c\right) \cdot a + \left(\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)} \cdot d\right) \cdot b\\ \mathbf{elif}\;c \le 2.2413672752256256 \cdot 10^{125}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -2.9459980091776752 \cdot 10^{158}:\\
\;\;\;\;\frac{-1 \cdot a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\

\mathbf{elif}\;c \le -86223460658051.5:\\
\;\;\;\;\left(\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)} \cdot c\right) \cdot a + \left(\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)} \cdot d\right) \cdot b\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r100511 = a;
        double r100512 = c;
        double r100513 = r100511 * r100512;
        double r100514 = b;
        double r100515 = d;
        double r100516 = r100514 * r100515;
        double r100517 = r100513 + r100516;
        double r100518 = r100512 * r100512;
        double r100519 = r100515 * r100515;
        double r100520 = r100518 + r100519;
        double r100521 = r100517 / r100520;
        return r100521;
}

double f(double a, double b, double c, double d) {
        double r100522 = c;
        double r100523 = -2.945998009177675e+158;
        bool r100524 = r100522 <= r100523;
        double r100525 = -1.0;
        double r100526 = a;
        double r100527 = r100525 * r100526;
        double r100528 = d;
        double r100529 = hypot(r100522, r100528);
        double r100530 = 1.0;
        double r100531 = r100529 * r100530;
        double r100532 = r100527 / r100531;
        double r100533 = -86223460658051.5;
        bool r100534 = r100522 <= r100533;
        double r100535 = r100530 / r100529;
        double r100536 = r100535 / r100529;
        double r100537 = r100536 * r100522;
        double r100538 = r100537 * r100526;
        double r100539 = r100536 * r100528;
        double r100540 = b;
        double r100541 = r100539 * r100540;
        double r100542 = r100538 + r100541;
        double r100543 = 2.2413672752256256e+125;
        bool r100544 = r100522 <= r100543;
        double r100545 = r100540 * r100528;
        double r100546 = fma(r100526, r100522, r100545);
        double r100547 = r100546 / r100529;
        double r100548 = r100547 / r100531;
        double r100549 = r100526 / r100531;
        double r100550 = r100544 ? r100548 : r100549;
        double r100551 = r100534 ? r100542 : r100550;
        double r100552 = r100524 ? r100532 : r100551;
        return r100552;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.4
Target0.5
Herbie12.8
\[\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 4 regimes
  2. if c < -2.945998009177675e+158

    1. Initial program 45.8

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

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

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac45.8

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified45.8

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

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

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

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

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

    if -2.945998009177675e+158 < c < -86223460658051.5

    1. Initial program 22.3

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

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

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac22.3

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified22.3

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity17.2

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    10. Applied times-frac17.3

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\mathsf{fma}\left(a, c, b \cdot d\right)}\right)\]
    12. Using strategy rm
    13. Applied fma-udef17.3

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(a \cdot c + b \cdot d\right)}\right)\]
    14. Applied distribute-lft-in17.3

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a \cdot c\right) + \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(b \cdot d\right)\right)}\]
    15. Applied distribute-lft-in17.3

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

      \[\leadsto \color{blue}{\left(\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)} \cdot c\right) \cdot a} + \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(b \cdot d\right)\right)\]
    17. Simplified15.5

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

    if -86223460658051.5 < c < 2.2413672752256256e+125

    1. Initial program 18.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt18.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 *-un-lft-identity18.7

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac18.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified18.7

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

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

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

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

    if 2.2413672752256256e+125 < c

    1. Initial program 42.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt42.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 *-un-lft-identity42.7

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac42.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified42.7

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

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

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

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

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

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

Reproduce

herbie shell --seed 2020065 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :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))))