Average Error: 40.5 → 0.3
Time: 12.8s
Precision: 64
\[\frac{e^{x} - 1}{x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.629133836907384867438014497409426439845 \cdot 10^{-4}:\\ \;\;\;\;\frac{e^{x}}{x} - \frac{1}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x \cdot x, \frac{1}{2}, \mathsf{fma}\left(\frac{1}{6}, x \cdot \left(x \cdot x\right), x\right)\right)}{x}\\ \end{array}\]
\frac{e^{x} - 1}{x}
\begin{array}{l}
\mathbf{if}\;x \le -1.629133836907384867438014497409426439845 \cdot 10^{-4}:\\
\;\;\;\;\frac{e^{x}}{x} - \frac{1}{x}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x \cdot x, \frac{1}{2}, \mathsf{fma}\left(\frac{1}{6}, x \cdot \left(x \cdot x\right), x\right)\right)}{x}\\

\end{array}
double f(double x) {
        double r3862269 = x;
        double r3862270 = exp(r3862269);
        double r3862271 = 1.0;
        double r3862272 = r3862270 - r3862271;
        double r3862273 = r3862272 / r3862269;
        return r3862273;
}

double f(double x) {
        double r3862274 = x;
        double r3862275 = -0.0001629133836907385;
        bool r3862276 = r3862274 <= r3862275;
        double r3862277 = exp(r3862274);
        double r3862278 = r3862277 / r3862274;
        double r3862279 = 1.0;
        double r3862280 = r3862279 / r3862274;
        double r3862281 = r3862278 - r3862280;
        double r3862282 = r3862274 * r3862274;
        double r3862283 = 0.5;
        double r3862284 = 0.16666666666666666;
        double r3862285 = r3862274 * r3862282;
        double r3862286 = fma(r3862284, r3862285, r3862274);
        double r3862287 = fma(r3862282, r3862283, r3862286);
        double r3862288 = r3862287 / r3862274;
        double r3862289 = r3862276 ? r3862281 : r3862288;
        return r3862289;
}

Error

Bits error versus x

Target

Original40.5
Target40.8
Herbie0.3
\[\begin{array}{l} \mathbf{if}\;x \lt 1 \land x \gt -1:\\ \;\;\;\;\frac{e^{x} - 1}{\log \left(e^{x}\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{x} - 1}{x}\\ \end{array}\]

Derivation

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

    1. Initial program 0.1

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

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

    if -0.0001629133836907385 < x

    1. Initial program 60.1

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

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(x \cdot x, \frac{1}{2}, \mathsf{fma}\left(\frac{1}{6}, \left(x \cdot x\right) \cdot x, x\right)\right)}}{x}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -1.629133836907384867438014497409426439845 \cdot 10^{-4}:\\ \;\;\;\;\frac{e^{x}}{x} - \frac{1}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x \cdot x, \frac{1}{2}, \mathsf{fma}\left(\frac{1}{6}, x \cdot \left(x \cdot x\right), x\right)\right)}{x}\\ \end{array}\]

Reproduce

herbie shell --seed 2019192 +o rules:numerics
(FPCore (x)
  :name "Kahan's exp quotient"

  :herbie-target
  (if (and (< x 1.0) (> x -1.0)) (/ (- (exp x) 1.0) (log (exp x))) (/ (- (exp x) 1.0) x))

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