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

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

\end{array}
double f(double a, double x) {
        double r40795 = a;
        double r40796 = x;
        double r40797 = r40795 * r40796;
        double r40798 = exp(r40797);
        double r40799 = 1.0;
        double r40800 = r40798 - r40799;
        return r40800;
}

double f(double a, double x) {
        double r40801 = a;
        double r40802 = x;
        double r40803 = r40801 * r40802;
        double r40804 = -0.00591504720525442;
        bool r40805 = r40803 <= r40804;
        double r40806 = 2.0;
        double r40807 = r40806 * r40803;
        double r40808 = exp(r40807);
        double r40809 = 1.0;
        double r40810 = r40809 * r40809;
        double r40811 = r40808 - r40810;
        double r40812 = exp(r40803);
        double r40813 = r40812 + r40809;
        double r40814 = r40811 / r40813;
        double r40815 = r40801 * r40803;
        double r40816 = 0.16666666666666666;
        double r40817 = r40816 * r40802;
        double r40818 = 0.5;
        double r40819 = fma(r40817, r40801, r40818);
        double r40820 = r40815 * r40819;
        double r40821 = r40802 * r40820;
        double r40822 = r40802 * r40801;
        double r40823 = r40821 + r40822;
        double r40824 = log1p(r40823);
        double r40825 = expm1(r40824);
        double r40826 = r40805 ? r40814 : r40825;
        return r40826;
}

Error

Bits error versus a

Bits error versus x

Target

Original29.6
Target0.2
Herbie0.3
\[\begin{array}{l} \mathbf{if}\;\left|a \cdot x\right| \lt 0.1000000000000000055511151231257827021182:\\ \;\;\;\;\left(a \cdot x\right) \cdot \left(1 + \left(\frac{a \cdot x}{2} + \frac{{\left(a \cdot x\right)}^{2}}{6}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;e^{a \cdot x} - 1\\ \end{array}\]

Derivation

  1. Split input into 2 regimes
  2. if (* a x) < -0.00591504720525442

    1. Initial program 0.0

      \[e^{a \cdot x} - 1\]
    2. Using strategy rm
    3. Applied flip--0.0

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

      \[\leadsto \frac{\color{blue}{e^{2 \cdot \left(a \cdot x\right)} - 1 \cdot 1}}{e^{a \cdot x} + 1}\]

    if -0.00591504720525442 < (* a x)

    1. Initial program 44.8

      \[e^{a \cdot x} - 1\]
    2. Taylor expanded around 0 14.7

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

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

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left({x}^{2}, \mathsf{fma}\left(x, \frac{1}{6} \cdot {a}^{3}, \frac{1}{2} \cdot {a}^{2}\right), a \cdot x\right)\right)\right)}\]
    6. Simplified4.7

      \[\leadsto \mathsf{expm1}\left(\color{blue}{\mathsf{log1p}\left(x \cdot \left(\left(\left(a \cdot a\right) \cdot \left(\left(x \cdot \frac{1}{6}\right) \cdot a + \frac{1}{2}\right)\right) \cdot x + a\right)\right)}\right)\]
    7. Using strategy rm
    8. Applied distribute-lft-in4.7

      \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{x \cdot \left(\left(\left(a \cdot a\right) \cdot \left(\left(x \cdot \frac{1}{6}\right) \cdot a + \frac{1}{2}\right)\right) \cdot x\right) + x \cdot a}\right)\right)\]
    9. Simplified4.7

      \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\left(x \cdot \left(\mathsf{fma}\left(\frac{1}{6} \cdot x, a, \frac{1}{2}\right) \cdot \left(a \cdot a\right)\right)\right) \cdot x} + x \cdot a\right)\right)\]
    10. Using strategy rm
    11. Applied associate-*l*4.7

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

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

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

Reproduce

herbie shell --seed 2019212 +o rules:numerics
(FPCore (a x)
  :name "expax (section 3.5)"
  :precision binary64
  :herbie-expected 14

  :herbie-target
  (if (< (fabs (* a x)) 0.10000000000000001) (* (* a x) (+ 1 (+ (/ (* a x) 2) (/ (pow (* a x) 2) 6)))) (- (exp (* a x)) 1))

  (- (exp (* a x)) 1))