
(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:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
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.0001)
(+ x_m (* (pow x_m 3.0) -0.3333333333333333))
(if (<= t_1 2.0) t_1 x_m)))))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.0001) {
tmp = x_m + (pow(x_m, 3.0) * -0.3333333333333333);
} else if (t_1 <= 2.0) {
tmp = t_1;
} else {
tmp = x_m;
}
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.0001d0) then
tmp = x_m + ((x_m ** 3.0d0) * (-0.3333333333333333d0))
else if (t_1 <= 2.0d0) then
tmp = t_1
else
tmp = x_m
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.0001) {
tmp = x_m + (Math.pow(x_m, 3.0) * -0.3333333333333333);
} else if (t_1 <= 2.0) {
tmp = t_1;
} else {
tmp = x_m;
}
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.0001: tmp = x_m + (math.pow(x_m, 3.0) * -0.3333333333333333) elif t_1 <= 2.0: tmp = t_1 else: tmp = x_m 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.0001) tmp = Float64(x_m + Float64((x_m ^ 3.0) * -0.3333333333333333)); elseif (t_1 <= 2.0) tmp = t_1; else tmp = x_m; 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.0001) tmp = x_m + ((x_m ^ 3.0) * -0.3333333333333333); elseif (t_1 <= 2.0) tmp = t_1; else tmp = x_m; 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.0001], N[(x$95$m + N[(N[Power[x$95$m, 3.0], $MachinePrecision] * -0.3333333333333333), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 2.0], t$95$1, x$95$m]]), $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.0001:\\
\;\;\;\;x_m + {x_m}^{3} \cdot -0.3333333333333333\\
\mathbf{elif}\;t_1 \leq 2:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;x_m\\
\end{array}
\end{array}
\end{array}
if (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) < 1.00000000000000005e-4Initial program 9.5%
Taylor expanded in x around 0 98.8%
*-commutative98.8%
Simplified98.8%
if 1.00000000000000005e-4 < (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) < 2Initial program 100.0%
if 2 < (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) Initial program 0.0%
Taylor expanded in x around 0 12.4%
Final simplification96.4%
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
:precision binary64
(*
x_s
(/
(+
(* 0.0003968253968253968 (pow x_m 7.0))
(+
(* 0.016666666666666666 (pow x_m 5.0))
(+ (* 0.3333333333333333 (pow x_m 3.0)) (* x_m 2.0))))
(+ (exp x_m) (exp (- x_m))))))x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * (((0.0003968253968253968 * pow(x_m, 7.0)) + ((0.016666666666666666 * pow(x_m, 5.0)) + ((0.3333333333333333 * pow(x_m, 3.0)) + (x_m * 2.0)))) / (exp(x_m) + exp(-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 * (((0.0003968253968253968d0 * (x_m ** 7.0d0)) + ((0.016666666666666666d0 * (x_m ** 5.0d0)) + ((0.3333333333333333d0 * (x_m ** 3.0d0)) + (x_m * 2.0d0)))) / (exp(x_m) + exp(-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 * (((0.0003968253968253968 * Math.pow(x_m, 7.0)) + ((0.016666666666666666 * Math.pow(x_m, 5.0)) + ((0.3333333333333333 * Math.pow(x_m, 3.0)) + (x_m * 2.0)))) / (Math.exp(x_m) + Math.exp(-x_m)));
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m): return x_s * (((0.0003968253968253968 * math.pow(x_m, 7.0)) + ((0.016666666666666666 * math.pow(x_m, 5.0)) + ((0.3333333333333333 * math.pow(x_m, 3.0)) + (x_m * 2.0)))) / (math.exp(x_m) + math.exp(-x_m)))
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * Float64(Float64(Float64(0.0003968253968253968 * (x_m ^ 7.0)) + Float64(Float64(0.016666666666666666 * (x_m ^ 5.0)) + Float64(Float64(0.3333333333333333 * (x_m ^ 3.0)) + Float64(x_m * 2.0)))) / Float64(exp(x_m) + exp(Float64(-x_m))))) end
x_m = abs(x); x_s = sign(x) * abs(1.0); function tmp = code(x_s, x_m) tmp = x_s * (((0.0003968253968253968 * (x_m ^ 7.0)) + ((0.016666666666666666 * (x_m ^ 5.0)) + ((0.3333333333333333 * (x_m ^ 3.0)) + (x_m * 2.0)))) / (exp(x_m) + exp(-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 * N[(N[(N[(0.0003968253968253968 * N[Power[x$95$m, 7.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.016666666666666666 * N[Power[x$95$m, 5.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Exp[x$95$m], $MachinePrecision] + N[Exp[(-x$95$m)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x_s \cdot \frac{0.0003968253968253968 \cdot {x_m}^{7} + \left(0.016666666666666666 \cdot {x_m}^{5} + \left(0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2\right)\right)}{e^{x_m} + e^{-x_m}}
\end{array}
Initial program 10.0%
Taylor expanded in x around 0 95.9%
Final simplification95.9%
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
:precision binary64
(*
x_s
(+
x_m
(+
(* (pow x_m 3.0) -0.3333333333333333)
(* (pow x_m 5.0) 0.13333333333333333)))))x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * (x_m + ((pow(x_m, 3.0) * -0.3333333333333333) + (pow(x_m, 5.0) * 0.13333333333333333)));
}
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 + (((x_m ** 3.0d0) * (-0.3333333333333333d0)) + ((x_m ** 5.0d0) * 0.13333333333333333d0)))
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 + ((Math.pow(x_m, 3.0) * -0.3333333333333333) + (Math.pow(x_m, 5.0) * 0.13333333333333333)));
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m): return x_s * (x_m + ((math.pow(x_m, 3.0) * -0.3333333333333333) + (math.pow(x_m, 5.0) * 0.13333333333333333)))
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((x_m ^ 3.0) * -0.3333333333333333) + Float64((x_m ^ 5.0) * 0.13333333333333333)))) end
x_m = abs(x); x_s = sign(x) * abs(1.0); function tmp = code(x_s, x_m) tmp = x_s * (x_m + (((x_m ^ 3.0) * -0.3333333333333333) + ((x_m ^ 5.0) * 0.13333333333333333))); 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[(N[Power[x$95$m, 3.0], $MachinePrecision] * -0.3333333333333333), $MachinePrecision] + N[(N[Power[x$95$m, 5.0], $MachinePrecision] * 0.13333333333333333), $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({x_m}^{3} \cdot -0.3333333333333333 + {x_m}^{5} \cdot 0.13333333333333333\right)\right)
\end{array}
Initial program 10.0%
Taylor expanded in x around 0 95.8%
Final simplification95.8%
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.13333333333333333))))
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.13333333333333333));
}
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.13333333333333333d0))
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.13333333333333333));
}
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.13333333333333333))
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * Float64(Float64(x_m - Float64(0.3333333333333333 * (x_m ^ 3.0))) + Float64((x_m ^ 5.0) * 0.13333333333333333))) 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.13333333333333333)); 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[(x$95$m - N[(0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Power[x$95$m, 5.0], $MachinePrecision] * 0.13333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x_s \cdot \left(\left(x_m - 0.3333333333333333 \cdot {x_m}^{3}\right) + {x_m}^{5} \cdot 0.13333333333333333\right)
\end{array}
Initial program 10.0%
Taylor expanded in x around 0 95.8%
associate-+r+95.8%
+-commutative95.8%
*-commutative95.8%
fma-def95.8%
*-commutative95.8%
Simplified95.8%
fma-udef95.8%
+-commutative95.8%
*-commutative95.8%
add-sqr-sqrt79.5%
sqrt-unprod95.4%
swap-sqr95.4%
metadata-eval95.4%
metadata-eval95.4%
swap-sqr95.4%
sqrt-unprod73.7%
add-sqr-sqrt95.2%
metadata-eval95.2%
cancel-sign-sub-inv95.2%
sub-neg95.2%
add-sqr-sqrt79.3%
sqrt-unprod95.6%
swap-sqr95.6%
metadata-eval95.6%
metadata-eval95.6%
swap-sqr95.6%
sqrt-unprod74.1%
add-sqr-sqrt95.8%
Applied egg-rr95.8%
unsub-neg95.8%
Applied egg-rr95.8%
Final simplification95.8%
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}
Initial program 10.0%
Taylor expanded in x around 0 95.4%
Final simplification95.4%
herbie shell --seed 2023334
(FPCore (x)
:name "Hyperbolic tangent"
:precision binary64
(/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))