Average Error: 26.6 → 13.5
Time: 16.2s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -1.696889279647290237234578100884848526295 \cdot 10^{245}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot b\\ \mathbf{elif}\;d \le 7.009208612122958667307700471256846817647 \cdot 10^{165}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{1}{\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.696889279647290237234578100884848526295 \cdot 10^{245}:\\
\;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot b\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r4876615 = a;
        double r4876616 = c;
        double r4876617 = r4876615 * r4876616;
        double r4876618 = b;
        double r4876619 = d;
        double r4876620 = r4876618 * r4876619;
        double r4876621 = r4876617 + r4876620;
        double r4876622 = r4876616 * r4876616;
        double r4876623 = r4876619 * r4876619;
        double r4876624 = r4876622 + r4876623;
        double r4876625 = r4876621 / r4876624;
        return r4876625;
}

double f(double a, double b, double c, double d) {
        double r4876626 = d;
        double r4876627 = -1.6968892796472902e+245;
        bool r4876628 = r4876626 <= r4876627;
        double r4876629 = -1.0;
        double r4876630 = c;
        double r4876631 = hypot(r4876626, r4876630);
        double r4876632 = r4876629 / r4876631;
        double r4876633 = b;
        double r4876634 = r4876632 * r4876633;
        double r4876635 = 7.009208612122959e+165;
        bool r4876636 = r4876626 <= r4876635;
        double r4876637 = a;
        double r4876638 = r4876633 * r4876626;
        double r4876639 = fma(r4876637, r4876630, r4876638);
        double r4876640 = r4876639 / r4876631;
        double r4876641 = r4876640 / r4876631;
        double r4876642 = 1.0;
        double r4876643 = r4876642 / r4876631;
        double r4876644 = r4876633 * r4876643;
        double r4876645 = r4876636 ? r4876641 : r4876644;
        double r4876646 = r4876628 ? r4876634 : r4876645;
        return r4876646;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.6
Target0.5
Herbie13.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.6968892796472902e+245

    1. Initial program 41.8

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

      \[\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 clear-num41.8

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

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}}\]
    7. Applied add-sqr-sqrt41.8

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

      \[\leadsto \frac{1}{\color{blue}{\frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{1} \cdot \frac{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}\]
    9. Applied add-cube-cbrt41.8

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

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

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

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

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

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

    if -1.6968892796472902e+245 < d < 7.009208612122959e+165

    1. Initial program 23.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified23.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 clear-num23.1

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}\]
    5. Using strategy rm
    6. Applied *-un-lft-identity23.1

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}}\]
    7. Applied add-sqr-sqrt23.1

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

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

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

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

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

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

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

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

    if 7.009208612122959e+165 < d

    1. Initial program 45.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified45.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 clear-num45.0

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}\]
    5. Using strategy rm
    6. Applied *-un-lft-identity45.0

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}}\]
    7. Applied add-sqr-sqrt45.0

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -1.696889279647290237234578100884848526295 \cdot 10^{245}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot b\\ \mathbf{elif}\;d \le 7.009208612122958667307700471256846817647 \cdot 10^{165}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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