
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 11 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (if (<= x -1.0000000031710769e-30) (/ 1.0 (+ 2.0 (- (* 0.5 (/ x (/ (* s s) x))) (/ x s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.0000000031710769e-30f) {
tmp = 1.0f / (2.0f + ((0.5f * (x / ((s * s) / x))) - (x / s)));
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-1.0000000031710769e-30)) then
tmp = 1.0e0 / (2.0e0 + ((0.5e0 * (x / ((s * s) / x))) - (x / s)))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-1.0000000031710769e-30)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(0.5) * Float32(x / Float32(Float32(s * s) / x))) - Float32(x / s)))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-1.0000000031710769e-30)) tmp = single(1.0) / (single(2.0) + ((single(0.5) * (x / ((s * s) / x))) - (x / s))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.0000000031710769 \cdot 10^{-30}:\\
\;\;\;\;\frac{1}{2 + \left(0.5 \cdot \frac{x}{\frac{s \cdot s}{x}} - \frac{x}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1e-30Initial program 99.9%
Taylor expanded in x around 0 74.7%
mul-1-neg74.7%
unsub-neg74.7%
unpow274.7%
unpow274.7%
times-frac72.9%
Simplified72.9%
clear-num72.9%
frac-times75.9%
*-un-lft-identity75.9%
Applied egg-rr75.9%
associate-*l/79.5%
Applied egg-rr79.5%
if -1e-30 < x Initial program 99.9%
Taylor expanded in x around 0 49.7%
Final simplification62.3%
(FPCore (x s) :precision binary32 (if (<= x -5.000000229068525e-19) (/ 1.0 (+ 2.0 (* 0.5 (/ (* x x) (* s s))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -5.000000229068525e-19f) {
tmp = 1.0f / (2.0f + (0.5f * ((x * x) / (s * s))));
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-5.000000229068525e-19)) then
tmp = 1.0e0 / (2.0e0 + (0.5e0 * ((x * x) / (s * s))))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-5.000000229068525e-19)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(0.5) * Float32(Float32(x * x) / Float32(s * s))))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-5.000000229068525e-19)) tmp = single(1.0) / (single(2.0) + (single(0.5) * ((x * x) / (s * s)))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.000000229068525 \cdot 10^{-19}:\\
\;\;\;\;\frac{1}{2 + 0.5 \cdot \frac{x \cdot x}{s \cdot s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -5.00000023e-19Initial program 99.9%
Taylor expanded in x around 0 77.9%
mul-1-neg77.9%
unsub-neg77.9%
unpow277.9%
unpow277.9%
times-frac73.4%
Simplified73.4%
clear-num73.4%
frac-times77.0%
*-un-lft-identity77.0%
Applied egg-rr77.0%
Taylor expanded in x around inf 76.1%
unpow276.1%
unpow276.1%
Simplified76.1%
if -5.00000023e-19 < x Initial program 99.9%
Taylor expanded in x around 0 50.6%
Final simplification59.8%
(FPCore (x s) :precision binary32 (if (<= x -1.0000000031710769e-30) (/ 1.0 (+ 2.0 (* (* x 0.5) (/ x (* s s))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.0000000031710769e-30f) {
tmp = 1.0f / (2.0f + ((x * 0.5f) * (x / (s * s))));
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-1.0000000031710769e-30)) then
tmp = 1.0e0 / (2.0e0 + ((x * 0.5e0) * (x / (s * s))))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-1.0000000031710769e-30)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(x * Float32(0.5)) * Float32(x / Float32(s * s))))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-1.0000000031710769e-30)) tmp = single(1.0) / (single(2.0) + ((x * single(0.5)) * (x / (s * s)))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.0000000031710769 \cdot 10^{-30}:\\
\;\;\;\;\frac{1}{2 + \left(x \cdot 0.5\right) \cdot \frac{x}{s \cdot s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1e-30Initial program 99.9%
Taylor expanded in x around 0 74.7%
mul-1-neg74.7%
unsub-neg74.7%
unpow274.7%
unpow274.7%
times-frac72.9%
Simplified72.9%
clear-num72.9%
frac-times75.9%
*-un-lft-identity75.9%
Applied egg-rr75.9%
associate-*l/79.5%
Applied egg-rr79.5%
Taylor expanded in x around inf 72.3%
unpow272.3%
unpow272.3%
associate-*r/77.0%
associate-*r*77.0%
*-commutative77.0%
Simplified77.0%
if -1e-30 < x Initial program 99.9%
Taylor expanded in x around 0 49.7%
Final simplification61.3%
(FPCore (x s) :precision binary32 (if (<= x -4.999999858590343e-10) (* 2.0 (* (/ s x) (/ s x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999858590343e-10f) {
tmp = 2.0f * ((s / x) * (s / x));
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-4.999999858590343e-10)) then
tmp = 2.0e0 * ((s / x) * (s / x))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.999999858590343e-10)) tmp = Float32(Float32(2.0) * Float32(Float32(s / x) * Float32(s / x))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-4.999999858590343e-10)) tmp = single(2.0) * ((s / x) * (s / x)); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.999999858590343 \cdot 10^{-10}:\\
\;\;\;\;2 \cdot \left(\frac{s}{x} \cdot \frac{s}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999986e-10Initial program 100.0%
Taylor expanded in x around 0 77.6%
mul-1-neg77.6%
unsub-neg77.6%
unpow277.6%
unpow277.6%
times-frac77.6%
Simplified77.6%
Taylor expanded in x around inf 74.8%
unpow274.8%
unpow274.8%
times-frac73.8%
Simplified73.8%
if -4.99999986e-10 < x Initial program 99.9%
Taylor expanded in x around 0 50.3%
Final simplification57.1%
(FPCore (x s) :precision binary32 (if (<= x -4.999999858590343e-10) (* 2.0 (/ s (* x (/ x s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999858590343e-10f) {
tmp = 2.0f * (s / (x * (x / s)));
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-4.999999858590343e-10)) then
tmp = 2.0e0 * (s / (x * (x / s)))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.999999858590343e-10)) tmp = Float32(Float32(2.0) * Float32(s / Float32(x * Float32(x / s)))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-4.999999858590343e-10)) tmp = single(2.0) * (s / (x * (x / s))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.999999858590343 \cdot 10^{-10}:\\
\;\;\;\;2 \cdot \frac{s}{x \cdot \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999986e-10Initial program 100.0%
Taylor expanded in x around 0 77.6%
mul-1-neg77.6%
unsub-neg77.6%
unpow277.6%
unpow277.6%
times-frac77.6%
Simplified77.6%
Taylor expanded in x around inf 74.8%
unpow274.8%
unpow274.8%
times-frac73.8%
Simplified73.8%
clear-num73.8%
frac-times73.9%
*-un-lft-identity73.9%
Applied egg-rr73.9%
if -4.99999986e-10 < x Initial program 99.9%
Taylor expanded in x around 0 50.3%
Final simplification57.2%
(FPCore (x s) :precision binary32 (if (<= x -4.999999858590343e-10) (* 2.0 (/ (* s (/ s x)) x)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999858590343e-10f) {
tmp = 2.0f * ((s * (s / x)) / x);
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-4.999999858590343e-10)) then
tmp = 2.0e0 * ((s * (s / x)) / x)
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.999999858590343e-10)) tmp = Float32(Float32(2.0) * Float32(Float32(s * Float32(s / x)) / x)); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-4.999999858590343e-10)) tmp = single(2.0) * ((s * (s / x)) / x); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.999999858590343 \cdot 10^{-10}:\\
\;\;\;\;2 \cdot \frac{s \cdot \frac{s}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999986e-10Initial program 100.0%
Taylor expanded in x around 0 77.6%
mul-1-neg77.6%
unsub-neg77.6%
unpow277.6%
unpow277.6%
times-frac77.6%
Simplified77.6%
Taylor expanded in x around inf 74.8%
unpow274.8%
unpow274.8%
times-frac73.8%
Simplified73.8%
associate-*l/74.4%
Applied egg-rr74.4%
if -4.99999986e-10 < x Initial program 99.9%
Taylor expanded in x around 0 50.3%
Final simplification57.3%
(FPCore (x s) :precision binary32 (if (<= x -1.5000000583807998e-16) (/ (* 2.0 (* s s)) (* x x)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.5000000583807998e-16f) {
tmp = (2.0f * (s * s)) / (x * x);
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-1.5000000583807998e-16)) then
tmp = (2.0e0 * (s * s)) / (x * x)
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-1.5000000583807998e-16)) tmp = Float32(Float32(Float32(2.0) * Float32(s * s)) / Float32(x * x)); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-1.5000000583807998e-16)) tmp = (single(2.0) * (s * s)) / (x * x); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.5000000583807998 \cdot 10^{-16}:\\
\;\;\;\;\frac{2 \cdot \left(s \cdot s\right)}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.5000001e-16Initial program 99.9%
Taylor expanded in x around 0 77.2%
mul-1-neg77.2%
unsub-neg77.2%
unpow277.2%
unpow277.2%
times-frac73.2%
Simplified73.2%
clear-num73.2%
frac-times76.2%
*-un-lft-identity76.2%
Applied egg-rr76.2%
associate-*l/77.2%
Applied egg-rr77.2%
Taylor expanded in x around inf 70.6%
unpow270.6%
associate-*r/70.6%
unpow270.6%
Simplified70.6%
if -1.5000001e-16 < x Initial program 99.9%
Taylor expanded in x around 0 51.3%
Final simplification57.7%
(FPCore (x s) :precision binary32 (if (<= x -4.999999675228202e-39) (/ 1.0 (- 2.0 (/ x s))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999675228202e-39f) {
tmp = 1.0f / (2.0f - (x / s));
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-4.999999675228202e-39)) then
tmp = 1.0e0 / (2.0e0 - (x / s))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.999999675228202e-39)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(x / s))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-4.999999675228202e-39)) tmp = single(1.0) / (single(2.0) - (x / s)); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.999999675228202 \cdot 10^{-39}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999968e-39Initial program 99.8%
Taylor expanded in x around 0 55.5%
mul-1-neg55.5%
unsub-neg55.5%
Simplified55.5%
if -4.99999968e-39 < x Initial program 100.0%
Taylor expanded in x around 0 46.4%
Final simplification50.6%
(FPCore (x s) :precision binary32 (if (<= x -4.999999987376214e-7) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999987376214e-7f) {
tmp = -s / x;
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-4.999999987376214e-7)) then
tmp = -s / x
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.999999987376214e-7)) tmp = Float32(Float32(-s) / x); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-4.999999987376214e-7)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.999999987376214 \cdot 10^{-7}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999999e-7Initial program 100.0%
Taylor expanded in x around 0 46.8%
mul-1-neg46.8%
unsub-neg46.8%
Simplified46.8%
Taylor expanded in x around inf 44.0%
associate-*r/44.0%
neg-mul-144.0%
Simplified44.0%
if -4.99999999e-7 < x Initial program 99.9%
Taylor expanded in x around 0 50.2%
Final simplification48.5%
(FPCore (x s) :precision binary32 0.5)
float code(float x, float s) {
return 0.5f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0
end function
function code(x, s) return Float32(0.5) end
function tmp = code(x, s) tmp = single(0.5); end
\begin{array}{l}
\\
0.5
\end{array}
Initial program 99.9%
Taylor expanded in x around 0 38.1%
Final simplification38.1%
herbie shell --seed 2023274
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))