Average Error: 41.1 → 0.9
Time: 18.9s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \le 1.166841640166349666900712269954744631923 \cdot 10^{-47}:\\ \;\;\;\;\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 1.166841640166349666900712269954744631923 \cdot 10^{-47}:\\
\;\;\;\;\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 r68113 = x;
        double r68114 = exp(r68113);
        double r68115 = 1.0;
        double r68116 = r68114 - r68115;
        double r68117 = r68114 / r68116;
        return r68117;
}

double f(double x) {
        double r68118 = x;
        double r68119 = exp(r68118);
        double r68120 = 1.1668416401663497e-47;
        bool r68121 = r68119 <= r68120;
        double r68122 = 1.0;
        double r68123 = 1.0;
        double r68124 = r68123 / r68119;
        double r68125 = r68122 - r68124;
        double r68126 = r68122 / r68125;
        double r68127 = 0.08333333333333333;
        double r68128 = r68122 / r68118;
        double r68129 = fma(r68127, r68118, r68128);
        double r68130 = 0.5;
        double r68131 = r68129 + r68130;
        double r68132 = r68121 ? r68126 : r68131;
        return r68132;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if (exp x) < 1.1668416401663497e-47

    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}}}}\]

    if 1.1668416401663497e-47 < (exp x)

    1. Initial program 61.5

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

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

      \[\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 1.166841640166349666900712269954744631923 \cdot 10^{-47}:\\ \;\;\;\;\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 2019322 +o rules:numerics
(FPCore (x)
  :name "expq2 (section 3.11)"
  :precision binary64

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

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