Hyperbolic tangent

Percentage Accurate: 9.4% → 96.8%
Time: 3.5s
Alternatives: 3
Speedup: 409.0×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-x}\\ \frac{e^{x} - t_0}{e^{x} + t_0} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (exp (- x)))) (/ (- (exp x) t_0) (+ (exp x) t_0))))
double code(double x) {
	double t_0 = exp(-x);
	return (exp(x) - t_0) / (exp(x) + t_0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = exp(-x)
    code = (exp(x) - t_0) / (exp(x) + t_0)
end function
public static double code(double x) {
	double t_0 = Math.exp(-x);
	return (Math.exp(x) - t_0) / (Math.exp(x) + t_0);
}
def code(x):
	t_0 = math.exp(-x)
	return (math.exp(x) - t_0) / (math.exp(x) + t_0)
function code(x)
	t_0 = exp(Float64(-x))
	return Float64(Float64(exp(x) - t_0) / Float64(exp(x) + t_0))
end
function tmp = code(x)
	t_0 = exp(-x);
	tmp = (exp(x) - t_0) / (exp(x) + t_0);
end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-x}\\
\frac{e^{x} - t_0}{e^{x} + t_0}
\end{array}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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.

Accuracy vs Speed?

Herbie found 3 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 9.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-x}\\ \frac{e^{x} - t_0}{e^{x} + t_0} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (exp (- x)))) (/ (- (exp x) t_0) (+ (exp x) t_0))))
double code(double x) {
	double t_0 = exp(-x);
	return (exp(x) - t_0) / (exp(x) + t_0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = exp(-x)
    code = (exp(x) - t_0) / (exp(x) + t_0)
end function
public static double code(double x) {
	double t_0 = Math.exp(-x);
	return (Math.exp(x) - t_0) / (Math.exp(x) + t_0);
}
def code(x):
	t_0 = math.exp(-x)
	return (math.exp(x) - t_0) / (math.exp(x) + t_0)
function code(x)
	t_0 = exp(Float64(-x))
	return Float64(Float64(exp(x) - t_0) / Float64(exp(x) + t_0))
end
function tmp = code(x)
	t_0 = exp(-x);
	tmp = (exp(x) - t_0) / (exp(x) + t_0);
end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-x}\\
\frac{e^{x} - t_0}{e^{x} + t_0}
\end{array}
\end{array}

Alternative 1: 96.8% accurate, 1.9× speedup?

\[\begin{array}{l} \\ -0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (+
  (* -0.3333333333333333 (pow x 3.0))
  (+ x (* 0.13333333333333333 (pow x 5.0)))))
double code(double x) {
	return (-0.3333333333333333 * pow(x, 3.0)) + (x + (0.13333333333333333 * pow(x, 5.0)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((-0.3333333333333333d0) * (x ** 3.0d0)) + (x + (0.13333333333333333d0 * (x ** 5.0d0)))
end function
public static double code(double x) {
	return (-0.3333333333333333 * Math.pow(x, 3.0)) + (x + (0.13333333333333333 * Math.pow(x, 5.0)));
}
def code(x):
	return (-0.3333333333333333 * math.pow(x, 3.0)) + (x + (0.13333333333333333 * math.pow(x, 5.0)))
function code(x)
	return Float64(Float64(-0.3333333333333333 * (x ^ 3.0)) + Float64(x + Float64(0.13333333333333333 * (x ^ 5.0))))
end
function tmp = code(x)
	tmp = (-0.3333333333333333 * (x ^ 3.0)) + (x + (0.13333333333333333 * (x ^ 5.0)));
end
code[x_] := 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]
\begin{array}{l}

\\
-0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right)
\end{array}
Derivation
  1. Initial program 9.9%

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

    \[\leadsto \color{blue}{-0.3333333333333333 \cdot {x}^{3} + \left(0.13333333333333333 \cdot {x}^{5} + x\right)} \]
  3. Final simplification96.8%

    \[\leadsto -0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right) \]

Alternative 2: 96.4% accurate, 3.9× speedup?

\[\begin{array}{l} \\ x + -0.3333333333333333 \cdot {x}^{3} \end{array} \]
(FPCore (x) :precision binary64 (+ x (* -0.3333333333333333 (pow x 3.0))))
double code(double x) {
	return x + (-0.3333333333333333 * pow(x, 3.0));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x + ((-0.3333333333333333d0) * (x ** 3.0d0))
end function
public static double code(double x) {
	return x + (-0.3333333333333333 * Math.pow(x, 3.0));
}
def code(x):
	return x + (-0.3333333333333333 * math.pow(x, 3.0))
function code(x)
	return Float64(x + Float64(-0.3333333333333333 * (x ^ 3.0)))
end
function tmp = code(x)
	tmp = x + (-0.3333333333333333 * (x ^ 3.0));
end
code[x_] := N[(x + N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + -0.3333333333333333 \cdot {x}^{3}
\end{array}
Derivation
  1. Initial program 9.9%

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

    \[\leadsto \color{blue}{-0.3333333333333333 \cdot {x}^{3} + x} \]
  3. Final simplification96.5%

    \[\leadsto x + -0.3333333333333333 \cdot {x}^{3} \]

Alternative 3: 96.2% accurate, 409.0× speedup?

\[\begin{array}{l} \\ x \end{array} \]
(FPCore (x) :precision binary64 x)
double code(double x) {
	return x;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x
end function
public static double code(double x) {
	return x;
}
def code(x):
	return x
function code(x)
	return x
end
function tmp = code(x)
	tmp = x;
end
code[x_] := x
\begin{array}{l}

\\
x
\end{array}
Derivation
  1. Initial program 9.9%

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

    \[\leadsto \color{blue}{x} \]
  3. Final simplification96.2%

    \[\leadsto x \]

Reproduce

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