Average Error: 58.1 → 1.0
Time: 8.6s
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}:\\ \;\;\;\;\left(x + \frac{2}{15} \cdot {x}^{5}\right) - \frac{1}{3} \cdot {x}^{3}\\ \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}:\\
\;\;\;\;\left(x + \frac{2}{15} \cdot {x}^{5}\right) - \frac{1}{3} \cdot {x}^{3}\\

\end{array}
double f(double x) {
        double r42449 = x;
        double r42450 = exp(r42449);
        double r42451 = -r42449;
        double r42452 = exp(r42451);
        double r42453 = r42450 - r42452;
        double r42454 = r42450 + r42452;
        double r42455 = r42453 / r42454;
        return r42455;
}

double f(double x) {
        double r42456 = x;
        double r42457 = -0.005609947366707223;
        bool r42458 = r42456 <= r42457;
        double r42459 = tanh(r42456);
        double r42460 = exp(r42459);
        double r42461 = log(r42460);
        double r42462 = 0.13333333333333333;
        double r42463 = 5.0;
        double r42464 = pow(r42456, r42463);
        double r42465 = r42462 * r42464;
        double r42466 = r42456 + r42465;
        double r42467 = 0.3333333333333333;
        double r42468 = 3.0;
        double r42469 = pow(r42456, r42468);
        double r42470 = r42467 * r42469;
        double r42471 = r42466 - r42470;
        double r42472 = r42458 ? r42461 : r42471;
        return r42472;
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

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. Taylor expanded around 0 1.0

      \[\leadsto \color{blue}{\left(x + \frac{2}{15} \cdot {x}^{5}\right) - \frac{1}{3} \cdot {x}^{3}}\]
  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}:\\ \;\;\;\;\left(x + \frac{2}{15} \cdot {x}^{5}\right) - \frac{1}{3} \cdot {x}^{3}\\ \end{array}\]

Reproduce

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