
(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 7 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 (pow (exp -1.0) (/ x s)))))
float code(float x, float s) {
return 1.0f / (1.0f + powf(expf(-1.0f), (x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + (exp((-1.0e0)) ** (x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + (exp(Float32(-1.0)) ^ Float32(x / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + (exp(single(-1.0)) ^ (x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + {\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}}
\end{array}
Initial program 99.9%
div-inv99.9%
exp-prod85.6%
neg-mul-185.6%
exp-prod85.6%
pow-pow99.9%
div-inv99.9%
Applied egg-rr99.9%
Final simplification99.9%
(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.9999999920083944e-12) (+ -1.0 (+ 1.0 (/ s x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.9999999920083944e-12f) {
tmp = -1.0f + (1.0f + (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 <= (-1.9999999920083944e-12)) then
tmp = (-1.0e0) + (1.0e0 + (s / x))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-1.9999999920083944e-12)) tmp = Float32(Float32(-1.0) + Float32(Float32(1.0) + 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(-1.9999999920083944e-12)) tmp = single(-1.0) + (single(1.0) + (s / x)); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.9999999920083944 \cdot 10^{-12}:\\
\;\;\;\;-1 + \left(1 + \frac{s}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.99999999e-12Initial program 99.9%
Taylor expanded in x around 0 42.2%
mul-1-neg42.2%
unsub-neg42.2%
Simplified42.2%
Taylor expanded in x around inf 35.5%
mul-1-neg35.5%
distribute-frac-neg35.5%
Simplified35.5%
clear-num37.9%
associate-/r/35.5%
add-sqr-sqrt-0.0%
sqrt-unprod51.5%
sqr-neg51.5%
sqrt-unprod34.5%
add-sqr-sqrt34.5%
Applied egg-rr34.5%
expm1-log1p-u34.3%
expm1-udef87.1%
log1p-udef87.1%
rem-exp-log87.2%
associate-*l/87.2%
*-un-lft-identity87.2%
Applied egg-rr87.2%
if -1.99999999e-12 < x Initial program 99.9%
Taylor expanded in x around 0 47.5%
Final simplification62.6%
(FPCore (x s) :precision binary32 (if (<= x -7.999999979801942e-6) (/ 1.0 (/ x s)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -7.999999979801942e-6f) {
tmp = 1.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 <= (-7.999999979801942e-6)) then
tmp = 1.0e0 / (x / s)
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-7.999999979801942e-6)) tmp = Float32(Float32(1.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(-7.999999979801942e-6)) tmp = single(1.0) / (x / s); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.999999979801942 \cdot 10^{-6}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -7.99999998e-6Initial program 100.0%
Taylor expanded in x around 0 44.7%
mul-1-neg44.7%
unsub-neg44.7%
Simplified44.7%
Taylor expanded in x around inf 41.7%
mul-1-neg41.7%
distribute-frac-neg41.7%
Simplified41.7%
clear-num44.7%
associate-/r/41.7%
add-sqr-sqrt-0.0%
sqrt-unprod56.1%
sqr-neg56.1%
sqrt-unprod41.7%
add-sqr-sqrt41.7%
Applied egg-rr41.7%
associate-*l/41.7%
associate-/l*44.7%
Applied egg-rr44.7%
if -7.99999998e-6 < x Initial program 99.8%
Taylor expanded in x around 0 45.3%
Final simplification45.1%
(FPCore (x s) :precision binary32 (if (<= x -5.000000058430487e-8) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -5.000000058430487e-8f) {
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 <= (-5.000000058430487e-8)) 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(-5.000000058430487e-8)) 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(-5.000000058430487e-8)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.000000058430487 \cdot 10^{-8}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -5.00000006e-8Initial program 99.9%
Taylor expanded in x around 0 43.1%
mul-1-neg43.1%
unsub-neg43.1%
Simplified43.1%
Taylor expanded in x around inf 40.2%
mul-1-neg40.2%
distribute-frac-neg40.2%
Simplified40.2%
if -5.00000006e-8 < x Initial program 99.8%
Taylor expanded in x around 0 46.1%
Final simplification44.2%
(FPCore (x s) :precision binary32 (if (<= x -7.999999979801942e-6) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -7.999999979801942e-6f) {
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 <= (-7.999999979801942e-6)) 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(-7.999999979801942e-6)) tmp = 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(-7.999999979801942e-6)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.999999979801942 \cdot 10^{-6}:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -7.99999998e-6Initial program 100.0%
Taylor expanded in x around 0 44.7%
mul-1-neg44.7%
unsub-neg44.7%
Simplified44.7%
Taylor expanded in x around inf 41.7%
mul-1-neg41.7%
distribute-frac-neg41.7%
Simplified41.7%
expm1-log1p-u41.7%
expm1-udef100.0%
add-sqr-sqrt-0.0%
sqrt-unprod97.6%
sqr-neg97.6%
sqrt-unprod97.6%
add-sqr-sqrt97.6%
Applied egg-rr97.6%
expm1-def41.7%
expm1-log1p41.7%
Simplified41.7%
if -7.99999998e-6 < x Initial program 99.8%
Taylor expanded in x around 0 45.3%
Final simplification44.2%
(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 33.5%
Final simplification33.5%
herbie shell --seed 2023310
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))