Average Error: 39.8 → 0.6
Time: 14.2s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.002140507652831692:\\ \;\;\;\;\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.002140507652831692:\\
\;\;\;\;\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 r1807782 = x;
        double r1807783 = exp(r1807782);
        double r1807784 = 1.0;
        double r1807785 = r1807783 - r1807784;
        double r1807786 = r1807783 / r1807785;
        return r1807786;
}

double f(double x) {
        double r1807787 = x;
        double r1807788 = -0.002140507652831692;
        bool r1807789 = r1807787 <= r1807788;
        double r1807790 = 1.0;
        double r1807791 = exp(r1807787);
        double r1807792 = r1807790 / r1807791;
        double r1807793 = r1807790 - r1807792;
        double r1807794 = r1807790 / r1807793;
        double r1807795 = r1807790 / r1807787;
        double r1807796 = 0.08333333333333333;
        double r1807797 = 0.5;
        double r1807798 = fma(r1807796, r1807787, r1807797);
        double r1807799 = r1807795 + r1807798;
        double r1807800 = r1807789 ? r1807794 : r1807799;
        return r1807800;
}

Error

Bits error versus x

Target

Original39.8
Target39.3
Herbie0.6
\[\frac{1}{1 - e^{-x}}\]

Derivation

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

    1. Initial program 0.0

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

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

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

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

    if -0.002140507652831692 < x

    1. Initial program 60.1

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

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

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

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

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

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

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