Average Error: 41.5 → 0.8
Time: 13.3s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \le 3.936159656535852086796232633999635795103 \cdot 10^{-90}:\\ \;\;\;\;\frac{1}{1 - \frac{1}{e^{x}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} + \mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)\\ \end{array}\]
\frac{e^{x}}{e^{x} - 1}
\begin{array}{l}
\mathbf{if}\;e^{x} \le 3.936159656535852086796232633999635795103 \cdot 10^{-90}:\\
\;\;\;\;\frac{1}{1 - \frac{1}{e^{x}}}\\

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

\end{array}
double f(double x) {
        double r95716 = x;
        double r95717 = exp(r95716);
        double r95718 = 1.0;
        double r95719 = r95717 - r95718;
        double r95720 = r95717 / r95719;
        return r95720;
}

double f(double x) {
        double r95721 = x;
        double r95722 = exp(r95721);
        double r95723 = 3.936159656535852e-90;
        bool r95724 = r95722 <= r95723;
        double r95725 = 1.0;
        double r95726 = 1.0;
        double r95727 = r95726 / r95722;
        double r95728 = r95725 - r95727;
        double r95729 = r95725 / r95728;
        double r95730 = r95725 / r95721;
        double r95731 = 0.08333333333333333;
        double r95732 = 0.5;
        double r95733 = fma(r95731, r95721, r95732);
        double r95734 = r95730 + r95733;
        double r95735 = r95724 ? r95729 : r95734;
        return r95735;
}

Error

Bits error versus x

Target

Original41.5
Target41.0
Herbie0.8
\[\frac{1}{1 - e^{-x}}\]

Derivation

  1. Split input into 2 regimes
  2. if (exp x) < 3.936159656535852e-90

    1. Initial program 0

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

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

      \[\leadsto \frac{1}{\color{blue}{1 - \frac{1}{e^{x}}}}\]

    if 3.936159656535852e-90 < (exp x)

    1. Initial program 61.6

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

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

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

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

Reproduce

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

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

  (/ (exp x) (- (exp x) 1.0)))