Average Error: 58.1 → 1.0
Time: 23.9s
Precision: 64
\[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.005609947366707223291582007362876538536511:\\ \;\;\;\;\log \left(e^{\tanh x}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left({x}^{3}, \frac{-1}{3}, \mathsf{fma}\left(\frac{2}{15}, {x}^{5}, x\right)\right)\\ \end{array}\]
\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
\begin{array}{l}
\mathbf{if}\;x \le -0.005609947366707223291582007362876538536511:\\
\;\;\;\;\log \left(e^{\tanh x}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left({x}^{3}, \frac{-1}{3}, \mathsf{fma}\left(\frac{2}{15}, {x}^{5}, x\right)\right)\\

\end{array}
double f(double x) {
        double r51546 = x;
        double r51547 = exp(r51546);
        double r51548 = -r51546;
        double r51549 = exp(r51548);
        double r51550 = r51547 - r51549;
        double r51551 = r51547 + r51549;
        double r51552 = r51550 / r51551;
        return r51552;
}

double f(double x) {
        double r51553 = x;
        double r51554 = -0.005609947366707223;
        bool r51555 = r51553 <= r51554;
        double r51556 = tanh(r51553);
        double r51557 = exp(r51556);
        double r51558 = log(r51557);
        double r51559 = 3.0;
        double r51560 = pow(r51553, r51559);
        double r51561 = -0.3333333333333333;
        double r51562 = 0.13333333333333333;
        double r51563 = 5.0;
        double r51564 = pow(r51553, r51563);
        double r51565 = fma(r51562, r51564, r51553);
        double r51566 = fma(r51560, r51561, r51565);
        double r51567 = r51555 ? r51558 : r51566;
        return r51567;
}

Error

Bits error versus x

Derivation

  1. Split input into 2 regimes
  2. if x < -0.005609947366707223

    1. Initial program 36.1

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\]
    2. Using strategy rm
    3. Applied tanh-undef0.1

      \[\leadsto \color{blue}{\tanh x}\]
    4. Using strategy rm
    5. Applied add-log-exp0.5

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

    if -0.005609947366707223 < x

    1. Initial program 58.6

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\]
    2. Using strategy rm
    3. Applied tanh-undef0.0

      \[\leadsto \color{blue}{\tanh x}\]
    4. Taylor expanded around 0 1.0

      \[\leadsto \color{blue}{\left(x + \frac{2}{15} \cdot {x}^{5}\right) - \frac{1}{3} \cdot {x}^{3}}\]
    5. Simplified1.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.005609947366707223291582007362876538536511:\\ \;\;\;\;\log \left(e^{\tanh x}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left({x}^{3}, \frac{-1}{3}, \mathsf{fma}\left(\frac{2}{15}, {x}^{5}, x\right)\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019322 +o rules:numerics
(FPCore (x)
  :name "Hyperbolic tangent"
  :precision binary64
  (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))