Average Error: 58.1 → 0.2
Time: 4.2s
Precision: binary64
\[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
\[\begin{array}{l} t_0 := -1 + \frac{2}{1 + {\left(e^{-2}\right)}^{x}}\\ \mathbf{if}\;x \leq -15.59074868643072:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.0010185916314394518:\\ \;\;\;\;\mathsf{fma}\left(-0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, \mathsf{fma}\left(-0.05396825396825397, {x}^{7}, x\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (x)
 :precision binary64
 (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (+ -1.0 (/ 2.0 (+ 1.0 (pow (exp -2.0) x))))))
   (if (<= x -15.59074868643072)
     t_0
     (if (<= x 0.0010185916314394518)
       (fma
        -0.3333333333333333
        (pow x 3.0)
        (fma
         0.13333333333333333
         (pow x 5.0)
         (fma -0.05396825396825397 (pow x 7.0) x)))
       t_0))))
double code(double x) {
	return (exp(x) - exp(-x)) / (exp(x) + exp(-x));
}
double code(double x) {
	double t_0 = -1.0 + (2.0 / (1.0 + pow(exp(-2.0), x)));
	double tmp;
	if (x <= -15.59074868643072) {
		tmp = t_0;
	} else if (x <= 0.0010185916314394518) {
		tmp = fma(-0.3333333333333333, pow(x, 3.0), fma(0.13333333333333333, pow(x, 5.0), fma(-0.05396825396825397, pow(x, 7.0), x)));
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x)
	return Float64(Float64(exp(x) - exp(Float64(-x))) / Float64(exp(x) + exp(Float64(-x))))
end
function code(x)
	t_0 = Float64(-1.0 + Float64(2.0 / Float64(1.0 + (exp(-2.0) ^ x))))
	tmp = 0.0
	if (x <= -15.59074868643072)
		tmp = t_0;
	elseif (x <= 0.0010185916314394518)
		tmp = fma(-0.3333333333333333, (x ^ 3.0), fma(0.13333333333333333, (x ^ 5.0), fma(-0.05396825396825397, (x ^ 7.0), x)));
	else
		tmp = t_0;
	end
	return tmp
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(-1.0 + N[(2.0 / N[(1.0 + N[Power[N[Exp[-2.0], $MachinePrecision], x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -15.59074868643072], t$95$0, If[LessEqual[x, 0.0010185916314394518], N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision] + N[(0.13333333333333333 * N[Power[x, 5.0], $MachinePrecision] + N[(-0.05396825396825397 * N[Power[x, 7.0], $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
\begin{array}{l}
t_0 := -1 + \frac{2}{1 + {\left(e^{-2}\right)}^{x}}\\
\mathbf{if}\;x \leq -15.59074868643072:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq 0.0010185916314394518:\\
\;\;\;\;\mathsf{fma}\left(-0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, \mathsf{fma}\left(-0.05396825396825397, {x}^{7}, x\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error

Derivation

  1. Split input into 2 regimes
  2. if x < -15.5907486864307199 or 0.00101859163143945184 < x

    1. Initial program 39.2

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
    2. Simplified0.8

      \[\leadsto \color{blue}{-1 + \frac{2}{1 + \frac{e^{-x}}{e^{x}}}} \]
    3. Taylor expanded in x around inf 0.8

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

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

    if -15.5907486864307199 < x < 0.00101859163143945184

    1. Initial program 58.7

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
    2. Simplified58.8

      \[\leadsto \color{blue}{-1 + \frac{2}{1 + \frac{e^{-x}}{e^{x}}}} \]
    3. Taylor expanded in x around 0 0.2

      \[\leadsto \color{blue}{-0.05396825396825397 \cdot {x}^{7} + \left(-0.3333333333333333 \cdot {x}^{3} + \left(0.13333333333333333 \cdot {x}^{5} + x\right)\right)} \]
    4. Simplified0.2

      \[\leadsto \color{blue}{\mathsf{fma}\left(-0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, \mathsf{fma}\left(-0.05396825396825397, {x}^{7}, x\right)\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -15.59074868643072:\\ \;\;\;\;-1 + \frac{2}{1 + {\left(e^{-2}\right)}^{x}}\\ \mathbf{elif}\;x \leq 0.0010185916314394518:\\ \;\;\;\;\mathsf{fma}\left(-0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, \mathsf{fma}\left(-0.05396825396825397, {x}^{7}, x\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{2}{1 + {\left(e^{-2}\right)}^{x}}\\ \end{array} \]

Reproduce

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