Average Error: 25.9 → 12.6
Time: 16.3s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -5.28633112851537097840983624634443392687 \cdot 10^{162}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.543067909128725187583327964894972109151 \cdot 10^{185}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -5.28633112851537097840983624634443392687 \cdot 10^{162}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r118728 = a;
        double r118729 = c;
        double r118730 = r118728 * r118729;
        double r118731 = b;
        double r118732 = d;
        double r118733 = r118731 * r118732;
        double r118734 = r118730 + r118733;
        double r118735 = r118729 * r118729;
        double r118736 = r118732 * r118732;
        double r118737 = r118735 + r118736;
        double r118738 = r118734 / r118737;
        return r118738;
}

double f(double a, double b, double c, double d) {
        double r118739 = c;
        double r118740 = -5.286331128515371e+162;
        bool r118741 = r118739 <= r118740;
        double r118742 = a;
        double r118743 = -r118742;
        double r118744 = d;
        double r118745 = hypot(r118739, r118744);
        double r118746 = r118743 / r118745;
        double r118747 = 1.5430679091287252e+185;
        bool r118748 = r118739 <= r118747;
        double r118749 = b;
        double r118750 = r118749 * r118744;
        double r118751 = fma(r118742, r118739, r118750);
        double r118752 = r118751 / r118745;
        double r118753 = r118752 / r118745;
        double r118754 = r118742 / r118745;
        double r118755 = r118748 ? r118753 : r118754;
        double r118756 = r118741 ? r118746 : r118755;
        return r118756;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.9
Target0.5
Herbie12.6
\[\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 < -5.286331128515371e+162

    1. Initial program 44.6

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{1} \cdot \frac{\color{blue}{-1 \cdot a}}{\mathsf{hypot}\left(c, d\right)}\]
    15. Simplified12.7

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

    if -5.286331128515371e+162 < c < 1.5430679091287252e+185

    1. Initial program 20.4

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

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

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

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

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

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

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

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

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

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

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

    if 1.5430679091287252e+185 < c

    1. Initial program 44.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt44.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-identity44.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-frac44.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. Simplified44.3

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.28633112851537097840983624634443392687 \cdot 10^{162}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.543067909128725187583327964894972109151 \cdot 10^{185}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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