Average Error: 58.0 → 0.9
Time: 16.9s
Precision: 64
\[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\]
\[\begin{array}{l} \mathbf{if}\;x \le 0.003206596894967449901114875387975189369172:\\ \;\;\;\;\left(x + \frac{2}{15} \cdot {x}^{5}\right) - \frac{1}{3} \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;e^{\log \left(\tanh x\right)}\\ \end{array}\]
\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
\begin{array}{l}
\mathbf{if}\;x \le 0.003206596894967449901114875387975189369172:\\
\;\;\;\;\left(x + \frac{2}{15} \cdot {x}^{5}\right) - \frac{1}{3} \cdot {x}^{3}\\

\mathbf{else}:\\
\;\;\;\;e^{\log \left(\tanh x\right)}\\

\end{array}
double f(double x) {
        double r47912 = x;
        double r47913 = exp(r47912);
        double r47914 = -r47912;
        double r47915 = exp(r47914);
        double r47916 = r47913 - r47915;
        double r47917 = r47913 + r47915;
        double r47918 = r47916 / r47917;
        return r47918;
}

double f(double x) {
        double r47919 = x;
        double r47920 = 0.00320659689496745;
        bool r47921 = r47919 <= r47920;
        double r47922 = 0.13333333333333333;
        double r47923 = 5.0;
        double r47924 = pow(r47919, r47923);
        double r47925 = r47922 * r47924;
        double r47926 = r47919 + r47925;
        double r47927 = 0.3333333333333333;
        double r47928 = 3.0;
        double r47929 = pow(r47919, r47928);
        double r47930 = r47927 * r47929;
        double r47931 = r47926 - r47930;
        double r47932 = tanh(r47919);
        double r47933 = log(r47932);
        double r47934 = exp(r47933);
        double r47935 = r47921 ? r47931 : r47934;
        return r47935;
}

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.00320659689496745

    1. Initial program 58.4

      \[\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}}\]

    if 0.00320659689496745 < x

    1. Initial program 34.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. Using strategy rm
    5. Applied add-exp-log0.2

      \[\leadsto \color{blue}{e^{\log \left(\tanh x\right)}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.9

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

Reproduce

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