
(FPCore (x) :precision binary64 (/ 1.0 (+ 1.0 (exp (- x)))))
double code(double x) {
return 1.0 / (1.0 + exp(-x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (1.0d0 + exp(-x))
end function
public static double code(double x) {
return 1.0 / (1.0 + Math.exp(-x));
}
def code(x): return 1.0 / (1.0 + math.exp(-x))
function code(x) return Float64(1.0 / Float64(1.0 + exp(Float64(-x)))) end
function tmp = code(x) tmp = 1.0 / (1.0 + exp(-x)); end
code[x_] := N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{1 + e^{-x}}
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ 1.0 (+ 1.0 (exp (- x)))))
double code(double x) {
return 1.0 / (1.0 + exp(-x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (1.0d0 + exp(-x))
end function
public static double code(double x) {
return 1.0 / (1.0 + Math.exp(-x));
}
def code(x): return 1.0 / (1.0 + math.exp(-x))
function code(x) return Float64(1.0 / Float64(1.0 + exp(Float64(-x)))) end
function tmp = code(x) tmp = 1.0 / (1.0 + exp(-x)); end
code[x_] := N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{1 + e^{-x}}
(FPCore (x) :precision binary64 (if (<= x -0.65) (/ (exp x) 2.0) (/ 1.0 (- (/ 1.0 (- x -1.0)) -1.0))))
double code(double x) {
double tmp;
if (x <= -0.65) {
tmp = exp(x) / 2.0;
} else {
tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-0.65d0)) then
tmp = exp(x) / 2.0d0
else
tmp = 1.0d0 / ((1.0d0 / (x - (-1.0d0))) - (-1.0d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -0.65) {
tmp = Math.exp(x) / 2.0;
} else {
tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= -0.65: tmp = math.exp(x) / 2.0 else: tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0) return tmp
function code(x) tmp = 0.0 if (x <= -0.65) tmp = Float64(exp(x) / 2.0); else tmp = Float64(1.0 / Float64(Float64(1.0 / Float64(x - -1.0)) - -1.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -0.65) tmp = exp(x) / 2.0; else tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -0.65], N[(N[Exp[x], $MachinePrecision] / 2.0), $MachinePrecision], N[(1.0 / N[(N[(1.0 / N[(x - -1.0), $MachinePrecision]), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;x \leq -0.65:\\
\;\;\;\;\frac{e^{x}}{2}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\
\end{array}
if x < -0.65000000000000002Initial program 100.0%
lift-/.f64N/A
lift-+.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f64N/A
lower-exp.f64N/A
add-flipN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-exp.f6475.6%
Applied rewrites75.6%
Taylor expanded in x around 0
Applied rewrites75.0%
if -0.65000000000000002 < x Initial program 100.0%
lift-/.f64N/A
lift-+.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f64N/A
lower-exp.f64N/A
add-flipN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-exp.f6475.6%
Applied rewrites75.6%
Taylor expanded in x around 0
lower-+.f6451.8%
Applied rewrites51.8%
Taylor expanded in x around 0
lower-+.f6475.4%
Applied rewrites75.4%
lift-/.f64N/A
div-flipN/A
lower-unsound-/.f64N/A
lower-unsound-/.f6475.4%
lift-+.f64N/A
+-commutativeN/A
add-flipN/A
metadata-evalN/A
lower--.f6475.4%
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
Applied rewrites75.4%
lift-/.f64N/A
frac-2negN/A
lift--.f64N/A
sub-negate-revN/A
div-subN/A
metadata-evalN/A
frac-2negN/A
distribute-neg-frac2N/A
*-inversesN/A
metadata-evalN/A
lower--.f64N/A
lower-/.f6475.4%
Applied rewrites75.4%
(FPCore (x) :precision binary64 (if (<= (/ 1.0 (+ 1.0 (exp (- x)))) 0.5526041551930219) (/ 1.0 (fma (fma 0.5 x -1.0) x 2.0)) (/ 1.0 (- (/ 1.0 (- x -1.0)) -1.0))))
double code(double x) {
double tmp;
if ((1.0 / (1.0 + exp(-x))) <= 0.5526041551930219) {
tmp = 1.0 / fma(fma(0.5, x, -1.0), x, 2.0);
} else {
tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(1.0 / Float64(1.0 + exp(Float64(-x)))) <= 0.5526041551930219) tmp = Float64(1.0 / fma(fma(0.5, x, -1.0), x, 2.0)); else tmp = Float64(1.0 / Float64(Float64(1.0 / Float64(x - -1.0)) - -1.0)); end return tmp end
code[x_] := If[LessEqual[N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5526041551930219], N[(1.0 / N[(N[(0.5 * x + -1.0), $MachinePrecision] * x + 2.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(1.0 / N[(x - -1.0), $MachinePrecision]), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 0.5526041551930219:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\
\end{array}
if (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) < 0.55260415519302186Initial program 100.0%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f64N/A
lower--.f64N/A
lower-*.f6464.4%
Applied rewrites64.4%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6464.4%
lift--.f64N/A
sub-flipN/A
lift-*.f64N/A
metadata-evalN/A
lower-fma.f6464.4%
Applied rewrites64.4%
if 0.55260415519302186 < (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) Initial program 100.0%
lift-/.f64N/A
lift-+.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f64N/A
lower-exp.f64N/A
add-flipN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-exp.f6475.6%
Applied rewrites75.6%
Taylor expanded in x around 0
lower-+.f6451.8%
Applied rewrites51.8%
Taylor expanded in x around 0
lower-+.f6475.4%
Applied rewrites75.4%
lift-/.f64N/A
div-flipN/A
lower-unsound-/.f64N/A
lower-unsound-/.f6475.4%
lift-+.f64N/A
+-commutativeN/A
add-flipN/A
metadata-evalN/A
lower--.f6475.4%
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
Applied rewrites75.4%
lift-/.f64N/A
frac-2negN/A
lift--.f64N/A
sub-negate-revN/A
div-subN/A
metadata-evalN/A
frac-2negN/A
distribute-neg-frac2N/A
*-inversesN/A
metadata-evalN/A
lower--.f64N/A
lower-/.f6475.4%
Applied rewrites75.4%
(FPCore (x) :precision binary64 (if (<= (/ 1.0 (+ 1.0 (exp (- x)))) 1e-5) (/ 1.0 (fma (* 0.5 x) x 2.0)) (/ 1.0 (- (/ 1.0 (- x -1.0)) -1.0))))
double code(double x) {
double tmp;
if ((1.0 / (1.0 + exp(-x))) <= 1e-5) {
tmp = 1.0 / fma((0.5 * x), x, 2.0);
} else {
tmp = 1.0 / ((1.0 / (x - -1.0)) - -1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(1.0 / Float64(1.0 + exp(Float64(-x)))) <= 1e-5) tmp = Float64(1.0 / fma(Float64(0.5 * x), x, 2.0)); else tmp = Float64(1.0 / Float64(Float64(1.0 / Float64(x - -1.0)) - -1.0)); end return tmp end
code[x_] := If[LessEqual[N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1e-5], N[(1.0 / N[(N[(0.5 * x), $MachinePrecision] * x + 2.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(1.0 / N[(x - -1.0), $MachinePrecision]), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 10^{-5}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{1}{x - -1} - -1}\\
\end{array}
if (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) < 1.0000000000000001e-5Initial program 100.0%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f64N/A
lower--.f64N/A
lower-*.f6464.4%
Applied rewrites64.4%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6464.4%
lift--.f64N/A
sub-flipN/A
lift-*.f64N/A
metadata-evalN/A
lower-fma.f6464.4%
Applied rewrites64.4%
Taylor expanded in x around inf
lower-*.f6463.8%
Applied rewrites63.8%
if 1.0000000000000001e-5 < (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) Initial program 100.0%
lift-/.f64N/A
lift-+.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f64N/A
lower-exp.f64N/A
add-flipN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-exp.f6475.6%
Applied rewrites75.6%
Taylor expanded in x around 0
lower-+.f6451.8%
Applied rewrites51.8%
Taylor expanded in x around 0
lower-+.f6475.4%
Applied rewrites75.4%
lift-/.f64N/A
div-flipN/A
lower-unsound-/.f64N/A
lower-unsound-/.f6475.4%
lift-+.f64N/A
+-commutativeN/A
add-flipN/A
metadata-evalN/A
lower--.f6475.4%
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
lower--.f64N/A
Applied rewrites75.4%
lift-/.f64N/A
frac-2negN/A
lift--.f64N/A
sub-negate-revN/A
div-subN/A
metadata-evalN/A
frac-2negN/A
distribute-neg-frac2N/A
*-inversesN/A
metadata-evalN/A
lower--.f64N/A
lower-/.f6475.4%
Applied rewrites75.4%
(FPCore (x) :precision binary64 (if (<= (/ 1.0 (+ 1.0 (exp (- x)))) 1e-5) (/ 1.0 (fma (* 0.5 x) x 2.0)) (/ (+ 1.0 x) (- (+ 1.0 x) -1.0))))
double code(double x) {
double tmp;
if ((1.0 / (1.0 + exp(-x))) <= 1e-5) {
tmp = 1.0 / fma((0.5 * x), x, 2.0);
} else {
tmp = (1.0 + x) / ((1.0 + x) - -1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(1.0 / Float64(1.0 + exp(Float64(-x)))) <= 1e-5) tmp = Float64(1.0 / fma(Float64(0.5 * x), x, 2.0)); else tmp = Float64(Float64(1.0 + x) / Float64(Float64(1.0 + x) - -1.0)); end return tmp end
code[x_] := If[LessEqual[N[(1.0 / N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1e-5], N[(1.0 / N[(N[(0.5 * x), $MachinePrecision] * x + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 + x), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\frac{1}{1 + e^{-x}} \leq 10^{-5}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(0.5 \cdot x, x, 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{1 + x}{\left(1 + x\right) - -1}\\
\end{array}
if (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) < 1.0000000000000001e-5Initial program 100.0%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f64N/A
lower--.f64N/A
lower-*.f6464.4%
Applied rewrites64.4%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6464.4%
lift--.f64N/A
sub-flipN/A
lift-*.f64N/A
metadata-evalN/A
lower-fma.f6464.4%
Applied rewrites64.4%
Taylor expanded in x around inf
lower-*.f6463.8%
Applied rewrites63.8%
if 1.0000000000000001e-5 < (/.f64 #s(literal 1 binary64) (+.f64 #s(literal 1 binary64) (exp.f64 (neg.f64 x)))) Initial program 100.0%
lift-/.f64N/A
lift-+.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f64N/A
lower-exp.f64N/A
add-flipN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-exp.f6475.6%
Applied rewrites75.6%
Taylor expanded in x around 0
lower-+.f6451.8%
Applied rewrites51.8%
Taylor expanded in x around 0
lower-+.f6475.4%
Applied rewrites75.4%
(FPCore (x) :precision binary64 (/ (+ 1.0 x) (- (+ 1.0 x) -1.0)))
double code(double x) {
return (1.0 + x) / ((1.0 + x) - -1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 + x) / ((1.0d0 + x) - (-1.0d0))
end function
public static double code(double x) {
return (1.0 + x) / ((1.0 + x) - -1.0);
}
def code(x): return (1.0 + x) / ((1.0 + x) - -1.0)
function code(x) return Float64(Float64(1.0 + x) / Float64(Float64(1.0 + x) - -1.0)) end
function tmp = code(x) tmp = (1.0 + x) / ((1.0 + x) - -1.0); end
code[x_] := N[(N[(1.0 + x), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]
\frac{1 + x}{\left(1 + x\right) - -1}
Initial program 100.0%
lift-/.f64N/A
lift-+.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f64N/A
lower-exp.f64N/A
add-flipN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-exp.f6475.6%
Applied rewrites75.6%
Taylor expanded in x around 0
lower-+.f6451.8%
Applied rewrites51.8%
Taylor expanded in x around 0
lower-+.f6475.4%
Applied rewrites75.4%
(FPCore (x) :precision binary64 (/ (+ 1.0 x) (+ 2.0 x)))
double code(double x) {
return (1.0 + x) / (2.0 + x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 + x) / (2.0d0 + x)
end function
public static double code(double x) {
return (1.0 + x) / (2.0 + x);
}
def code(x): return (1.0 + x) / (2.0 + x)
function code(x) return Float64(Float64(1.0 + x) / Float64(2.0 + x)) end
function tmp = code(x) tmp = (1.0 + x) / (2.0 + x); end
code[x_] := N[(N[(1.0 + x), $MachinePrecision] / N[(2.0 + x), $MachinePrecision]), $MachinePrecision]
\frac{1 + x}{2 + x}
Initial program 100.0%
lift-/.f64N/A
lift-+.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
add-to-fractionN/A
div-flip-revN/A
lower-/.f64N/A
lower-exp.f64N/A
add-flipN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-exp.f6475.6%
Applied rewrites75.6%
Taylor expanded in x around 0
lower-+.f6451.8%
Applied rewrites51.8%
Taylor expanded in x around 0
lower-+.f6475.4%
Applied rewrites75.4%
Taylor expanded in x around 0
lower-+.f6475.4%
Applied rewrites75.4%
(FPCore (x) :precision binary64 0.5)
double code(double x) {
return 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0
end function
public static double code(double x) {
return 0.5;
}
def code(x): return 0.5
function code(x) return 0.5 end
function tmp = code(x) tmp = 0.5; end
code[x_] := 0.5
0.5
Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites55.3%
herbie shell --seed 1
(FPCore (x)
:name "1/(1+exp(-x))"
:precision binary64
:pre (and (<= -1.79e+308 x) (<= x 1.79e+308))
(/ 1.0 (+ 1.0 (exp (- x)))))