Average Error: 41.5 → 0.8
Time: 13.1s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \le 3.936159656535852086796232633999635795103 \cdot 10^{-90}:\\ \;\;\;\;\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}\;e^{x} \le 3.936159656535852086796232633999635795103 \cdot 10^{-90}:\\
\;\;\;\;\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 r155922 = x;
        double r155923 = exp(r155922);
        double r155924 = 1.0;
        double r155925 = r155923 - r155924;
        double r155926 = r155923 / r155925;
        return r155926;
}

double f(double x) {
        double r155927 = x;
        double r155928 = exp(r155927);
        double r155929 = 3.936159656535852e-90;
        bool r155930 = r155928 <= r155929;
        double r155931 = 1.0;
        double r155932 = 1.0;
        double r155933 = r155932 / r155928;
        double r155934 = r155931 - r155933;
        double r155935 = r155931 / r155934;
        double r155936 = r155931 / r155927;
        double r155937 = 0.08333333333333333;
        double r155938 = 0.5;
        double r155939 = fma(r155937, r155927, r155938);
        double r155940 = r155936 + r155939;
        double r155941 = r155930 ? r155935 : r155940;
        return r155941;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if (exp x) < 3.936159656535852e-90

    1. Initial program 0

      \[\frac{e^{x}}{e^{x} - 1}\]
    2. Using strategy rm
    3. Applied clear-num0

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

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

    if 3.936159656535852e-90 < (exp x)

    1. Initial program 61.6

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{x} \le 3.936159656535852086796232633999635795103 \cdot 10^{-90}:\\ \;\;\;\;\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 2019194 +o rules:numerics
(FPCore (x)
  :name "expq2 (section 3.11)"

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

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