Hyperbolic tangent

?

Percentage Accurate: 9.2% → 98.2%
Time: 6.2s
Precision: binary64
Cost: 13636

?

\[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -1.25:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;-0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right)\\ \end{array} \]
(FPCore (x)
 :precision binary64
 (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))
(FPCore (x)
 :precision binary64
 (if (<= x -1.25)
   -1.0
   (+
    (* -0.3333333333333333 (pow x 3.0))
    (+ x (* 0.13333333333333333 (pow x 5.0))))))
double code(double x) {
	return (exp(x) - exp(-x)) / (exp(x) + exp(-x));
}
double code(double x) {
	double tmp;
	if (x <= -1.25) {
		tmp = -1.0;
	} else {
		tmp = (-0.3333333333333333 * pow(x, 3.0)) + (x + (0.13333333333333333 * pow(x, 5.0)));
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
end function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-1.25d0)) then
        tmp = -1.0d0
    else
        tmp = ((-0.3333333333333333d0) * (x ** 3.0d0)) + (x + (0.13333333333333333d0 * (x ** 5.0d0)))
    end if
    code = tmp
end function
public static double code(double x) {
	return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
}
public static double code(double x) {
	double tmp;
	if (x <= -1.25) {
		tmp = -1.0;
	} else {
		tmp = (-0.3333333333333333 * Math.pow(x, 3.0)) + (x + (0.13333333333333333 * Math.pow(x, 5.0)));
	}
	return tmp;
}
def code(x):
	return (math.exp(x) - math.exp(-x)) / (math.exp(x) + math.exp(-x))
def code(x):
	tmp = 0
	if x <= -1.25:
		tmp = -1.0
	else:
		tmp = (-0.3333333333333333 * math.pow(x, 3.0)) + (x + (0.13333333333333333 * math.pow(x, 5.0)))
	return tmp
function code(x)
	return Float64(Float64(exp(x) - exp(Float64(-x))) / Float64(exp(x) + exp(Float64(-x))))
end
function code(x)
	tmp = 0.0
	if (x <= -1.25)
		tmp = -1.0;
	else
		tmp = Float64(Float64(-0.3333333333333333 * (x ^ 3.0)) + Float64(x + Float64(0.13333333333333333 * (x ^ 5.0))));
	end
	return tmp
end
function tmp = code(x)
	tmp = (exp(x) - exp(-x)) / (exp(x) + exp(-x));
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -1.25)
		tmp = -1.0;
	else
		tmp = (-0.3333333333333333 * (x ^ 3.0)) + (x + (0.13333333333333333 * (x ^ 5.0)));
	end
	tmp_2 = 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_] := If[LessEqual[x, -1.25], -1.0, N[(N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x + N[(0.13333333333333333 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
\begin{array}{l}
\mathbf{if}\;x \leq -1.25:\\
\;\;\;\;-1\\

\mathbf{else}:\\
\;\;\;\;-0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right)\\


\end{array}

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 7 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

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

    1. Initial program 33.3%

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
    2. Taylor expanded in x around 0 3.9%

      \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{2 + {x}^{2}}} \]
    3. Simplified3.9%

      \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{2 + x \cdot x}} \]
      Step-by-step derivation

      [Start]3.9

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

      unpow2 [=>]3.9

      \[ \frac{e^{x} - e^{-x}}{2 + \color{blue}{x \cdot x}} \]
    4. Applied egg-rr100.0%

      \[\leadsto \color{blue}{-1} \]

    if -1.25 < x

    1. Initial program 8.6%

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
    2. Taylor expanded in x around 0 98.8%

      \[\leadsto \color{blue}{-0.3333333333333333 \cdot {x}^{3} + \left(0.13333333333333333 \cdot {x}^{5} + x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.25:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;-0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.2%
Cost7428
\[\begin{array}{l} \mathbf{if}\;x \leq -1.6:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot 2 + {x}^{3} \cdot 0.3333333333333333}{2 + x \cdot x}\\ \end{array} \]
Alternative 2
Accuracy98.0%
Cost6916
\[\begin{array}{l} \mathbf{if}\;x \leq -1.15:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \end{array} \]
Alternative 3
Accuracy97.7%
Cost708
\[\begin{array}{l} \mathbf{if}\;x \leq -1.25:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\frac{x + x}{2 + x \cdot x}\\ \end{array} \]
Alternative 4
Accuracy7.8%
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{-312}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 5
Accuracy97.6%
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 6
Accuracy5.0%
Cost64
\[-1 \]

Reproduce?

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