Average Error: 41.0 → 0.6
Time: 2.9s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \le 0.33936867890953548:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{1 - \frac{1}{e^{x}}}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{x}\right) + \frac{1}{2}\\ \end{array}\]
\frac{e^{x}}{e^{x} - 1}
\begin{array}{l}
\mathbf{if}\;e^{x} \le 0.33936867890953548:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{1 - \frac{1}{e^{x}}}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{x}\right) + \frac{1}{2}\\

\end{array}
double f(double x) {
        double r104707 = x;
        double r104708 = exp(r104707);
        double r104709 = 1.0;
        double r104710 = r104708 - r104709;
        double r104711 = r104708 / r104710;
        return r104711;
}

double f(double x) {
        double r104712 = x;
        double r104713 = exp(r104712);
        double r104714 = 0.3393686789095355;
        bool r104715 = r104713 <= r104714;
        double r104716 = 1.0;
        double r104717 = 1.0;
        double r104718 = r104717 / r104713;
        double r104719 = r104716 - r104718;
        double r104720 = r104716 / r104719;
        double r104721 = log1p(r104720);
        double r104722 = expm1(r104721);
        double r104723 = 0.08333333333333333;
        double r104724 = r104716 / r104712;
        double r104725 = fma(r104723, r104712, r104724);
        double r104726 = 0.5;
        double r104727 = r104725 + r104726;
        double r104728 = r104715 ? r104722 : r104727;
        return r104728;
}

Error

Bits error versus x

Target

Original41.0
Target40.6
Herbie0.6
\[\frac{1}{1 - e^{-x}}\]

Derivation

  1. Split input into 2 regimes
  2. if (exp x) < 0.3393686789095355

    1. Initial program 0.0

      \[\frac{e^{x}}{e^{x} - 1}\]
    2. Using strategy rm
    3. Applied clear-num0.0

      \[\leadsto \color{blue}{\frac{1}{\frac{e^{x} - 1}{e^{x}}}}\]
    4. Simplified0.0

      \[\leadsto \frac{1}{\color{blue}{1 - \frac{1}{e^{x}}}}\]
    5. Using strategy rm
    6. Applied expm1-log1p-u0.0

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{1 - \frac{1}{e^{x}}}\right)\right)}\]

    if 0.3393686789095355 < (exp x)

    1. Initial program 61.8

      \[\frac{e^{x}}{e^{x} - 1}\]
    2. Taylor expanded around 0 1.0

      \[\leadsto \color{blue}{\frac{1}{2} + \left(\frac{1}{12} \cdot x + \frac{1}{x}\right)}\]
    3. Simplified1.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{x}\right) + \frac{1}{2}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{x} \le 0.33936867890953548:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{1 - \frac{1}{e^{x}}}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{x}\right) + \frac{1}{2}\\ \end{array}\]

Reproduce

herbie shell --seed 2020065 +o rules:numerics
(FPCore (x)
  :name "expq2 (section 3.11)"
  :precision binary64

  :herbie-target
  (/ 1 (- 1 (exp (- x))))

  (/ (exp x) (- (exp x) 1)))