Average Error: 39.8 → 0.6
Time: 25.9s
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 r3305609 = x;
        double r3305610 = exp(r3305609);
        double r3305611 = 1.0;
        double r3305612 = r3305610 - r3305611;
        double r3305613 = r3305612 / r3305609;
        return r3305613;
}

double f(double x) {
        double r3305614 = x;
        double r3305615 = exp(r3305614);
        double r3305616 = 1.0;
        double r3305617 = r3305615 - r3305616;
        double r3305618 = r3305617 / r3305614;
        double r3305619 = 0.0;
        bool r3305620 = r3305618 <= r3305619;
        double r3305621 = 0.16666666666666666;
        double r3305622 = 0.5;
        double r3305623 = fma(r3305621, r3305614, r3305622);
        double r3305624 = 1.0;
        double r3305625 = fma(r3305614, r3305623, r3305624);
        double r3305626 = r3305625 * r3305625;
        double r3305627 = r3305625 * r3305626;
        double r3305628 = cbrt(r3305627);
        double r3305629 = r3305615 / r3305614;
        double r3305630 = r3305616 / r3305614;
        double r3305631 = r3305629 - r3305630;
        double r3305632 = r3305620 ? r3305628 : r3305631;
        return r3305632;
}

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))