
(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 7 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 #s(literal 1 binary64) 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.002)
(fma
(- (* 0.13333333333333333 (pow x_m 2.0)) 0.3333333333333333)
(pow x_m 3.0)
x_m)
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.002) {
tmp = fma(((0.13333333333333333 * pow(x_m, 2.0)) - 0.3333333333333333), pow(x_m, 3.0), x_m);
} 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.002) tmp = fma(Float64(Float64(0.13333333333333333 * (x_m ^ 2.0)) - 0.3333333333333333), (x_m ^ 3.0), x_m); else tmp = t_1; end return Float64(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.002], N[(N[(N[(0.13333333333333333 * N[Power[x$95$m, 2.0], $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision] * N[Power[x$95$m, 3.0], $MachinePrecision] + x$95$m), $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.002:\\
\;\;\;\;\mathsf{fma}\left(0.13333333333333333 \cdot {x\_m}^{2} - 0.3333333333333333, {x\_m}^{3}, x\_m\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\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)))) < 2e-3Initial program 10.1%
Taylor expanded in x around 0 99.0%
+-commutative99.0%
distribute-rgt-in99.0%
*-commutative99.0%
associate-*l*99.0%
*-lft-identity99.0%
fma-define99.0%
*-commutative99.0%
fmm-def99.0%
metadata-eval99.0%
pow-plus99.0%
metadata-eval99.0%
Simplified99.0%
Taylor expanded in x around 0 99.0%
if 2e-3 < (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) Initial program 42.5%
Final simplification97.5%
x\_m = (fabs.f64 x) x\_s = (copysign.f64 #s(literal 1 binary64) x) (FPCore (x_s x_m) :precision binary64 (* x_s (fma (- (* 0.13333333333333333 (pow x_m 2.0)) 0.3333333333333333) (pow x_m 3.0) x_m)))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * fma(((0.13333333333333333 * pow(x_m, 2.0)) - 0.3333333333333333), pow(x_m, 3.0), x_m);
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * fma(Float64(Float64(0.13333333333333333 * (x_m ^ 2.0)) - 0.3333333333333333), (x_m ^ 3.0), 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.13333333333333333 * N[Power[x$95$m, 2.0], $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision] * N[Power[x$95$m, 3.0], $MachinePrecision] + x$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \mathsf{fma}\left(0.13333333333333333 \cdot {x\_m}^{2} - 0.3333333333333333, {x\_m}^{3}, x\_m\right)
\end{array}
Initial program 10.9%
Taylor expanded in x around 0 96.7%
+-commutative96.7%
distribute-rgt-in96.7%
*-commutative96.7%
associate-*l*96.7%
*-lft-identity96.7%
fma-define96.7%
*-commutative96.7%
fmm-def96.7%
metadata-eval96.7%
pow-plus96.7%
metadata-eval96.7%
Simplified96.7%
Taylor expanded in x around 0 96.7%
Final simplification96.7%
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
:precision binary64
(*
x_s
(*
x_m
(+
1.0
(*
(pow x_m 2.0)
(- (* 0.13333333333333333 (pow x_m 2.0)) 0.3333333333333333))))))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * (x_m * (1.0 + (pow(x_m, 2.0) * ((0.13333333333333333 * pow(x_m, 2.0)) - 0.3333333333333333))));
}
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 * (1.0d0 + ((x_m ** 2.0d0) * ((0.13333333333333333d0 * (x_m ** 2.0d0)) - 0.3333333333333333d0))))
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 * (1.0 + (Math.pow(x_m, 2.0) * ((0.13333333333333333 * Math.pow(x_m, 2.0)) - 0.3333333333333333))));
}
x\_m = math.fabs(x) x\_s = math.copysign(1.0, x) def code(x_s, x_m): return x_s * (x_m * (1.0 + (math.pow(x_m, 2.0) * ((0.13333333333333333 * math.pow(x_m, 2.0)) - 0.3333333333333333))))
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * Float64(x_m * Float64(1.0 + Float64((x_m ^ 2.0) * Float64(Float64(0.13333333333333333 * (x_m ^ 2.0)) - 0.3333333333333333))))) end
x\_m = abs(x); x\_s = sign(x) * abs(1.0); function tmp = code(x_s, x_m) tmp = x_s * (x_m * (1.0 + ((x_m ^ 2.0) * ((0.13333333333333333 * (x_m ^ 2.0)) - 0.3333333333333333)))); 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[(1.0 + N[(N[Power[x$95$m, 2.0], $MachinePrecision] * N[(N[(0.13333333333333333 * N[Power[x$95$m, 2.0], $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $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 \cdot \left(1 + {x\_m}^{2} \cdot \left(0.13333333333333333 \cdot {x\_m}^{2} - 0.3333333333333333\right)\right)\right)
\end{array}
Initial program 10.9%
Taylor expanded in x around 0 96.7%
Final simplification96.7%
x\_m = (fabs.f64 x) x\_s = (copysign.f64 #s(literal 1 binary64) x) (FPCore (x_s x_m) :precision binary64 (* x_s (/ (* x_m (+ 2.0 (* (pow x_m 2.0) 0.3333333333333333))) (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 * ((x_m * (2.0 + (pow(x_m, 2.0) * 0.3333333333333333))) / 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(x_m * Float64(2.0 + Float64((x_m ^ 2.0) * 0.3333333333333333))) / 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[(x$95$m * N[(2.0 + N[(N[Power[x$95$m, 2.0], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]), $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 \cdot \left(2 + {x\_m}^{2} \cdot 0.3333333333333333\right)}{\mathsf{fma}\left(x\_m, x\_m, 2\right)}
\end{array}
Initial program 10.9%
Taylor expanded in x around 0 9.1%
+-commutative9.1%
unpow29.1%
fma-define9.1%
Simplified9.1%
Taylor expanded in x around 0 96.7%
Final simplification96.7%
x\_m = (fabs.f64 x) x\_s = (copysign.f64 #s(literal 1 binary64) x) (FPCore (x_s x_m) :precision binary64 (* x_s (+ x_m (* (pow x_m 3.0) -0.3333333333333333))))
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));
}
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)))
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));
}
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))
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * Float64(x_m + Float64((x_m ^ 3.0) * -0.3333333333333333))) 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)); 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[Power[x$95$m, 3.0], $MachinePrecision] * -0.3333333333333333), $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 + {x\_m}^{3} \cdot -0.3333333333333333\right)
\end{array}
Initial program 10.9%
Taylor expanded in x around 0 96.7%
+-commutative96.7%
distribute-rgt-in96.7%
*-commutative96.7%
associate-*l*96.7%
*-lft-identity96.7%
fma-define96.7%
*-commutative96.7%
fmm-def96.7%
metadata-eval96.7%
pow-plus96.7%
metadata-eval96.7%
Simplified96.7%
Taylor expanded in x around 0 96.2%
distribute-rgt-in96.2%
*-lft-identity96.2%
associate-*r*96.2%
unpow296.2%
unpow396.2%
Simplified96.2%
Final simplification96.2%
x\_m = (fabs.f64 x) x\_s = (copysign.f64 #s(literal 1 binary64) x) (FPCore (x_s x_m) :precision binary64 (* x_s 0.75))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * 0.75;
}
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.75d0
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.75;
}
x\_m = math.fabs(x) x\_s = math.copysign(1.0, x) def code(x_s, x_m): return x_s * 0.75
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * 0.75) end
x\_m = abs(x); x\_s = sign(x) * abs(1.0); function tmp = code(x_s, x_m) tmp = x_s * 0.75; 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 * 0.75), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot 0.75
\end{array}
Initial program 10.9%
Taylor expanded in x around 0 9.1%
+-commutative9.1%
unpow29.1%
fma-define9.1%
Simplified9.1%
Applied egg-rr3.9%
Taylor expanded in x around 0 4.1%
Final simplification4.1%
x\_m = (fabs.f64 x) x\_s = (copysign.f64 #s(literal 1 binary64) 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.9%
Taylor expanded in x around 0 95.8%
Final simplification95.8%
herbie shell --seed 2024112
(FPCore (x)
:name "Hyperbolic tangent"
:precision binary64
(/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))