
(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) -200.0)
(+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0)
(expm1
(*
x
(+
1.0
(*
x
(-
(*
(pow x 2.0)
(+ 0.08333333333333333 (* (pow x 2.0) -0.022222222222222223)))
0.5)))))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -200.0) {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
} else {
tmp = expm1((x * (1.0 + (x * ((pow(x, 2.0) * (0.08333333333333333 + (pow(x, 2.0) * -0.022222222222222223))) - 0.5)))));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -200.0) {
tmp = (2.0 / (1.0 + Math.exp((-2.0 * x)))) + -1.0;
} else {
tmp = Math.expm1((x * (1.0 + (x * ((Math.pow(x, 2.0) * (0.08333333333333333 + (Math.pow(x, 2.0) * -0.022222222222222223))) - 0.5)))));
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -200.0: tmp = (2.0 / (1.0 + math.exp((-2.0 * x)))) + -1.0 else: tmp = math.expm1((x * (1.0 + (x * ((math.pow(x, 2.0) * (0.08333333333333333 + (math.pow(x, 2.0) * -0.022222222222222223))) - 0.5))))) return tmp
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -200.0) tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0); else tmp = expm1(Float64(x * Float64(1.0 + Float64(x * Float64(Float64((x ^ 2.0) * Float64(0.08333333333333333 + Float64((x ^ 2.0) * -0.022222222222222223))) - 0.5))))); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -200.0], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(Exp[N[(x * N[(1.0 + N[(x * N[(N[(N[Power[x, 2.0], $MachinePrecision] * N[(0.08333333333333333 + N[(N[Power[x, 2.0], $MachinePrecision] * -0.022222222222222223), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -200:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x \cdot \left(1 + x \cdot \left({x}^{2} \cdot \left(0.08333333333333333 + {x}^{2} \cdot -0.022222222222222223\right) - 0.5\right)\right)\right)\\
\end{array}
\end{array}
if (*.f64 #s(literal -2 binary64) x) < -200Initial program 100.0%
if -200 < (*.f64 #s(literal -2 binary64) x) Initial program 37.9%
add-exp-log37.9%
expm1-define37.9%
log-div37.8%
log1p-define37.9%
exp-prod37.9%
Applied egg-rr37.9%
Taylor expanded in x around 0 99.7%
Final simplification99.8%
(FPCore (x y) :precision binary64 (if (<= (* -2.0 x) -0.0004) (+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0) (expm1 (* x (+ 1.0 (* x -0.5))))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.0004) {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
} 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.0004) {
tmp = (2.0 / (1.0 + Math.exp((-2.0 * x)))) + -1.0;
} else {
tmp = Math.expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -0.0004: tmp = (2.0 / (1.0 + math.exp((-2.0 * x)))) + -1.0 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.0004) tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0); 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.0004], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $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.0004:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\
\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) < -4.00000000000000019e-4Initial program 99.7%
if -4.00000000000000019e-4 < (*.f64 #s(literal -2 binary64) x) Initial program 37.7%
add-exp-log37.7%
expm1-define37.7%
log-div37.6%
log1p-define37.6%
exp-prod37.6%
Applied egg-rr37.6%
Taylor expanded in x around 0 99.4%
*-commutative99.4%
Simplified99.4%
Final simplification99.5%
(FPCore (x y) :precision binary64 (if (<= x 0.95) (expm1 (* x (+ 1.0 (* x -0.5)))) (/ 1.0 (/ (+ x 2.0) (* x 2.0)))))
double code(double x, double y) {
double tmp;
if (x <= 0.95) {
tmp = expm1((x * (1.0 + (x * -0.5))));
} else {
tmp = 1.0 / ((x + 2.0) / (x * 2.0));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if (x <= 0.95) {
tmp = Math.expm1((x * (1.0 + (x * -0.5))));
} else {
tmp = 1.0 / ((x + 2.0) / (x * 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 0.95: tmp = math.expm1((x * (1.0 + (x * -0.5)))) else: tmp = 1.0 / ((x + 2.0) / (x * 2.0)) return tmp
function code(x, y) tmp = 0.0 if (x <= 0.95) tmp = expm1(Float64(x * Float64(1.0 + Float64(x * -0.5)))); else tmp = Float64(1.0 / Float64(Float64(x + 2.0) / Float64(x * 2.0))); end return tmp end
code[x_, y_] := If[LessEqual[x, 0.95], N[(Exp[N[(x * N[(1.0 + N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision], N[(1.0 / N[(N[(x + 2.0), $MachinePrecision] / N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.95:\\
\;\;\;\;\mathsf{expm1}\left(x \cdot \left(1 + x \cdot -0.5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x + 2}{x \cdot 2}}\\
\end{array}
\end{array}
if x < 0.94999999999999996Initial program 37.9%
add-exp-log37.9%
expm1-define37.9%
log-div37.8%
log1p-define37.9%
exp-prod37.9%
Applied egg-rr37.9%
Taylor expanded in x around 0 99.3%
*-commutative99.3%
Simplified99.3%
if 0.94999999999999996 < x Initial program 100.0%
Taylor expanded in x around 0 5.3%
+-commutative5.3%
Simplified5.3%
flip--5.0%
clear-num5.0%
associate-+l+5.0%
metadata-eval5.0%
metadata-eval5.0%
difference-of-sqr-15.0%
associate-+l+5.0%
metadata-eval5.0%
associate--l+5.0%
metadata-eval5.0%
+-rgt-identity5.0%
Applied egg-rr5.0%
Taylor expanded in x around 0 18.8%
*-commutative18.8%
Simplified18.8%
Final simplification81.7%
(FPCore (x y) :precision binary64 (if (<= x 2e-20) (expm1 x) (/ 1.0 (/ (+ x 2.0) (* x 2.0)))))
double code(double x, double y) {
double tmp;
if (x <= 2e-20) {
tmp = expm1(x);
} else {
tmp = 1.0 / ((x + 2.0) / (x * 2.0));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if (x <= 2e-20) {
tmp = Math.expm1(x);
} else {
tmp = 1.0 / ((x + 2.0) / (x * 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 2e-20: tmp = math.expm1(x) else: tmp = 1.0 / ((x + 2.0) / (x * 2.0)) return tmp
function code(x, y) tmp = 0.0 if (x <= 2e-20) tmp = expm1(x); else tmp = Float64(1.0 / Float64(Float64(x + 2.0) / Float64(x * 2.0))); end return tmp end
code[x_, y_] := If[LessEqual[x, 2e-20], N[(Exp[x] - 1), $MachinePrecision], N[(1.0 / N[(N[(x + 2.0), $MachinePrecision] / N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2 \cdot 10^{-20}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x + 2}{x \cdot 2}}\\
\end{array}
\end{array}
if x < 1.99999999999999989e-20Initial program 37.0%
add-exp-log37.0%
expm1-define37.0%
log-div37.0%
log1p-define37.1%
exp-prod37.1%
Applied egg-rr37.1%
Taylor expanded in x around 0 99.7%
Taylor expanded in x around 0 99.2%
if 1.99999999999999989e-20 < x Initial program 96.6%
Taylor expanded in x around 0 10.5%
+-commutative10.5%
Simplified10.5%
flip--10.1%
clear-num10.1%
associate-+l+10.1%
metadata-eval10.1%
metadata-eval10.1%
difference-of-sqr-110.2%
associate-+l+10.2%
metadata-eval10.2%
associate--l+12.4%
metadata-eval12.4%
+-rgt-identity12.4%
Applied egg-rr12.4%
Taylor expanded in x around 0 22.5%
*-commutative22.5%
Simplified22.5%
Final simplification80.6%
(FPCore (x y) :precision binary64 (if (<= x 0.66) x (/ 1.0 (/ (+ x 2.0) (* x 2.0)))))
double code(double x, double y) {
double tmp;
if (x <= 0.66) {
tmp = x;
} else {
tmp = 1.0 / ((x + 2.0) / (x * 2.0));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 0.66d0) then
tmp = x
else
tmp = 1.0d0 / ((x + 2.0d0) / (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 0.66) {
tmp = x;
} else {
tmp = 1.0 / ((x + 2.0) / (x * 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 0.66: tmp = x else: tmp = 1.0 / ((x + 2.0) / (x * 2.0)) return tmp
function code(x, y) tmp = 0.0 if (x <= 0.66) tmp = x; else tmp = Float64(1.0 / Float64(Float64(x + 2.0) / Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 0.66) tmp = x; else tmp = 1.0 / ((x + 2.0) / (x * 2.0)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 0.66], x, N[(1.0 / N[(N[(x + 2.0), $MachinePrecision] / N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.66:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x + 2}{x \cdot 2}}\\
\end{array}
\end{array}
if x < 0.660000000000000031Initial program 37.9%
Taylor expanded in x around 0 68.9%
if 0.660000000000000031 < x Initial program 100.0%
Taylor expanded in x around 0 5.3%
+-commutative5.3%
Simplified5.3%
flip--5.0%
clear-num5.0%
associate-+l+5.0%
metadata-eval5.0%
metadata-eval5.0%
difference-of-sqr-15.0%
associate-+l+5.0%
metadata-eval5.0%
associate--l+5.0%
metadata-eval5.0%
+-rgt-identity5.0%
Applied egg-rr5.0%
Taylor expanded in x around 0 18.8%
*-commutative18.8%
Simplified18.8%
Final simplification57.9%
(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 51.5%
Taylor expanded in x around 0 55.0%
Final simplification55.0%
herbie shell --seed 2024078
(FPCore (x y)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))