Hyperbolic tangent

Percentage Accurate: 8.9% → 97.8%
Time: 6.7s
Alternatives: 8
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 8 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: 8.9% 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: 97.8% accurate, 0.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ \begin{array}{l} t_0 := e^{-x_m}\\ t_1 := \frac{e^{x_m} - t_0}{e^{x_m} + t_0}\\ x_s \cdot \begin{array}{l} \mathbf{if}\;t_1 \leq 0.0005:\\ \;\;\;\;x_m + \left(-0.3333333333333333 \cdot {x_m}^{3} + 0.13333333333333333 \cdot {x_m}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (let* ((t_0 (exp (- x_m))) (t_1 (/ (- (exp x_m) t_0) (+ (exp x_m) t_0))))
   (*
    x_s
    (if (<= t_1 0.0005)
      (+
       x_m
       (+
        (* -0.3333333333333333 (pow x_m 3.0))
        (* 0.13333333333333333 (pow x_m 5.0))))
      t_1))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	double t_0 = exp(-x_m);
	double t_1 = (exp(x_m) - t_0) / (exp(x_m) + t_0);
	double tmp;
	if (t_1 <= 0.0005) {
		tmp = x_m + ((-0.3333333333333333 * pow(x_m, 3.0)) + (0.13333333333333333 * pow(x_m, 5.0)));
	} else {
		tmp = t_1;
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = exp(-x_m)
    t_1 = (exp(x_m) - t_0) / (exp(x_m) + t_0)
    if (t_1 <= 0.0005d0) then
        tmp = x_m + (((-0.3333333333333333d0) * (x_m ** 3.0d0)) + (0.13333333333333333d0 * (x_m ** 5.0d0)))
    else
        tmp = t_1
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	double t_0 = Math.exp(-x_m);
	double t_1 = (Math.exp(x_m) - t_0) / (Math.exp(x_m) + t_0);
	double tmp;
	if (t_1 <= 0.0005) {
		tmp = x_m + ((-0.3333333333333333 * Math.pow(x_m, 3.0)) + (0.13333333333333333 * Math.pow(x_m, 5.0)));
	} else {
		tmp = t_1;
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	t_0 = math.exp(-x_m)
	t_1 = (math.exp(x_m) - t_0) / (math.exp(x_m) + t_0)
	tmp = 0
	if t_1 <= 0.0005:
		tmp = x_m + ((-0.3333333333333333 * math.pow(x_m, 3.0)) + (0.13333333333333333 * math.pow(x_m, 5.0)))
	else:
		tmp = t_1
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	t_0 = exp(Float64(-x_m))
	t_1 = Float64(Float64(exp(x_m) - t_0) / Float64(exp(x_m) + t_0))
	tmp = 0.0
	if (t_1 <= 0.0005)
		tmp = Float64(x_m + Float64(Float64(-0.3333333333333333 * (x_m ^ 3.0)) + Float64(0.13333333333333333 * (x_m ^ 5.0))));
	else
		tmp = t_1;
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp_2 = code(x_s, x_m)
	t_0 = exp(-x_m);
	t_1 = (exp(x_m) - t_0) / (exp(x_m) + t_0);
	tmp = 0.0;
	if (t_1 <= 0.0005)
		tmp = x_m + ((-0.3333333333333333 * (x_m ^ 3.0)) + (0.13333333333333333 * (x_m ^ 5.0)));
	else
		tmp = t_1;
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := Block[{t$95$0 = N[Exp[(-x$95$m)], $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[Exp[x$95$m], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x$95$m], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]}, N[(x$95$s * If[LessEqual[t$95$1, 0.0005], N[(x$95$m + N[(N[(-0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] + N[(0.13333333333333333 * N[Power[x$95$m, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$1]), $MachinePrecision]]]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
\begin{array}{l}
t_0 := e^{-x_m}\\
t_1 := \frac{e^{x_m} - t_0}{e^{x_m} + t_0}\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;t_1 \leq 0.0005:\\
\;\;\;\;x_m + \left(-0.3333333333333333 \cdot {x_m}^{3} + 0.13333333333333333 \cdot {x_m}^{5}\right)\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) < 5.0000000000000001e-4

    1. Initial program 9.3%

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 99.0%

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

    if 5.0000000000000001e-4 < (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x))))

    1. Initial program 65.9%

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
    2. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Final simplification98.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \leq 0.0005:\\ \;\;\;\;x + \left(-0.3333333333333333 \cdot {x}^{3} + 0.13333333333333333 \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 97.1% accurate, 1.3× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \frac{{x_m}^{3} \cdot 0.3333333333333333 + x_m \cdot 2}{2 + \mathsf{fma}\left(x_m, x_m, 0.08333333333333333 \cdot {x_m}^{4}\right)} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (/
   (+ (* (pow x_m 3.0) 0.3333333333333333) (* x_m 2.0))
   (+ 2.0 (fma x_m x_m (* 0.08333333333333333 (pow x_m 4.0)))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (((pow(x_m, 3.0) * 0.3333333333333333) + (x_m * 2.0)) / (2.0 + fma(x_m, x_m, (0.08333333333333333 * pow(x_m, 4.0)))));
}
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(Float64(Float64((x_m ^ 3.0) * 0.3333333333333333) + Float64(x_m * 2.0)) / Float64(2.0 + fma(x_m, x_m, Float64(0.08333333333333333 * (x_m ^ 4.0))))))
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(N[(N[(N[Power[x$95$m, 3.0], $MachinePrecision] * 0.3333333333333333), $MachinePrecision] + N[(x$95$m * 2.0), $MachinePrecision]), $MachinePrecision] / N[(2.0 + N[(x$95$m * x$95$m + N[(0.08333333333333333 * N[Power[x$95$m, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \frac{{x_m}^{3} \cdot 0.3333333333333333 + x_m \cdot 2}{2 + \mathsf{fma}\left(x_m, x_m, 0.08333333333333333 \cdot {x_m}^{4}\right)}
\end{array}
Derivation
  1. Initial program 10.6%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 97.1%

    \[\leadsto \frac{\color{blue}{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}}{e^{x} + e^{-x}} \]
  4. Taylor expanded in x around 0 97.3%

    \[\leadsto \frac{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}{\color{blue}{2 + \left(0.08333333333333333 \cdot {x}^{4} + {x}^{2}\right)}} \]
  5. Step-by-step derivation
    1. +-commutative97.3%

      \[\leadsto \frac{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}{2 + \color{blue}{\left({x}^{2} + 0.08333333333333333 \cdot {x}^{4}\right)}} \]
    2. unpow297.3%

      \[\leadsto \frac{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}{2 + \left(\color{blue}{x \cdot x} + 0.08333333333333333 \cdot {x}^{4}\right)} \]
    3. fma-def97.3%

      \[\leadsto \frac{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}{2 + \color{blue}{\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)}} \]
  6. Applied egg-rr97.3%

    \[\leadsto \frac{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}{2 + \color{blue}{\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)}} \]
  7. Final simplification97.3%

    \[\leadsto \frac{{x}^{3} \cdot 0.3333333333333333 + x \cdot 2}{2 + \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)} \]
  8. Add Preprocessing

Alternative 3: 97.0% accurate, 1.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \frac{{x_m}^{3} \cdot 0.3333333333333333 + x_m \cdot 2}{\mathsf{fma}\left(x_m, x_m, 2\right)} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (/ (+ (* (pow x_m 3.0) 0.3333333333333333) (* x_m 2.0)) (fma x_m x_m 2.0))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (((pow(x_m, 3.0) * 0.3333333333333333) + (x_m * 2.0)) / fma(x_m, x_m, 2.0));
}
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(Float64(Float64((x_m ^ 3.0) * 0.3333333333333333) + Float64(x_m * 2.0)) / fma(x_m, x_m, 2.0)))
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(N[(N[(N[Power[x$95$m, 3.0], $MachinePrecision] * 0.3333333333333333), $MachinePrecision] + N[(x$95$m * 2.0), $MachinePrecision]), $MachinePrecision] / N[(x$95$m * x$95$m + 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \frac{{x_m}^{3} \cdot 0.3333333333333333 + x_m \cdot 2}{\mathsf{fma}\left(x_m, x_m, 2\right)}
\end{array}
Derivation
  1. Initial program 10.6%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 97.1%

    \[\leadsto \frac{\color{blue}{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}}{e^{x} + e^{-x}} \]
  4. Taylor expanded in x around 0 97.3%

    \[\leadsto \frac{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}{\color{blue}{2 + {x}^{2}}} \]
  5. Step-by-step derivation
    1. +-commutative8.5%

      \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{{x}^{2} + 2}} \]
    2. unpow28.5%

      \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{x \cdot x} + 2} \]
    3. fma-def8.5%

      \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{\mathsf{fma}\left(x, x, 2\right)}} \]
  6. Simplified97.3%

    \[\leadsto \frac{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}{\color{blue}{\mathsf{fma}\left(x, x, 2\right)}} \]
  7. Final simplification97.3%

    \[\leadsto \frac{{x}^{3} \cdot 0.3333333333333333 + x \cdot 2}{\mathsf{fma}\left(x, x, 2\right)} \]
  8. Add Preprocessing

Alternative 4: 97.0% accurate, 1.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \left(x_m + \left(-0.3333333333333333 \cdot {x_m}^{3} + {x_m}^{5} \cdot 0.125\right)\right) \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (+ x_m (+ (* -0.3333333333333333 (pow x_m 3.0)) (* (pow x_m 5.0) 0.125)))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (x_m + ((-0.3333333333333333 * pow(x_m, 3.0)) + (pow(x_m, 5.0) * 0.125)));
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    code = x_s * (x_m + (((-0.3333333333333333d0) * (x_m ** 3.0d0)) + ((x_m ** 5.0d0) * 0.125d0)))
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * (x_m + ((-0.3333333333333333 * Math.pow(x_m, 3.0)) + (Math.pow(x_m, 5.0) * 0.125)));
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * (x_m + ((-0.3333333333333333 * math.pow(x_m, 3.0)) + (math.pow(x_m, 5.0) * 0.125)))
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(x_m + Float64(Float64(-0.3333333333333333 * (x_m ^ 3.0)) + Float64((x_m ^ 5.0) * 0.125))))
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * (x_m + ((-0.3333333333333333 * (x_m ^ 3.0)) + ((x_m ^ 5.0) * 0.125)));
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(x$95$m + N[(N[(-0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[Power[x$95$m, 5.0], $MachinePrecision] * 0.125), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \left(x_m + \left(-0.3333333333333333 \cdot {x_m}^{3} + {x_m}^{5} \cdot 0.125\right)\right)
\end{array}
Derivation
  1. Initial program 10.6%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 97.1%

    \[\leadsto \frac{\color{blue}{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}}{e^{x} + e^{-x}} \]
  4. Taylor expanded in x around 0 97.2%

    \[\leadsto \color{blue}{x + \left(-0.3333333333333333 \cdot {x}^{3} + 0.125 \cdot {x}^{5}\right)} \]
  5. Final simplification97.2%

    \[\leadsto x + \left(-0.3333333333333333 \cdot {x}^{3} + {x}^{5} \cdot 0.125\right) \]
  6. Add Preprocessing

Alternative 5: 97.1% accurate, 1.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \left(x_m + \left(-0.3333333333333333 \cdot {x_m}^{3} + 0.13333333333333333 \cdot {x_m}^{5}\right)\right) \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (+
   x_m
   (+
    (* -0.3333333333333333 (pow x_m 3.0))
    (* 0.13333333333333333 (pow x_m 5.0))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (x_m + ((-0.3333333333333333 * pow(x_m, 3.0)) + (0.13333333333333333 * pow(x_m, 5.0))));
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    code = x_s * (x_m + (((-0.3333333333333333d0) * (x_m ** 3.0d0)) + (0.13333333333333333d0 * (x_m ** 5.0d0))))
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * (x_m + ((-0.3333333333333333 * Math.pow(x_m, 3.0)) + (0.13333333333333333 * Math.pow(x_m, 5.0))));
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * (x_m + ((-0.3333333333333333 * math.pow(x_m, 3.0)) + (0.13333333333333333 * math.pow(x_m, 5.0))))
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(x_m + Float64(Float64(-0.3333333333333333 * (x_m ^ 3.0)) + Float64(0.13333333333333333 * (x_m ^ 5.0)))))
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * (x_m + ((-0.3333333333333333 * (x_m ^ 3.0)) + (0.13333333333333333 * (x_m ^ 5.0))));
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(x$95$m + N[(N[(-0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] + N[(0.13333333333333333 * N[Power[x$95$m, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

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

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 97.2%

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

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

Alternative 6: 96.7% accurate, 3.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \left(x_m + -0.3333333333333333 \cdot {x_m}^{3}\right) \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (* x_s (+ x_m (* -0.3333333333333333 (pow x_m 3.0)))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (x_m + (-0.3333333333333333 * pow(x_m, 3.0)));
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    code = x_s * (x_m + ((-0.3333333333333333d0) * (x_m ** 3.0d0)))
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * (x_m + (-0.3333333333333333 * Math.pow(x_m, 3.0)));
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * (x_m + (-0.3333333333333333 * math.pow(x_m, 3.0)))
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(x_m + Float64(-0.3333333333333333 * (x_m ^ 3.0))))
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * (x_m + (-0.3333333333333333 * (x_m ^ 3.0)));
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(x$95$m + N[(-0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \left(x_m + -0.3333333333333333 \cdot {x_m}^{3}\right)
\end{array}
Derivation
  1. Initial program 10.6%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.9%

    \[\leadsto \color{blue}{x + -0.3333333333333333 \cdot {x}^{3}} \]
  4. Step-by-step derivation
    1. *-commutative96.9%

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

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

    \[\leadsto x + -0.3333333333333333 \cdot {x}^{3} \]
  7. Add Preprocessing

Alternative 7: 5.8% accurate, 409.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot 1.5 \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m) :precision binary64 (* x_s 1.5))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * 1.5;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    code = x_s * 1.5d0
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * 1.5;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * 1.5
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * 1.5)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * 1.5;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * 1.5), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot 1.5
\end{array}
Derivation
  1. Initial program 10.6%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 8.5%

    \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{2 + {x}^{2}}} \]
  4. Step-by-step derivation
    1. +-commutative8.5%

      \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{{x}^{2} + 2}} \]
    2. unpow28.5%

      \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{x \cdot x} + 2} \]
    3. fma-def8.5%

      \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{\mathsf{fma}\left(x, x, 2\right)}} \]
  5. Simplified8.5%

    \[\leadsto \frac{e^{x} - e^{-x}}{\color{blue}{\mathsf{fma}\left(x, x, 2\right)}} \]
  6. Applied egg-rr4.3%

    \[\leadsto \frac{\color{blue}{3}}{\mathsf{fma}\left(x, x, 2\right)} \]
  7. Taylor expanded in x around 0 4.4%

    \[\leadsto \color{blue}{1.5} \]
  8. Final simplification4.4%

    \[\leadsto 1.5 \]
  9. Add Preprocessing

Alternative 8: 96.5% accurate, 409.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot x_m \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m) :precision binary64 (* x_s x_m))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * x_m;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    code = x_s * x_m
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * x_m;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * x_m
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * x_m)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * x_m;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * x$95$m), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot x_m
\end{array}
Derivation
  1. Initial program 10.6%

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

    \[\leadsto \color{blue}{x} \]
  4. Final simplification96.5%

    \[\leadsto x \]
  5. Add Preprocessing

Reproduce

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