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

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

\end{array}
double f(double x) {
        double r56152 = x;
        double r56153 = exp(r56152);
        double r56154 = 1.0;
        double r56155 = r56153 - r56154;
        double r56156 = r56155 / r56152;
        return r56156;
}

double f(double x) {
        double r56157 = x;
        double r56158 = -0.0002022345445868293;
        bool r56159 = r56157 <= r56158;
        double r56160 = exp(r56157);
        double r56161 = sqrt(r56160);
        double r56162 = 1.0;
        double r56163 = -r56162;
        double r56164 = fma(r56161, r56161, r56163);
        double r56165 = r56164 / r56157;
        double r56166 = 0.16666666666666666;
        double r56167 = r56157 * r56157;
        double r56168 = 0.5;
        double r56169 = 1.0;
        double r56170 = fma(r56168, r56157, r56169);
        double r56171 = fma(r56166, r56167, r56170);
        double r56172 = r56159 ? r56165 : r56171;
        return r56172;
}

Error

Bits error versus x

Target

Original40.0
Target40.5
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.0002022345445868293

    1. Initial program 0.1

      \[\frac{e^{x} - 1}{x}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt0.1

      \[\leadsto \frac{\color{blue}{\sqrt{e^{x}} \cdot \sqrt{e^{x}}} - 1}{x}\]
    4. Applied fma-neg0.1

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{e^{x}}, \sqrt{e^{x}}, -1\right)}}{x}\]

    if -0.0002022345445868293 < x

    1. Initial program 60.2

      \[\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 \cdot x, \mathsf{fma}\left(\frac{1}{2}, x, 1\right)\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.3

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

Reproduce

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