
(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 8 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%
div-inv99.8%
exp-prod81.2%
neg-mul-181.2%
exp-prod81.2%
pow-pow99.8%
div-inv99.8%
Applied egg-rr99.8%
add-exp-log99.8%
log-rec99.8%
log1p-udef99.9%
pow-exp99.9%
neg-mul-199.9%
distribute-neg-frac99.9%
Applied egg-rr99.9%
Final simplification99.9%
(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
(let* ((t_0 (/ (- x) s)))
(if (<= t_0 0.0020000000949949026)
0.5
(if (<= t_0 1.9999999867631625e+37)
(/ 1.0 (/ (- 4.0 (/ x (* (/ 1.0 x) (/ s (/ 1.0 s))))) (+ 2.0 (/ x s))))
(/ -1.0 (/ x s))))))
float code(float x, float s) {
float t_0 = -x / s;
float tmp;
if (t_0 <= 0.0020000000949949026f) {
tmp = 0.5f;
} else if (t_0 <= 1.9999999867631625e+37f) {
tmp = 1.0f / ((4.0f - (x / ((1.0f / x) * (s / (1.0f / s))))) / (2.0f + (x / s)));
} else {
tmp = -1.0f / (x / s);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: tmp
t_0 = -x / s
if (t_0 <= 0.0020000000949949026e0) then
tmp = 0.5e0
else if (t_0 <= 1.9999999867631625e+37) then
tmp = 1.0e0 / ((4.0e0 - (x / ((1.0e0 / x) * (s / (1.0e0 / s))))) / (2.0e0 + (x / s)))
else
tmp = (-1.0e0) / (x / s)
end if
code = tmp
end function
function code(x, s) t_0 = Float32(Float32(-x) / s) tmp = Float32(0.0) if (t_0 <= Float32(0.0020000000949949026)) tmp = Float32(0.5); elseif (t_0 <= Float32(1.9999999867631625e+37)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(4.0) - Float32(x / Float32(Float32(Float32(1.0) / x) * Float32(s / Float32(Float32(1.0) / s))))) / Float32(Float32(2.0) + Float32(x / s)))); else tmp = Float32(Float32(-1.0) / Float32(x / s)); end return tmp end
function tmp_2 = code(x, s) t_0 = -x / s; tmp = single(0.0); if (t_0 <= single(0.0020000000949949026)) tmp = single(0.5); elseif (t_0 <= single(1.9999999867631625e+37)) tmp = single(1.0) / ((single(4.0) - (x / ((single(1.0) / x) * (s / (single(1.0) / s))))) / (single(2.0) + (x / s))); else tmp = single(-1.0) / (x / s); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-x}{s}\\
\mathbf{if}\;t_0 \leq 0.0020000000949949026:\\
\;\;\;\;0.5\\
\mathbf{elif}\;t_0 \leq 1.9999999867631625 \cdot 10^{+37}:\\
\;\;\;\;\frac{1}{\frac{4 - \frac{x}{\frac{1}{x} \cdot \frac{s}{\frac{1}{s}}}}{2 + \frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 0.00200000009Initial program 99.9%
Taylor expanded in x around 0 52.3%
if 0.00200000009 < (/.f32 (neg.f32 x) s) < 1.99999999e37Initial program 99.6%
Taylor expanded in x around 0 13.6%
mul-1-neg13.6%
unsub-neg13.6%
Simplified13.6%
sub-neg13.6%
neg-mul-113.6%
rem-log-exp94.1%
pow-exp94.1%
flip-+4.5%
metadata-eval4.5%
pow-exp4.5%
rem-log-exp4.5%
pow-exp4.5%
rem-log-exp5.9%
neg-mul-15.9%
distribute-neg-frac5.9%
neg-mul-15.9%
distribute-neg-frac5.9%
pow-exp5.9%
rem-log-exp55.5%
neg-mul-155.5%
distribute-neg-frac55.5%
Applied egg-rr55.5%
associate-*l/55.5%
associate-/l*56.9%
add-sqr-sqrt56.9%
sqrt-unprod56.8%
sqr-neg56.8%
sqrt-unprod-0.0%
add-sqr-sqrt56.5%
add-sqr-sqrt56.5%
sqrt-unprod56.5%
sqr-neg56.5%
sqrt-unprod-0.0%
add-sqr-sqrt56.9%
Applied egg-rr56.9%
*-un-lft-identity56.9%
div-inv56.9%
times-frac78.6%
Applied egg-rr78.6%
if 1.99999999e37 < (/.f32 (neg.f32 x) s) Initial program 100.0%
Taylor expanded in x around 0 100.0%
mul-1-neg100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in x around inf 100.0%
mul-1-neg100.0%
distribute-frac-neg100.0%
Simplified100.0%
associate-/r/90.9%
add-sqr-sqrt90.9%
sqrt-unprod96.7%
sqr-neg96.7%
sqrt-unprod-0.0%
add-sqr-sqrt90.9%
Applied egg-rr90.9%
frac-2neg90.9%
metadata-eval90.9%
associate-/r/100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification66.6%
(FPCore (x s)
:precision binary32
(let* ((t_0 (/ (- x) s)))
(if (<= t_0 -4.0)
0.5
(if (<= t_0 1.9999999867631625e+37)
(/ 1.0 (/ (- 4.0 (/ x (/ s (/ x s)))) (+ 2.0 (/ x s))))
(/ -1.0 (/ x s))))))
float code(float x, float s) {
float t_0 = -x / s;
float tmp;
if (t_0 <= -4.0f) {
tmp = 0.5f;
} else if (t_0 <= 1.9999999867631625e+37f) {
tmp = 1.0f / ((4.0f - (x / (s / (x / s)))) / (2.0f + (x / s)));
} else {
tmp = -1.0f / (x / s);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: tmp
t_0 = -x / s
if (t_0 <= (-4.0e0)) then
tmp = 0.5e0
else if (t_0 <= 1.9999999867631625e+37) then
tmp = 1.0e0 / ((4.0e0 - (x / (s / (x / s)))) / (2.0e0 + (x / s)))
else
tmp = (-1.0e0) / (x / s)
end if
code = tmp
end function
function code(x, s) t_0 = Float32(Float32(-x) / s) tmp = Float32(0.0) if (t_0 <= Float32(-4.0)) tmp = Float32(0.5); elseif (t_0 <= Float32(1.9999999867631625e+37)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(4.0) - Float32(x / Float32(s / Float32(x / s)))) / Float32(Float32(2.0) + Float32(x / s)))); else tmp = Float32(Float32(-1.0) / Float32(x / s)); end return tmp end
function tmp_2 = code(x, s) t_0 = -x / s; tmp = single(0.0); if (t_0 <= single(-4.0)) tmp = single(0.5); elseif (t_0 <= single(1.9999999867631625e+37)) tmp = single(1.0) / ((single(4.0) - (x / (s / (x / s)))) / (single(2.0) + (x / s))); else tmp = single(-1.0) / (x / s); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-x}{s}\\
\mathbf{if}\;t_0 \leq -4:\\
\;\;\;\;0.5\\
\mathbf{elif}\;t_0 \leq 1.9999999867631625 \cdot 10^{+37}:\\
\;\;\;\;\frac{1}{\frac{4 - \frac{x}{\frac{s}{\frac{x}{s}}}}{2 + \frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -4Initial program 100.0%
Taylor expanded in x around 0 28.1%
if -4 < (/.f32 (neg.f32 x) s) < 1.99999999e37Initial program 99.7%
Taylor expanded in x around 0 52.6%
mul-1-neg52.6%
unsub-neg52.6%
Simplified52.6%
sub-neg52.6%
neg-mul-152.6%
rem-log-exp95.7%
pow-exp95.7%
flip-+47.8%
metadata-eval47.8%
pow-exp47.8%
rem-log-exp47.8%
pow-exp47.8%
rem-log-exp48.5%
neg-mul-148.5%
distribute-neg-frac48.5%
neg-mul-148.5%
distribute-neg-frac48.5%
pow-exp48.5%
rem-log-exp75.1%
neg-mul-175.1%
distribute-neg-frac75.1%
Applied egg-rr75.1%
associate-*l/75.1%
associate-/l*75.8%
add-sqr-sqrt56.7%
sqrt-unprod76.1%
sqr-neg76.1%
sqrt-unprod19.1%
add-sqr-sqrt75.6%
add-sqr-sqrt56.5%
sqrt-unprod75.9%
sqr-neg75.9%
sqrt-unprod19.1%
add-sqr-sqrt75.8%
Applied egg-rr75.8%
if 1.99999999e37 < (/.f32 (neg.f32 x) s) Initial program 100.0%
Taylor expanded in x around 0 100.0%
mul-1-neg100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in x around inf 100.0%
mul-1-neg100.0%
distribute-frac-neg100.0%
Simplified100.0%
associate-/r/90.9%
add-sqr-sqrt90.9%
sqrt-unprod96.7%
sqr-neg96.7%
sqrt-unprod-0.0%
add-sqr-sqrt90.9%
Applied egg-rr90.9%
frac-2neg90.9%
metadata-eval90.9%
associate-/r/100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification62.6%
(FPCore (x s) :precision binary32 (if (<= (/ (- x) s) -4.0) 0.5 (/ 1.0 (- 2.0 (/ x s)))))
float code(float x, float s) {
float tmp;
if ((-x / s) <= -4.0f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (2.0f - (x / s));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if ((-x / s) <= (-4.0e0)) then
tmp = 0.5e0
else
tmp = 1.0e0 / (2.0e0 - (x / s))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(Float32(-x) / s) <= Float32(-4.0)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(x / s))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((-x / s) <= single(-4.0)) tmp = single(0.5); else tmp = single(1.0) / (single(2.0) - (x / s)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-x}{s} \leq -4:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -4Initial program 100.0%
Taylor expanded in x around 0 28.1%
if -4 < (/.f32 (neg.f32 x) s) Initial program 99.8%
Taylor expanded in x around 0 64.1%
mul-1-neg64.1%
unsub-neg64.1%
Simplified64.1%
Final simplification51.3%
(FPCore (x s) :precision binary32 (if (<= (/ (- x) s) 2.0) 0.5 (/ -1.0 (/ x s))))
float code(float x, float s) {
float tmp;
if ((-x / s) <= 2.0f) {
tmp = 0.5f;
} else {
tmp = -1.0f / (x / s);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if ((-x / s) <= 2.0e0) then
tmp = 0.5e0
else
tmp = (-1.0e0) / (x / s)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(Float32(-x) / s) <= Float32(2.0)) tmp = Float32(0.5); else tmp = Float32(Float32(-1.0) / Float32(x / s)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((-x / s) <= single(2.0)) tmp = single(0.5); else tmp = single(-1.0) / (x / s); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-x}{s} \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 2Initial program 99.8%
Taylor expanded in x around 0 51.9%
if 2 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 45.4%
mul-1-neg45.4%
unsub-neg45.4%
Simplified45.4%
Taylor expanded in x around inf 45.4%
mul-1-neg45.4%
distribute-frac-neg45.4%
Simplified45.4%
associate-/r/41.9%
add-sqr-sqrt41.9%
sqrt-unprod52.3%
sqr-neg52.3%
sqrt-unprod-0.0%
add-sqr-sqrt41.7%
Applied egg-rr41.7%
frac-2neg41.7%
metadata-eval41.7%
associate-/r/45.3%
add-sqr-sqrt45.3%
sqrt-unprod53.5%
sqr-neg53.5%
sqrt-unprod-0.0%
add-sqr-sqrt45.4%
Applied egg-rr45.4%
Final simplification49.3%
(FPCore (x s) :precision binary32 (if (<= x -9.999999747378752e-5) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -9.999999747378752e-5f) {
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 <= (-9.999999747378752e-5)) 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(-9.999999747378752e-5)) 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(-9.999999747378752e-5)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.999999747378752 \cdot 10^{-5}:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -9.99999975e-5Initial program 100.0%
Taylor expanded in x around 0 56.9%
mul-1-neg56.9%
unsub-neg56.9%
Simplified56.9%
Taylor expanded in x around inf 56.9%
mul-1-neg56.9%
distribute-frac-neg56.9%
Simplified56.9%
expm1-log1p-u56.9%
expm1-udef97.6%
clear-num97.6%
add-sqr-sqrt97.6%
sqrt-unprod97.6%
sqr-neg97.6%
sqrt-unprod-0.0%
add-sqr-sqrt97.6%
Applied egg-rr97.6%
expm1-def52.2%
expm1-log1p52.2%
Simplified52.2%
if -9.99999975e-5 < x Initial program 99.8%
Taylor expanded in x around 0 45.8%
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.8%
Taylor expanded in x around 0 33.8%
Final simplification33.8%
herbie shell --seed 2023322
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))