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

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

\end{array}
double f(double x) {
        double r68894 = x;
        double r68895 = exp(r68894);
        double r68896 = 1.0;
        double r68897 = r68895 - r68896;
        double r68898 = r68897 / r68894;
        return r68898;
}

double f(double x) {
        double r68899 = x;
        double r68900 = -0.0001506654427850415;
        bool r68901 = r68899 <= r68900;
        double r68902 = r68899 + r68899;
        double r68903 = exp(r68902);
        double r68904 = 1.0;
        double r68905 = r68904 * r68904;
        double r68906 = r68903 - r68905;
        double r68907 = exp(r68899);
        double r68908 = r68907 + r68904;
        double r68909 = r68906 / r68908;
        double r68910 = r68909 / r68899;
        double r68911 = 0.16666666666666666;
        double r68912 = 0.5;
        double r68913 = fma(r68899, r68911, r68912);
        double r68914 = 1.0;
        double r68915 = fma(r68899, r68913, r68914);
        double r68916 = expm1(r68915);
        double r68917 = log1p(r68916);
        double r68918 = r68901 ? r68910 : r68917;
        return r68918;
}

Error

Bits error versus x

Target

Original40.1
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.0001506654427850415

    1. Initial program 0.1

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

      \[\leadsto \frac{\color{blue}{\frac{e^{x} \cdot e^{x} - 1 \cdot 1}{e^{x} + 1}}}{x}\]
    4. Simplified0.0

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

    if -0.0001506654427850415 < x

    1. Initial program 60.2

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

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

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\mathsf{fma}\left(\frac{1}{6}, x \cdot x, \mathsf{fma}\left(\frac{1}{2}, x, 1\right)\right)\right)\right)}\]
    6. Simplified0.4

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

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

Reproduce

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