Average Error: 41.2 → 0.9
Time: 2.3s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \le 0.0:\\ \;\;\;\;\frac{1}{1 - \frac{1}{e^{x}}}\\ \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.0:\\
\;\;\;\;\frac{1}{1 - \frac{1}{e^{x}}}\\

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

\end{array}
double f(double x) {
        double r79241 = x;
        double r79242 = exp(r79241);
        double r79243 = 1.0;
        double r79244 = r79242 - r79243;
        double r79245 = r79242 / r79244;
        return r79245;
}

double f(double x) {
        double r79246 = x;
        double r79247 = exp(r79246);
        double r79248 = 0.0;
        bool r79249 = r79247 <= r79248;
        double r79250 = 1.0;
        double r79251 = 1.0;
        double r79252 = r79251 / r79247;
        double r79253 = r79250 - r79252;
        double r79254 = r79250 / r79253;
        double r79255 = 0.08333333333333333;
        double r79256 = r79250 / r79246;
        double r79257 = fma(r79255, r79246, r79256);
        double r79258 = 0.5;
        double r79259 = r79257 + r79258;
        double r79260 = r79249 ? r79254 : r79259;
        return r79260;
}

Error

Bits error versus x

Target

Original41.2
Target40.8
Herbie0.9
\[\frac{1}{1 - e^{-x}}\]

Derivation

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

    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 0.0 < (exp x)

    1. Initial program 61.3

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

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

      \[\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.9

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

Reproduce

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

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

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