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

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

\end{array}
double f(double x) {
        double r75104 = x;
        double r75105 = exp(r75104);
        double r75106 = 1.0;
        double r75107 = r75105 - r75106;
        double r75108 = r75107 / r75104;
        return r75108;
}

double f(double x) {
        double r75109 = x;
        double r75110 = -0.00019141971874233585;
        bool r75111 = r75109 <= r75110;
        double r75112 = exp(r75109);
        double r75113 = 1.0;
        double r75114 = r75112 - r75113;
        double r75115 = exp(r75114);
        double r75116 = log(r75115);
        double r75117 = r75116 / r75109;
        double r75118 = 0.16666666666666666;
        double r75119 = 2.0;
        double r75120 = pow(r75109, r75119);
        double r75121 = 0.5;
        double r75122 = 1.0;
        double r75123 = fma(r75121, r75109, r75122);
        double r75124 = fma(r75118, r75120, r75123);
        double r75125 = r75111 ? r75117 : r75124;
        return r75125;
}

Error

Bits error versus x

Target

Original39.8
Target40.2
Herbie0.4
\[\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.00019141971874233585

    1. Initial program 0.0

      \[\frac{e^{x} - 1}{x}\]
    2. Using strategy rm
    3. Applied add-log-exp0.0

      \[\leadsto \frac{e^{x} - \color{blue}{\log \left(e^{1}\right)}}{x}\]
    4. Applied add-log-exp0.1

      \[\leadsto \frac{\color{blue}{\log \left(e^{e^{x}}\right)} - \log \left(e^{1}\right)}{x}\]
    5. Applied diff-log0.1

      \[\leadsto \frac{\color{blue}{\log \left(\frac{e^{e^{x}}}{e^{1}}\right)}}{x}\]
    6. Simplified0.1

      \[\leadsto \frac{\log \color{blue}{\left(e^{e^{x} - 1}\right)}}{x}\]

    if -0.00019141971874233585 < x

    1. Initial program 60.1

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

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

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

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

Reproduce

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

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

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