
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
double code(double x, double y) {
return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
public static double code(double x, double y) {
return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
def code(x, y): return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
function code(x, y) return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0) end
function tmp = code(x, y) tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0; end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{1 + e^{-2 \cdot x}} - 1
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
double code(double x, double y) {
return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
public static double code(double x, double y) {
return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
def code(x, y): return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
function code(x, y) return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0) end
function tmp = code(x, y) tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0; end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{1 + e^{-2 \cdot x}} - 1
\end{array}
(FPCore (x y)
:precision binary64
(if (<= (* -2.0 x) -0.5)
(fma (expm1 (* -2.0 x)) (/ 2.0 (expm1 (* x -4.0))) -1.0)
(if (<= (* -2.0 x) 4e-11)
(+
(* -0.05396825396825397 (pow x 7.0))
(+
(* -0.3333333333333333 (pow x 3.0))
(+ x (* 0.13333333333333333 (pow x 5.0)))))
(+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.5) {
tmp = fma(expm1((-2.0 * x)), (2.0 / expm1((x * -4.0))), -1.0);
} else if ((-2.0 * x) <= 4e-11) {
tmp = (-0.05396825396825397 * pow(x, 7.0)) + ((-0.3333333333333333 * pow(x, 3.0)) + (x + (0.13333333333333333 * pow(x, 5.0))));
} else {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.5) tmp = fma(expm1(Float64(-2.0 * x)), Float64(2.0 / expm1(Float64(x * -4.0))), -1.0); elseif (Float64(-2.0 * x) <= 4e-11) tmp = Float64(Float64(-0.05396825396825397 * (x ^ 7.0)) + Float64(Float64(-0.3333333333333333 * (x ^ 3.0)) + Float64(x + Float64(0.13333333333333333 * (x ^ 5.0))))); else tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.5], N[(N[(Exp[N[(-2.0 * x), $MachinePrecision]] - 1), $MachinePrecision] * N[(2.0 / N[(Exp[N[(x * -4.0), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 4e-11], N[(N[(-0.05396825396825397 * N[Power[x, 7.0], $MachinePrecision]), $MachinePrecision] + 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]), $MachinePrecision], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.5:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{expm1}\left(-2 \cdot x\right), \frac{2}{\mathsf{expm1}\left(x \cdot -4\right)}, -1\right)\\
\mathbf{elif}\;-2 \cdot x \leq 4 \cdot 10^{-11}:\\
\;\;\;\;-0.05396825396825397 \cdot {x}^{7} + \left(-0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\
\end{array}
\end{array}
if (*.f64 -2 x) < -0.5Initial program 100.0%
flip-+100.0%
associate-/r/100.0%
fma-neg100.0%
metadata-eval100.0%
pow2100.0%
*-commutative100.0%
exp-prod100.0%
pow-pow100.0%
metadata-eval100.0%
exp-prod100.0%
metadata-eval100.0%
Applied egg-rr100.0%
sub-neg100.0%
+-commutative100.0%
neg-sub0100.0%
associate-+l-100.0%
exp-prod100.0%
expm1-def100.0%
sub0-neg100.0%
*-commutative100.0%
Simplified100.0%
pow-exp100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 100.0%
metadata-eval100.0%
exp-prod100.0%
associate-/r*100.0%
neg-mul-1100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
exp-prod100.0%
expm1-def100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in x around inf 100.0%
associate-*r/100.0%
expm1-def100.0%
*-commutative100.0%
associate-/l*100.0%
associate-/r/100.0%
sub-neg100.0%
+-commutative100.0%
neg-sub0100.0%
associate-+l-100.0%
expm1-def100.0%
neg-sub0100.0%
distribute-rgt-neg-in100.0%
*-commutative100.0%
distribute-rgt-neg-in100.0%
mul-1-neg100.0%
Simplified100.0%
if -0.5 < (*.f64 -2 x) < 3.99999999999999976e-11Initial program 7.1%
Taylor expanded in x around 0 100.0%
if 3.99999999999999976e-11 < (*.f64 -2 x) Initial program 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= (* -2.0 x) -0.02) (fma (expm1 (* -2.0 x)) (/ 2.0 (expm1 (* x -4.0))) -1.0) (if (<= (* -2.0 x) 4e-11) x (+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.02) {
tmp = fma(expm1((-2.0 * x)), (2.0 / expm1((x * -4.0))), -1.0);
} else if ((-2.0 * x) <= 4e-11) {
tmp = x;
} else {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.02) tmp = fma(expm1(Float64(-2.0 * x)), Float64(2.0 / expm1(Float64(x * -4.0))), -1.0); elseif (Float64(-2.0 * x) <= 4e-11) tmp = x; else tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.02], N[(N[(Exp[N[(-2.0 * x), $MachinePrecision]] - 1), $MachinePrecision] * N[(2.0 / N[(Exp[N[(x * -4.0), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 4e-11], x, N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.02:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{expm1}\left(-2 \cdot x\right), \frac{2}{\mathsf{expm1}\left(x \cdot -4\right)}, -1\right)\\
\mathbf{elif}\;-2 \cdot x \leq 4 \cdot 10^{-11}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\
\end{array}
\end{array}
if (*.f64 -2 x) < -0.0200000000000000004Initial program 99.9%
flip-+99.8%
associate-/r/99.8%
fma-neg99.8%
metadata-eval99.8%
pow299.8%
*-commutative99.8%
exp-prod99.7%
pow-pow99.7%
metadata-eval99.7%
exp-prod99.7%
metadata-eval99.7%
Applied egg-rr99.7%
sub-neg99.7%
+-commutative99.7%
neg-sub099.7%
associate-+l-99.7%
exp-prod99.7%
expm1-def99.7%
sub0-neg99.7%
*-commutative99.7%
Simplified99.7%
pow-exp99.8%
Applied egg-rr99.8%
Taylor expanded in x around inf 99.8%
metadata-eval99.8%
exp-prod99.8%
associate-/r*99.8%
neg-mul-199.8%
sub-neg99.8%
+-commutative99.8%
distribute-neg-in99.8%
remove-double-neg99.8%
sub-neg99.8%
exp-prod99.8%
expm1-def99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in x around inf 99.7%
associate-*r/99.7%
expm1-def99.7%
*-commutative99.7%
associate-/l*99.7%
associate-/r/99.7%
sub-neg99.7%
+-commutative99.7%
neg-sub099.7%
associate-+l-99.7%
expm1-def99.9%
neg-sub099.9%
distribute-rgt-neg-in99.9%
*-commutative99.9%
distribute-rgt-neg-in99.9%
mul-1-neg99.9%
Simplified99.9%
if -0.0200000000000000004 < (*.f64 -2 x) < 3.99999999999999976e-11Initial program 6.4%
Taylor expanded in x around 0 100.0%
if 3.99999999999999976e-11 < (*.f64 -2 x) Initial program 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (or (<= (* -2.0 x) -0.02) (not (<= (* -2.0 x) 4e-11))) (+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0) x))
double code(double x, double y) {
double tmp;
if (((-2.0 * x) <= -0.02) || !((-2.0 * x) <= 4e-11)) {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((((-2.0d0) * x) <= (-0.02d0)) .or. (.not. (((-2.0d0) * x) <= 4d-11))) then
tmp = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) + (-1.0d0)
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((-2.0 * x) <= -0.02) || !((-2.0 * x) <= 4e-11)) {
tmp = (2.0 / (1.0 + Math.exp((-2.0 * x)))) + -1.0;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if ((-2.0 * x) <= -0.02) or not ((-2.0 * x) <= 4e-11): tmp = (2.0 / (1.0 + math.exp((-2.0 * x)))) + -1.0 else: tmp = x return tmp
function code(x, y) tmp = 0.0 if ((Float64(-2.0 * x) <= -0.02) || !(Float64(-2.0 * x) <= 4e-11)) tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((-2.0 * x) <= -0.02) || ~(((-2.0 * x) <= 4e-11))) tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.02], N[Not[LessEqual[N[(-2.0 * x), $MachinePrecision], 4e-11]], $MachinePrecision]], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.02 \lor \neg \left(-2 \cdot x \leq 4 \cdot 10^{-11}\right):\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if (*.f64 -2 x) < -0.0200000000000000004 or 3.99999999999999976e-11 < (*.f64 -2 x) Initial program 99.9%
if -0.0200000000000000004 < (*.f64 -2 x) < 3.99999999999999976e-11Initial program 6.4%
Taylor expanded in x around 0 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 53.2%
Taylor expanded in x around 0 52.9%
Final simplification52.9%
herbie shell --seed 2023224
(FPCore (x y)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))