Average Error: 41.5 → 0.1
Time: 3.0s
Precision: 64
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \le 0.88704448357346632:\\ \;\;\;\;\frac{e^{x}}{e^{x} - 1}\\ \mathbf{elif}\;e^{x} \le 1.00000012358749735:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{x}\right) + \frac{1}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{1 - \frac{1}{e^{x}}}\\ \end{array}\]
\frac{e^{x}}{e^{x} - 1}
\begin{array}{l}
\mathbf{if}\;e^{x} \le 0.88704448357346632:\\
\;\;\;\;\frac{e^{x}}{e^{x} - 1}\\

\mathbf{elif}\;e^{x} \le 1.00000012358749735:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{x}\right) + \frac{1}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{1 - \frac{1}{e^{x}}}\\

\end{array}
double f(double x) {
        double r89807 = x;
        double r89808 = exp(r89807);
        double r89809 = 1.0;
        double r89810 = r89808 - r89809;
        double r89811 = r89808 / r89810;
        return r89811;
}

double f(double x) {
        double r89812 = x;
        double r89813 = exp(r89812);
        double r89814 = 0.8870444835734663;
        bool r89815 = r89813 <= r89814;
        double r89816 = 1.0;
        double r89817 = r89813 - r89816;
        double r89818 = r89813 / r89817;
        double r89819 = 1.0000001235874973;
        bool r89820 = r89813 <= r89819;
        double r89821 = 0.08333333333333333;
        double r89822 = 1.0;
        double r89823 = r89822 / r89812;
        double r89824 = fma(r89821, r89812, r89823);
        double r89825 = 0.5;
        double r89826 = r89824 + r89825;
        double r89827 = r89816 / r89813;
        double r89828 = r89822 - r89827;
        double r89829 = r89822 / r89828;
        double r89830 = r89820 ? r89826 : r89829;
        double r89831 = r89815 ? r89818 : r89830;
        return r89831;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if (exp x) < 0.8870444835734663

    1. Initial program 0.0

      \[\frac{e^{x}}{e^{x} - 1}\]

    if 0.8870444835734663 < (exp x) < 1.0000001235874973

    1. Initial program 62.7

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{12}, x, \frac{1}{x}\right) + \frac{1}{2}}\]

    if 1.0000001235874973 < (exp x)

    1. Initial program 34.3

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

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

      \[\leadsto \frac{1}{\color{blue}{1 - \frac{1}{e^{x}}}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.1

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

Reproduce

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

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

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