
(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 12 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) -0.6666666666666666)) (pow (cbrt (exp -1.0)) (/ x s))))))
float code(float x, float s) {
return 1.0f / (1.0f + (expf(((x / s) * -0.6666666666666666f)) * powf(cbrtf(expf(-1.0f)), (x / s))));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(exp(Float32(Float32(x / s) * Float32(-0.6666666666666666))) * (cbrt(exp(Float32(-1.0))) ^ Float32(x / s))))) end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{x}{s} \cdot -0.6666666666666666} \cdot {\left(\sqrt[3]{e^{-1}}\right)}^{\left(\frac{x}{s}\right)}}
\end{array}
Initial program 99.9%
div-inv99.9%
exp-prod82.9%
neg-mul-182.9%
exp-prod82.9%
pow-pow99.9%
div-inv99.9%
Applied egg-rr99.9%
add-cube-cbrt99.8%
unpow-prod-down99.8%
cbrt-unprod99.9%
prod-exp99.9%
metadata-eval99.9%
Applied egg-rr99.9%
add-exp-log99.9%
log-pow99.9%
pow1/399.9%
log-pow99.9%
add-log-exp99.9%
metadata-eval99.9%
Applied egg-rr99.9%
Final simplification99.9%
(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-prod82.9%
neg-mul-182.9%
exp-prod82.9%
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 -4.999999943633011e-27) (/ 1.0 (+ 2.0 (- (* 0.5 (/ x (/ (* s s) x))) (/ x s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999943633011e-27f) {
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 <= (-4.999999943633011e-27)) 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(-4.999999943633011e-27)) 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(-4.999999943633011e-27)) 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 -4.999999943633011 \cdot 10^{-27}:\\
\;\;\;\;\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 < -4.99999994e-27Initial program 99.9%
Taylor expanded in x around 0 81.0%
mul-1-neg81.0%
unsub-neg81.0%
unpow281.0%
unpow281.0%
times-frac73.4%
Simplified73.4%
frac-times81.0%
associate-/l*85.1%
Applied egg-rr85.1%
if -4.99999994e-27 < x Initial program 99.9%
Taylor expanded in x around 0 49.1%
Final simplification63.1%
(FPCore (x s) :precision binary32 (if (<= x -2.00000006274879e-22) (/ 1.0 (+ 2.0 (* 0.5 (/ (* x x) (* s s))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -2.00000006274879e-22f) {
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 <= (-2.00000006274879e-22)) 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(-2.00000006274879e-22)) 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(-2.00000006274879e-22)) 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 -2.00000006274879 \cdot 10^{-22}:\\
\;\;\;\;\frac{1}{2 + 0.5 \cdot \frac{x \cdot x}{s \cdot s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -2.00000006e-22Initial program 99.9%
Taylor expanded in x around 0 83.7%
mul-1-neg83.7%
unsub-neg83.7%
unpow283.7%
unpow283.7%
times-frac74.8%
Simplified74.8%
associate-*r*74.8%
*-un-lft-identity74.8%
distribute-rgt-out--74.8%
associate-*r/74.8%
Applied egg-rr74.8%
Taylor expanded in x around inf 82.8%
unpow282.8%
unpow282.8%
Simplified82.8%
if -2.00000006e-22 < x Initial program 99.9%
Taylor expanded in x around 0 49.3%
Final simplification61.1%
(FPCore (x s) :precision binary32 (if (<= x -1.9999999920083944e-11) (* 2.0 (* (/ s x) (/ s x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.9999999920083944e-11f) {
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 <= (-1.9999999920083944e-11)) 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(-1.9999999920083944e-11)) 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(-1.9999999920083944e-11)) 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 -1.9999999920083944 \cdot 10^{-11}:\\
\;\;\;\;2 \cdot \left(\frac{s}{x} \cdot \frac{s}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.99999999e-11Initial program 100.0%
Taylor expanded in x around 0 81.9%
mul-1-neg81.9%
unsub-neg81.9%
unpow281.9%
unpow281.9%
times-frac81.9%
Simplified81.9%
Taylor expanded in x around inf 79.8%
unpow279.8%
unpow279.8%
times-frac78.8%
Simplified78.8%
if -1.99999999e-11 < x Initial program 99.9%
Taylor expanded in x around 0 48.7%
Final simplification56.8%
(FPCore (x s) :precision binary32 (if (<= x -1.9999999920083944e-11) (* 2.0 (/ s (/ (* x x) s))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.9999999920083944e-11f) {
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 <= (-1.9999999920083944e-11)) 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(-1.9999999920083944e-11)) tmp = Float32(Float32(2.0) * Float32(s / Float32(Float32(x * 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.9999999920083944e-11)) 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 -1.9999999920083944 \cdot 10^{-11}:\\
\;\;\;\;2 \cdot \frac{s}{\frac{x \cdot x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.99999999e-11Initial program 100.0%
Taylor expanded in x around 0 81.9%
mul-1-neg81.9%
unsub-neg81.9%
unpow281.9%
unpow281.9%
times-frac81.9%
Simplified81.9%
Taylor expanded in x around inf 79.8%
unpow279.8%
unpow279.8%
times-frac78.8%
Simplified78.8%
frac-times79.8%
associate-/l*78.8%
Applied egg-rr78.8%
if -1.99999999e-11 < x Initial program 99.9%
Taylor expanded in x around 0 48.7%
Final simplification56.8%
(FPCore (x s) :precision binary32 (if (<= x -1.0999999650920748e-20) (* 2.0 (/ (* s s) (* x x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.0999999650920748e-20f) {
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.0999999650920748e-20)) 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.0999999650920748e-20)) tmp = Float32(Float32(2.0) * Float32(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.0999999650920748e-20)) 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.0999999650920748 \cdot 10^{-20}:\\
\;\;\;\;2 \cdot \frac{s \cdot s}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.09999997e-20Initial program 100.0%
Taylor expanded in x around 0 83.0%
mul-1-neg83.0%
unsub-neg83.0%
unpow283.0%
unpow283.0%
times-frac75.6%
Simplified75.6%
associate-*r*75.6%
*-un-lft-identity75.6%
distribute-rgt-out--75.6%
associate-*r/75.6%
Applied egg-rr75.6%
Taylor expanded in x around inf 76.8%
unpow276.8%
unpow276.8%
Simplified76.8%
if -1.09999997e-20 < x Initial program 99.8%
Taylor expanded in x around 0 49.9%
Final simplification58.6%
(FPCore (x s) :precision binary32 (if (<= x -1.0999999650920748e-20) (* 2.0 (/ (/ (* s s) x) x)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.0999999650920748e-20f) {
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.0999999650920748e-20)) 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.0999999650920748e-20)) tmp = Float32(Float32(2.0) * Float32(Float32(Float32(s * 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(-1.0999999650920748e-20)) 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.0999999650920748 \cdot 10^{-20}:\\
\;\;\;\;2 \cdot \frac{\frac{s \cdot s}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.09999997e-20Initial program 100.0%
Taylor expanded in x around 0 83.0%
mul-1-neg83.0%
unsub-neg83.0%
unpow283.0%
unpow283.0%
times-frac75.6%
Simplified75.6%
Taylor expanded in x around inf 76.8%
unpow276.8%
unpow276.8%
times-frac68.0%
Simplified68.0%
frac-times76.8%
associate-/r*76.8%
Applied egg-rr76.8%
if -1.09999997e-20 < x Initial program 99.8%
Taylor expanded in x around 0 49.9%
Final simplification58.6%
(FPCore (x s) :precision binary32 (if (<= x 4.999999943633011e-27) (/ 1.0 (- 2.0 (/ x s))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= 4.999999943633011e-27f) {
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.999999943633011e-27) 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.999999943633011e-27)) 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.999999943633011e-27)) 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.999999943633011 \cdot 10^{-27}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < 4.99999994e-27Initial program 99.8%
Taylor expanded in x around 0 61.9%
mul-1-neg61.9%
unsub-neg61.9%
Simplified61.9%
if 4.99999994e-27 < x Initial program 100.0%
Taylor expanded in x around 0 36.0%
Final simplification49.9%
(FPCore (x s) :precision binary32 (if (<= x -4.999999873689376e-6) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999873689376e-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 <= (-4.999999873689376e-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(-4.999999873689376e-6)) 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.999999873689376e-6)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.999999873689376 \cdot 10^{-6}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999987e-6Initial program 100.0%
Taylor expanded in x around 0 52.9%
mul-1-neg52.9%
unsub-neg52.9%
Simplified52.9%
Taylor expanded in x around inf 47.5%
associate-*r/47.5%
neg-mul-147.5%
Simplified47.5%
if -4.99999987e-6 < x Initial program 99.9%
Taylor expanded in x around 0 47.9%
Final simplification47.8%
(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 37.7%
Final simplification37.7%
herbie shell --seed 2023258
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))