Average Error: 41.5 → 0.6
Time: 2.8s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \le 0.011011906827630152:\\ \;\;\;\;\frac{1}{1 - e^{\log 1 - 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.011011906827630152:\\
\;\;\;\;\frac{1}{1 - e^{\log 1 - x}}\\

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

\end{array}
double f(double x) {
        double r92301 = x;
        double r92302 = exp(r92301);
        double r92303 = 1.0;
        double r92304 = r92302 - r92303;
        double r92305 = r92302 / r92304;
        return r92305;
}

double f(double x) {
        double r92306 = x;
        double r92307 = exp(r92306);
        double r92308 = 0.011011906827630152;
        bool r92309 = r92307 <= r92308;
        double r92310 = 1.0;
        double r92311 = 1.0;
        double r92312 = log(r92311);
        double r92313 = r92312 - r92306;
        double r92314 = exp(r92313);
        double r92315 = r92310 - r92314;
        double r92316 = r92310 / r92315;
        double r92317 = 0.08333333333333333;
        double r92318 = r92310 / r92306;
        double r92319 = fma(r92317, r92306, r92318);
        double r92320 = 0.5;
        double r92321 = r92319 + r92320;
        double r92322 = r92309 ? r92316 : r92321;
        return r92322;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 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 add-exp-log0.0

      \[\leadsto \frac{1}{1 - \frac{\color{blue}{e^{\log 1}}}{e^{x}}}\]
    7. Applied div-exp0.0

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

    if 0.011011906827630152 < (exp x)

    1. Initial program 61.7

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

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

      \[\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.011011906827630152:\\ \;\;\;\;\frac{1}{1 - e^{\log 1 - x}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{x}\right) + \frac{1}{2}\\ \end{array}\]

Reproduce

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

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

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