Average Error: 39.8 → 0.6
Time: 27.8s
Precision: 64
\[\frac{e^{x} - 1}{x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{e^{x} - 1}{x} \le 0.0:\\ \;\;\;\;\sqrt[3]{\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right) \cdot \left(\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right) \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{x}}{x} - \frac{1}{x}\\ \end{array}\]
\frac{e^{x} - 1}{x}
\begin{array}{l}
\mathbf{if}\;\frac{e^{x} - 1}{x} \le 0.0:\\
\;\;\;\;\sqrt[3]{\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right) \cdot \left(\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right) \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right)\right)}\\

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

\end{array}
double f(double x) {
        double r3397837 = x;
        double r3397838 = exp(r3397837);
        double r3397839 = 1.0;
        double r3397840 = r3397838 - r3397839;
        double r3397841 = r3397840 / r3397837;
        return r3397841;
}

double f(double x) {
        double r3397842 = x;
        double r3397843 = exp(r3397842);
        double r3397844 = 1.0;
        double r3397845 = r3397843 - r3397844;
        double r3397846 = r3397845 / r3397842;
        double r3397847 = 0.0;
        bool r3397848 = r3397846 <= r3397847;
        double r3397849 = 0.16666666666666666;
        double r3397850 = 0.5;
        double r3397851 = fma(r3397849, r3397842, r3397850);
        double r3397852 = 1.0;
        double r3397853 = fma(r3397842, r3397851, r3397852);
        double r3397854 = r3397853 * r3397853;
        double r3397855 = r3397853 * r3397854;
        double r3397856 = cbrt(r3397855);
        double r3397857 = r3397843 / r3397842;
        double r3397858 = r3397844 / r3397842;
        double r3397859 = r3397857 - r3397858;
        double r3397860 = r3397848 ? r3397856 : r3397859;
        return r3397860;
}

Error

Bits error versus x

Target

Original39.8
Target40.0
Herbie0.6
\[\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 (/ (- (exp x) 1.0) x) < 0.0

    1. Initial program 62.0

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

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

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

      \[\leadsto \color{blue}{\sqrt[3]{\left(\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right) \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right)\right) \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(\frac{1}{6}, x, \frac{1}{2}\right), 1\right)}}\]

    if 0.0 < (/ (- (exp x) 1.0) x)

    1. Initial program 2.7

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

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

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

Reproduce

herbie shell --seed 2019200 +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))