
(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 10 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 (exp (- (log1p (exp (/ (- x) s))))))
float code(float x, float s) {
return expf(-log1pf(expf((-x / s))));
}
function code(x, s) return exp(Float32(-log1p(exp(Float32(Float32(-x) / s))))) end
\begin{array}{l}
\\
e^{-\mathsf{log1p}\left(e^{\frac{-x}{s}}\right)}
\end{array}
Initial program 99.8%
distribute-frac-neg99.8%
exp-neg99.8%
add-sqr-sqrt55.4%
sqrt-unprod64.7%
sqr-neg64.7%
sqrt-unprod11.9%
add-sqr-sqrt25.8%
pow125.8%
pow125.8%
add-cube-cbrt25.8%
pow325.8%
pow-flip25.8%
Applied egg-rr99.8%
add-exp-log99.8%
log-rec99.8%
log1p-udef99.9%
pow1/399.9%
pow-pow100.0%
metadata-eval100.0%
Applied egg-rr100.0%
unpow-1100.0%
rec-exp100.0%
distribute-frac-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (exp (/ (- x) s)) 1.0)))
float code(float x, float s) {
return 1.0f / (expf((-x / s)) + 1.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (exp((-x / s)) + 1.0e0)
end function
function code(x, s) return Float32(Float32(1.0) / Float32(exp(Float32(Float32(-x) / s)) + Float32(1.0))) end
function tmp = code(x, s) tmp = single(1.0) / (exp((-x / s)) + single(1.0)); end
\begin{array}{l}
\\
\frac{1}{e^{\frac{-x}{s}} + 1}
\end{array}
Initial program 99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (if (<= x -5.000000015855384e-30) (/ 1.0 (+ 2.0 (* s (* (* x (- 0.5 (/ s x))) (/ 1.0 (* (/ s x) (* s s))))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -5.000000015855384e-30f) {
tmp = 1.0f / (2.0f + (s * ((x * (0.5f - (s / x))) * (1.0f / ((s / 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.000000015855384e-30)) then
tmp = 1.0e0 / (2.0e0 + (s * ((x * (0.5e0 - (s / x))) * (1.0e0 / ((s / 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.000000015855384e-30)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(s * Float32(Float32(x * Float32(Float32(0.5) - Float32(s / x))) * Float32(Float32(1.0) / Float32(Float32(s / 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.000000015855384e-30)) tmp = single(1.0) / (single(2.0) + (s * ((x * (single(0.5) - (s / x))) * (single(1.0) / ((s / x) * (s * s)))))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.000000015855384 \cdot 10^{-30}:\\
\;\;\;\;\frac{1}{2 + s \cdot \left(\left(x \cdot \left(0.5 - \frac{s}{x}\right)\right) \cdot \frac{1}{\frac{s}{x} \cdot \left(s \cdot s\right)}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -5.00000002e-30Initial program 99.8%
Taylor expanded in x around 0 82.2%
+-commutative82.2%
mul-1-neg82.2%
unsub-neg82.2%
unpow282.2%
unpow282.2%
times-frac77.1%
Simplified77.1%
clear-num77.1%
frac-times78.9%
*-un-lft-identity78.9%
Applied egg-rr78.9%
associate-*r/78.9%
frac-sub80.0%
*-commutative80.0%
*-commutative80.0%
Applied egg-rr80.0%
*-commutative80.0%
associate-*l*80.0%
distribute-lft-out--80.0%
*-commutative80.0%
*-commutative80.0%
Simplified80.0%
div-inv82.7%
*-commutative82.7%
Applied egg-rr82.7%
associate-*l*89.6%
distribute-lft-out--89.6%
associate-*r*89.6%
Simplified89.6%
if -5.00000002e-30 < x Initial program 99.8%
Taylor expanded in x around 0 46.3%
Final simplification62.9%
(FPCore (x s) :precision binary32 (if (<= x -5.000000015855384e-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 <= -5.000000015855384e-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 <= (-5.000000015855384e-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(-5.000000015855384e-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(-5.000000015855384e-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 -5.000000015855384 \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 < -5.00000002e-30Initial program 99.8%
Taylor expanded in x around 0 82.2%
+-commutative82.2%
mul-1-neg82.2%
unsub-neg82.2%
unpow282.2%
unpow282.2%
times-frac77.1%
Simplified77.1%
frac-times82.2%
associate-/l*85.2%
Applied egg-rr85.2%
if -5.00000002e-30 < x Initial program 99.8%
Taylor expanded in x around 0 46.3%
Final simplification61.2%
(FPCore (x s) :precision binary32 (if (<= x -4.999999841327613e-21) (/ 1.0 (+ 2.0 (* 0.5 (/ (* x x) (* s s))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999841327613e-21f) {
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 <= (-4.999999841327613e-21)) 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(-4.999999841327613e-21)) 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(-4.999999841327613e-21)) 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 -4.999999841327613 \cdot 10^{-21}:\\
\;\;\;\;\frac{1}{2 + 0.5 \cdot \frac{x \cdot x}{s \cdot s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999984e-21Initial program 99.9%
Taylor expanded in x around 0 83.4%
+-commutative83.4%
mul-1-neg83.4%
unsub-neg83.4%
unpow283.4%
unpow283.4%
times-frac77.4%
Simplified77.4%
clear-num77.4%
frac-times77.4%
*-un-lft-identity77.4%
Applied egg-rr77.4%
Taylor expanded in x around inf 82.4%
*-commutative82.4%
unpow282.4%
unpow282.4%
Simplified82.4%
if -4.99999984e-21 < x Initial program 99.8%
Taylor expanded in x around 0 48.1%
Final simplification59.7%
(FPCore (x s) :precision binary32 (if (<= x -9.999999960041972e-13) (* 2.0 (* (/ s x) (/ s x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -9.999999960041972e-13f) {
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 <= (-9.999999960041972e-13)) 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(-9.999999960041972e-13)) 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(-9.999999960041972e-13)) 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 -9.999999960041972 \cdot 10^{-13}:\\
\;\;\;\;2 \cdot \left(\frac{s}{x} \cdot \frac{s}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -9.99999996e-13Initial program 99.9%
Taylor expanded in x around 0 82.6%
+-commutative82.6%
mul-1-neg82.6%
unsub-neg82.6%
unpow282.6%
unpow282.6%
times-frac80.5%
Simplified80.5%
Taylor expanded in x around inf 79.4%
unpow279.4%
unpow279.4%
times-frac76.2%
Simplified76.2%
if -9.99999996e-13 < x Initial program 99.8%
Taylor expanded in x around 0 47.5%
Final simplification56.4%
(FPCore (x s) :precision binary32 (if (<= x -4.999999841327613e-21) (* 2.0 (/ (* s s) (* x x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.999999841327613e-21f) {
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.999999841327613e-21)) 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.999999841327613e-21)) 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(-4.999999841327613e-21)) 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.999999841327613 \cdot 10^{-21}:\\
\;\;\;\;2 \cdot \frac{s \cdot s}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999984e-21Initial program 99.9%
Taylor expanded in x around 0 83.4%
+-commutative83.4%
mul-1-neg83.4%
unsub-neg83.4%
unpow283.4%
unpow283.4%
times-frac77.4%
Simplified77.4%
Taylor expanded in x around inf 77.3%
unpow277.3%
unpow277.3%
times-frac70.3%
Simplified70.3%
frac-times77.3%
Applied egg-rr77.3%
if -4.99999984e-21 < x Initial program 99.8%
Taylor expanded in x around 0 48.1%
Final simplification58.0%
(FPCore (x s) :precision binary32 (if (<= x -1.999999982195158e-37) (/ 1.0 (- 2.0 (/ x s))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.999999982195158e-37f) {
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 <= (-1.999999982195158e-37)) 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(-1.999999982195158e-37)) 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(-1.999999982195158e-37)) 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 -1.999999982195158 \cdot 10^{-37}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.99999998e-37Initial program 99.7%
Taylor expanded in x around 0 51.5%
mul-1-neg51.5%
unsub-neg51.5%
Simplified51.5%
if -1.99999998e-37 < x Initial program 99.9%
Taylor expanded in x around 0 42.8%
Final simplification46.6%
(FPCore (x s) :precision binary32 (if (<= x -1.999999987845058e-8) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.999999987845058e-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 <= (-1.999999987845058e-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(-1.999999987845058e-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(-1.999999987845058e-8)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.999999987845058 \cdot 10^{-8}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.99999999e-8Initial program 100.0%
Taylor expanded in x around 0 45.6%
mul-1-neg45.6%
unsub-neg45.6%
Simplified45.6%
Taylor expanded in x around inf 39.6%
associate-*r/39.6%
neg-mul-139.6%
Simplified39.6%
if -1.99999999e-8 < x Initial program 99.8%
Taylor expanded in x around 0 46.4%
Final simplification44.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.8%
Taylor expanded in x around 0 35.3%
Final simplification35.3%
herbie shell --seed 2023297
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))