
(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 6 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.0005)
(/
(+ 1.0 (/ -4.0 (pow (hypot 1.0 (exp (- x))) 4.0)))
(+ -1.0 (/ 2.0 (- -1.0 (exp (* -2.0 x))))))
(expm1 (* x (+ 1.0 (* x -0.5))))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.0005) {
tmp = (1.0 + (-4.0 / pow(hypot(1.0, exp(-x)), 4.0))) / (-1.0 + (2.0 / (-1.0 - exp((-2.0 * x)))));
} else {
tmp = expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.0005) {
tmp = (1.0 + (-4.0 / Math.pow(Math.hypot(1.0, Math.exp(-x)), 4.0))) / (-1.0 + (2.0 / (-1.0 - Math.exp((-2.0 * x)))));
} else {
tmp = Math.expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -0.0005: tmp = (1.0 + (-4.0 / math.pow(math.hypot(1.0, math.exp(-x)), 4.0))) / (-1.0 + (2.0 / (-1.0 - math.exp((-2.0 * x))))) else: tmp = math.expm1((x * (1.0 + (x * -0.5)))) return tmp
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.0005) tmp = Float64(Float64(1.0 + Float64(-4.0 / (hypot(1.0, exp(Float64(-x))) ^ 4.0))) / Float64(-1.0 + Float64(2.0 / Float64(-1.0 - exp(Float64(-2.0 * x)))))); else tmp = expm1(Float64(x * Float64(1.0 + Float64(x * -0.5)))); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.0005], N[(N[(1.0 + N[(-4.0 / N[Power[N[Sqrt[1.0 ^ 2 + N[Exp[(-x)], $MachinePrecision] ^ 2], $MachinePrecision], 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(-1.0 + N[(2.0 / N[(-1.0 - N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[N[(x * N[(1.0 + N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.0005:\\
\;\;\;\;\frac{1 + \frac{-4}{{\left(\mathsf{hypot}\left(1, e^{-x}\right)\right)}^{4}}}{-1 + \frac{2}{-1 - e^{-2 \cdot x}}}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x \cdot \left(1 + x \cdot -0.5\right)\right)\\
\end{array}
\end{array}
if (*.f64 #s(literal -2 binary64) x) < -5.0000000000000001e-4Initial program 99.7%
flip--99.7%
div-inv99.7%
metadata-eval99.7%
sub-neg99.7%
frac-times99.8%
metadata-eval99.8%
pow299.8%
exp-prod99.8%
metadata-eval99.8%
+-commutative99.8%
exp-prod99.8%
Applied egg-rr99.8%
associate-*r/99.8%
*-rgt-identity99.8%
remove-double-neg99.8%
distribute-frac-neg299.8%
distribute-frac-neg99.8%
Simplified99.8%
Taylor expanded in x around inf 99.8%
if -5.0000000000000001e-4 < (*.f64 #s(literal -2 binary64) x) Initial program 39.8%
add-exp-log39.8%
expm1-define39.8%
log-div39.8%
log1p-define39.8%
exp-prod39.8%
Applied egg-rr39.8%
Taylor expanded in x around 0 99.5%
*-commutative99.5%
Simplified99.5%
Final simplification99.6%
(FPCore (x y) :precision binary64 (if (<= (* -2.0 x) -0.0005) (expm1 (- (log 2.0) (log1p (exp (* -2.0 x))))) (expm1 (* x (+ 1.0 (* x -0.5))))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.0005) {
tmp = expm1((log(2.0) - log1p(exp((-2.0 * x)))));
} else {
tmp = expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.0005) {
tmp = Math.expm1((Math.log(2.0) - Math.log1p(Math.exp((-2.0 * x)))));
} else {
tmp = Math.expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -0.0005: tmp = math.expm1((math.log(2.0) - math.log1p(math.exp((-2.0 * x))))) else: tmp = math.expm1((x * (1.0 + (x * -0.5)))) return tmp
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.0005) tmp = expm1(Float64(log(2.0) - log1p(exp(Float64(-2.0 * x))))); else tmp = expm1(Float64(x * Float64(1.0 + Float64(x * -0.5)))); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.0005], N[(Exp[N[(N[Log[2.0], $MachinePrecision] - N[Log[1 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision], N[(Exp[N[(x * N[(1.0 + N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.0005:\\
\;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{-2 \cdot x}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x \cdot \left(1 + x \cdot -0.5\right)\right)\\
\end{array}
\end{array}
if (*.f64 #s(literal -2 binary64) x) < -5.0000000000000001e-4Initial program 99.7%
add-exp-log99.7%
expm1-define99.7%
log-div99.7%
log1p-define99.7%
exp-prod99.7%
Applied egg-rr99.7%
Taylor expanded in x around inf 99.7%
if -5.0000000000000001e-4 < (*.f64 #s(literal -2 binary64) x) Initial program 39.8%
add-exp-log39.8%
expm1-define39.8%
log-div39.8%
log1p-define39.8%
exp-prod39.8%
Applied egg-rr39.8%
Taylor expanded in x around 0 99.5%
*-commutative99.5%
Simplified99.5%
Final simplification99.6%
(FPCore (x y) :precision binary64 (if (<= (* -2.0 x) -0.0005) (+ -1.0 (/ 2.0 (+ 1.0 (exp (* -2.0 x))))) (expm1 (* x (+ 1.0 (* x -0.5))))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.0005) {
tmp = -1.0 + (2.0 / (1.0 + exp((-2.0 * x))));
} else {
tmp = expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.0005) {
tmp = -1.0 + (2.0 / (1.0 + Math.exp((-2.0 * x))));
} else {
tmp = Math.expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -0.0005: tmp = -1.0 + (2.0 / (1.0 + math.exp((-2.0 * x)))) else: tmp = math.expm1((x * (1.0 + (x * -0.5)))) return tmp
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.0005) tmp = Float64(-1.0 + Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x))))); else tmp = expm1(Float64(x * Float64(1.0 + Float64(x * -0.5)))); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.0005], N[(-1.0 + N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[N[(x * N[(1.0 + N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.0005:\\
\;\;\;\;-1 + \frac{2}{1 + e^{-2 \cdot x}}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x \cdot \left(1 + x \cdot -0.5\right)\right)\\
\end{array}
\end{array}
if (*.f64 #s(literal -2 binary64) x) < -5.0000000000000001e-4Initial program 99.7%
if -5.0000000000000001e-4 < (*.f64 #s(literal -2 binary64) x) Initial program 39.8%
add-exp-log39.8%
expm1-define39.8%
log-div39.8%
log1p-define39.8%
exp-prod39.8%
Applied egg-rr39.8%
Taylor expanded in x around 0 99.5%
*-commutative99.5%
Simplified99.5%
Final simplification99.6%
(FPCore (x y) :precision binary64 (expm1 (* x (+ 1.0 (* x -0.5)))))
double code(double x, double y) {
return expm1((x * (1.0 + (x * -0.5))));
}
public static double code(double x, double y) {
return Math.expm1((x * (1.0 + (x * -0.5))));
}
def code(x, y): return math.expm1((x * (1.0 + (x * -0.5))))
function code(x, y) return expm1(Float64(x * Float64(1.0 + Float64(x * -0.5)))) end
code[x_, y_] := N[(Exp[N[(x * N[(1.0 + N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{expm1}\left(x \cdot \left(1 + x \cdot -0.5\right)\right)
\end{array}
Initial program 58.3%
add-exp-log58.3%
expm1-define58.3%
log-div58.3%
log1p-define58.3%
exp-prod58.3%
Applied egg-rr58.3%
Taylor expanded in x around 0 69.8%
*-commutative69.8%
Simplified69.8%
Final simplification69.8%
(FPCore (x y) :precision binary64 (if (<= x -1.0) -1.0 x))
double code(double x, double y) {
double tmp;
if (x <= -1.0) {
tmp = -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 (x <= (-1.0d0)) then
tmp = -1.0d0
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.0) {
tmp = -1.0;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.0: tmp = -1.0 else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -1.0) tmp = -1.0; else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.0) tmp = -1.0; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.0], -1.0, x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -1Initial program 100.0%
Taylor expanded in x around 0 97.0%
*-commutative97.0%
Simplified97.0%
Taylor expanded in x around inf 98.7%
if -1 < x Initial program 45.0%
Taylor expanded in x around 0 61.6%
Final simplification70.6%
(FPCore (x y) :precision binary64 -1.0)
double code(double x, double y) {
return -1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -1.0d0
end function
public static double code(double x, double y) {
return -1.0;
}
def code(x, y): return -1.0
function code(x, y) return -1.0 end
function tmp = code(x, y) tmp = -1.0; end
code[x_, y_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 58.3%
Taylor expanded in x around 0 27.3%
*-commutative27.3%
Simplified27.3%
Taylor expanded in x around inf 26.1%
Final simplification26.1%
herbie shell --seed 2024073
(FPCore (x y)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))