Average Error: 41.5 → 0.8
Time: 12.0s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \le 0.0:\\ \;\;\;\;\frac{e^{x}}{\log \left(e^{e^{x} - 1}\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} + e^{\log \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(x, \frac{1}{12}, \frac{1}{2}\right)\right)\right)\right)}\\ \end{array}\]
\frac{e^{x}}{e^{x} - 1}
\begin{array}{l}
\mathbf{if}\;e^{x} \le 0.0:\\
\;\;\;\;\frac{e^{x}}{\log \left(e^{e^{x} - 1}\right)}\\

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

\end{array}
double f(double x) {
        double r3058453 = x;
        double r3058454 = exp(r3058453);
        double r3058455 = 1.0;
        double r3058456 = r3058454 - r3058455;
        double r3058457 = r3058454 / r3058456;
        return r3058457;
}

double f(double x) {
        double r3058458 = x;
        double r3058459 = exp(r3058458);
        double r3058460 = 0.0;
        bool r3058461 = r3058459 <= r3058460;
        double r3058462 = 1.0;
        double r3058463 = r3058459 - r3058462;
        double r3058464 = exp(r3058463);
        double r3058465 = log(r3058464);
        double r3058466 = r3058459 / r3058465;
        double r3058467 = 1.0;
        double r3058468 = r3058467 / r3058458;
        double r3058469 = 0.08333333333333333;
        double r3058470 = 0.5;
        double r3058471 = fma(r3058458, r3058469, r3058470);
        double r3058472 = log1p(r3058471);
        double r3058473 = expm1(r3058472);
        double r3058474 = log(r3058473);
        double r3058475 = exp(r3058474);
        double r3058476 = r3058468 + r3058475;
        double r3058477 = r3058461 ? r3058466 : r3058476;
        return r3058477;
}

Error

Bits error versus x

Target

Original41.5
Target41.2
Herbie0.8
\[\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 add-log-exp0

      \[\leadsto \frac{e^{x}}{e^{x} - \color{blue}{\log \left(e^{1}\right)}}\]
    4. Applied add-log-exp0

      \[\leadsto \frac{e^{x}}{\color{blue}{\log \left(e^{e^{x}}\right)} - \log \left(e^{1}\right)}\]
    5. Applied diff-log0

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

      \[\leadsto \frac{e^{x}}{\log \color{blue}{\left(e^{e^{x} - 1}\right)}}\]

    if 0.0 < (exp x)

    1. Initial program 61.6

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right) + \frac{1}{x}}\]
    4. Using strategy rm
    5. Applied add-sqr-sqrt1.1

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)} \cdot \sqrt{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)}} + \frac{1}{x}\]
    6. Using strategy rm
    7. Applied add-exp-log1.1

      \[\leadsto \sqrt{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)} \cdot \color{blue}{e^{\log \left(\sqrt{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)}\right)}} + \frac{1}{x}\]
    8. Applied add-exp-log1.1

      \[\leadsto \color{blue}{e^{\log \left(\sqrt{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)}\right)}} \cdot e^{\log \left(\sqrt{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)}\right)} + \frac{1}{x}\]
    9. Applied prod-exp1.1

      \[\leadsto \color{blue}{e^{\log \left(\sqrt{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)}\right) + \log \left(\sqrt{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{2}\right)}\right)}} + \frac{1}{x}\]
    10. Simplified1.1

      \[\leadsto e^{\color{blue}{\log \left(\mathsf{fma}\left(x, \frac{1}{12}, \frac{1}{2}\right)\right)}} + \frac{1}{x}\]
    11. Using strategy rm
    12. Applied expm1-log1p-u1.1

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

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

Reproduce

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

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

  (/ (exp x) (- (exp x) 1.0)))