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

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

\end{array}
double f(double x) {
        double r93909 = x;
        double r93910 = exp(r93909);
        double r93911 = 1.0;
        double r93912 = r93910 - r93911;
        double r93913 = r93912 / r93909;
        return r93913;
}

double f(double x) {
        double r93914 = x;
        double r93915 = -0.0001767842498543489;
        bool r93916 = r93914 <= r93915;
        double r93917 = exp(r93914);
        double r93918 = 1.0;
        double r93919 = r93917 - r93918;
        double r93920 = r93919 / r93914;
        double r93921 = 2.0;
        double r93922 = pow(r93914, r93921);
        double r93923 = 0.16666666666666666;
        double r93924 = 0.5;
        double r93925 = fma(r93914, r93923, r93924);
        double r93926 = fma(r93922, r93925, r93914);
        double r93927 = r93926 / r93914;
        double r93928 = r93916 ? r93920 : r93927;
        return r93928;
}

Error

Bits error versus x

Target

Original39.8
Target40.4
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.0001767842498543489

    1. Initial program 0.0

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

    if -0.0001767842498543489 < x

    1. Initial program 60.4

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

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

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

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

Reproduce

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