Average Error: 40.0 → 0.7
Time: 14.6s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0019729997710959393:\\ \;\;\;\;\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}\;x \le -0.0019729997710959393:\\
\;\;\;\;\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 r1888978 = x;
        double r1888979 = exp(r1888978);
        double r1888980 = 1.0;
        double r1888981 = r1888979 - r1888980;
        double r1888982 = r1888979 / r1888981;
        return r1888982;
}

double f(double x) {
        double r1888983 = x;
        double r1888984 = -0.0019729997710959393;
        bool r1888985 = r1888983 <= r1888984;
        double r1888986 = 1.0;
        double r1888987 = exp(r1888983);
        double r1888988 = r1888986 / r1888987;
        double r1888989 = r1888986 - r1888988;
        double r1888990 = r1888986 / r1888989;
        double r1888991 = r1888986 / r1888983;
        double r1888992 = 0.08333333333333333;
        double r1888993 = 0.5;
        double r1888994 = fma(r1888992, r1888983, r1888993);
        double r1888995 = r1888991 + r1888994;
        double r1888996 = r1888985 ? r1888990 : r1888995;
        return r1888996;
}

Error

Bits error versus x

Target

Original40.0
Target39.5
Herbie0.7
\[\frac{1}{1 - e^{-x}}\]

Derivation

  1. Split input into 2 regimes
  2. if x < -0.0019729997710959393

    1. Initial program 0.0

      \[\frac{e^{x}}{e^{x} - 1}\]
    2. Using strategy rm
    3. Applied expm1-log1p-u0.0

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

      \[\leadsto \frac{e^{x}}{\mathsf{expm1}\left(\color{blue}{x}\right)}\]
    5. Using strategy rm
    6. Applied clear-num0.0

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{expm1}\left(x\right)}{e^{x}}}}\]
    7. Simplified0.0

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

    if -0.0019729997710959393 < x

    1. Initial program 60.1

      \[\frac{e^{x}}{e^{x} - 1}\]
    2. Using strategy rm
    3. Applied expm1-log1p-u60.1

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

      \[\leadsto \frac{e^{x}}{\mathsf{expm1}\left(\color{blue}{x}\right)}\]
    5. Taylor expanded around 0 1.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.0019729997710959393:\\ \;\;\;\;\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 2019133 +o rules:numerics
(FPCore (x)
  :name "expq2 (section 3.11)"

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

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