Hyperbolic tangent

Percentage Accurate: 9.2% → 100.0%
Time: 1.9min
Alternatives: 2
Speedup: 14.0×

Specification

?
\[\begin{array}{l} t_0 := e^{-x}\\ \frac{e^{x} - t\_0}{e^{x} + t\_0} \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (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)
use fmin_fmax_functions
    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]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET t_0 = (exp((- x))) IN
	((exp(x)) - t_0) / ((exp(x)) + t_0)
END code
\begin{array}{l}
t_0 := e^{-x}\\
\frac{e^{x} - t\_0}{e^{x} + t\_0}
\end{array}

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 2 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.2% accurate, 1.0× speedup?

\[\begin{array}{l} t_0 := e^{-x}\\ \frac{e^{x} - t\_0}{e^{x} + t\_0} \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (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)
use fmin_fmax_functions
    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]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET t_0 = (exp((- x))) IN
	((exp(x)) - t_0) / ((exp(x)) + t_0)
END code
\begin{array}{l}
t_0 := e^{-x}\\
\frac{e^{x} - t\_0}{e^{x} + t\_0}
\end{array}

Alternative 1: 100.0% accurate, 6.2× speedup?

\[\tanh x \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (tanh x))
double code(double x) {
	return tanh(x);
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = tanh(x)
end function
public static double code(double x) {
	return Math.tanh(x);
}
def code(x):
	return math.tanh(x)
function code(x)
	return tanh(x)
end
function tmp = code(x)
	tmp = tanh(x);
end
code[x_] := N[Tanh[x], $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	((exp(x)) + (- ((1) / (exp(x))))) / ((exp(x)) + ((1) / (exp(x))))
END code
\tanh x
Derivation
  1. Initial program 9.2%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Applied rewrites100.0%

    \[\leadsto \tanh x \]
  3. Add Preprocessing

Alternative 2: 96.6% accurate, 14.0× speedup?

\[x \cdot 1 \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (* x 1.0))
double code(double x) {
	return x * 1.0;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = x * 1.0d0
end function
public static double code(double x) {
	return x * 1.0;
}
def code(x):
	return x * 1.0
function code(x)
	return Float64(x * 1.0)
end
function tmp = code(x)
	tmp = x * 1.0;
end
code[x_] := N[(x * 1.0), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	x * (1)
END code
x \cdot 1
Derivation
  1. Initial program 9.2%

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

    \[\leadsto x \cdot \left(1 + \frac{-1}{3} \cdot {x}^{2}\right) \]
  3. Applied rewrites96.7%

    \[\leadsto x \cdot \left(1 + -0.3333333333333333 \cdot {x}^{2}\right) \]
  4. Taylor expanded in x around 0

    \[\leadsto x \cdot 1 \]
  5. Applied rewrites96.6%

    \[\leadsto x \cdot 1 \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2026074 +o generate:egglog
(FPCore (x)
  :name "Hyperbolic tangent"
  :precision binary64
  (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))