
(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.9%
distribute-frac-neg99.9%
exp-neg99.8%
add-sqr-sqrt46.4%
sqrt-unprod57.4%
sqr-neg57.4%
sqrt-unprod13.3%
add-sqr-sqrt25.9%
add-cbrt-cube25.9%
pow1/325.9%
pow-flip25.9%
Applied egg-rr99.5%
metadata-eval99.5%
pow-sqr99.5%
Simplified99.5%
add-sqr-sqrt99.5%
sqrt-unprod99.5%
pow299.5%
pow299.5%
pow-prod-up99.5%
pow-pow99.5%
metadata-eval99.5%
metadata-eval99.5%
Applied egg-rr99.5%
exp-prod99.5%
*-commutative99.5%
associate-*r/99.5%
Simplified99.5%
Taylor expanded in x around inf 99.8%
add-exp-log99.8%
log-rec99.8%
log1p-udef99.8%
unpow299.8%
*-commutative99.8%
pow-exp99.8%
*-commutative99.8%
pow-exp99.8%
pow-prod-up99.8%
metadata-eval99.8%
Applied egg-rr99.8%
*-rgt-identity99.8%
*-rgt-identity99.8%
unpow-199.8%
rec-exp99.9%
distribute-neg-frac99.9%
Simplified99.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.9%
Final simplification99.9%
(FPCore (x s)
:precision binary32
(let* ((t_0 (/ (- x) s)))
(if (<= t_0 100.0)
0.5
(if (<= t_0 1.9999999360571385e+38)
(/ 1.0 (/ (- 4.0 (/ x (/ (* s (- s)) x))) (+ 2.0 (/ x s))))
(/ 1.0 (/ x s))))))
float code(float x, float s) {
float t_0 = -x / s;
float tmp;
if (t_0 <= 100.0f) {
tmp = 0.5f;
} else if (t_0 <= 1.9999999360571385e+38f) {
tmp = 1.0f / ((4.0f - (x / ((s * -s) / x))) / (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 <= 100.0e0) then
tmp = 0.5e0
else if (t_0 <= 1.9999999360571385e+38) then
tmp = 1.0e0 / ((4.0e0 - (x / ((s * -s) / x))) / (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(100.0)) tmp = Float32(0.5); elseif (t_0 <= Float32(1.9999999360571385e+38)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(4.0) - Float32(x / Float32(Float32(s * Float32(-s)) / x))) / 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(100.0)) tmp = single(0.5); elseif (t_0 <= single(1.9999999360571385e+38)) tmp = single(1.0) / ((single(4.0) - (x / ((s * -s) / x))) / (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 100:\\
\;\;\;\;0.5\\
\mathbf{elif}\;t_0 \leq 1.9999999360571385 \cdot 10^{+38}:\\
\;\;\;\;\frac{1}{\frac{4 - \frac{x}{\frac{s \cdot \left(-s\right)}{x}}}{2 + \frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 100Initial program 99.8%
Taylor expanded in x around 0 52.0%
if 100 < (/.f32 (neg.f32 x) s) < 1.99999994e38Initial program 100.0%
Taylor expanded in x around 0 10.5%
mul-1-neg10.5%
unsub-neg10.5%
Simplified10.5%
sub-neg10.5%
flip-+48.7%
metadata-eval48.7%
distribute-neg-frac48.7%
distribute-neg-frac48.7%
distribute-neg-frac48.7%
Applied egg-rr48.7%
clear-num48.7%
frac-times52.6%
*-un-lft-identity52.6%
add-sqr-sqrt52.6%
sqrt-unprod52.5%
sqr-neg52.5%
sqrt-unprod-0.0%
add-sqr-sqrt52.6%
add-sqr-sqrt52.6%
sqrt-unprod52.5%
sqr-neg52.5%
sqrt-unprod-0.0%
add-sqr-sqrt52.6%
Applied egg-rr52.6%
frac-2neg52.6%
associate-*l/73.7%
add-sqr-sqrt73.7%
sqrt-unprod67.9%
sqr-neg67.9%
sqrt-unprod-0.0%
add-sqr-sqrt73.7%
Applied egg-rr73.7%
if 1.99999994e38 < (/.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%
inv-pow100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
unpow-1100.0%
Simplified100.0%
Final simplification64.3%
(FPCore (x s)
:precision binary32
(let* ((t_0 (/ (- x) s)))
(if (<= t_0 -1.0)
0.5
(if (<= t_0 1.9999999360571385e+38)
(/ 1.0 (/ (- 4.0 (* x (/ (/ x s) 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 <= -1.0f) {
tmp = 0.5f;
} else if (t_0 <= 1.9999999360571385e+38f) {
tmp = 1.0f / ((4.0f - (x * ((x / s) / 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 <= (-1.0e0)) then
tmp = 0.5e0
else if (t_0 <= 1.9999999360571385e+38) then
tmp = 1.0e0 / ((4.0e0 - (x * ((x / s) / 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(-1.0)) tmp = Float32(0.5); elseif (t_0 <= Float32(1.9999999360571385e+38)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(4.0) - Float32(x * Float32(Float32(x / s) / 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(-1.0)) tmp = single(0.5); elseif (t_0 <= single(1.9999999360571385e+38)) tmp = single(1.0) / ((single(4.0) - (x * ((x / s) / 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 -1:\\
\;\;\;\;0.5\\
\mathbf{elif}\;t_0 \leq 1.9999999360571385 \cdot 10^{+38}:\\
\;\;\;\;\frac{1}{\frac{4 - x \cdot \frac{\frac{x}{s}}{s}}{2 + \frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -1Initial program 99.9%
Taylor expanded in x around 0 28.2%
if -1 < (/.f32 (neg.f32 x) s) < 1.99999994e38Initial program 99.8%
Taylor expanded in x around 0 48.4%
mul-1-neg48.4%
unsub-neg48.4%
Simplified48.4%
sub-neg48.4%
flip-+68.4%
metadata-eval68.4%
distribute-neg-frac68.4%
distribute-neg-frac68.4%
distribute-neg-frac68.4%
Applied egg-rr68.4%
clear-num68.4%
frac-times70.5%
*-un-lft-identity70.5%
add-sqr-sqrt49.3%
sqrt-unprod70.5%
sqr-neg70.5%
sqrt-unprod21.2%
add-sqr-sqrt70.2%
add-sqr-sqrt49.0%
sqrt-unprod70.4%
sqr-neg70.4%
sqrt-unprod21.2%
add-sqr-sqrt70.5%
Applied egg-rr70.5%
clear-num70.5%
associate-/r/77.4%
associate-/r*77.4%
clear-num77.4%
Applied egg-rr77.4%
if 1.99999994e38 < (/.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%
inv-pow100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
unpow-1100.0%
Simplified100.0%
Final simplification63.3%
(FPCore (x s) :precision binary32 (if (<= (/ (- x) s) -1.0) 0.5 (/ 1.0 (- 2.0 (/ x s)))))
float code(float x, float s) {
float tmp;
if ((-x / s) <= -1.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) <= (-1.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(-1.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(-1.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 -1:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -1Initial program 99.9%
Taylor expanded in x around 0 28.2%
if -1 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 58.9%
mul-1-neg58.9%
unsub-neg58.9%
Simplified58.9%
Final simplification48.2%
(FPCore (x s) :precision binary32 (let* ((t_0 (/ (- x) s))) (if (<= t_0 2.0) 0.5 (/ 1.0 t_0))))
float code(float x, float s) {
float t_0 = -x / s;
float tmp;
if (t_0 <= 2.0f) {
tmp = 0.5f;
} else {
tmp = 1.0f / t_0;
}
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 <= 2.0e0) then
tmp = 0.5e0
else
tmp = 1.0e0 / t_0
end if
code = tmp
end function
function code(x, s) t_0 = Float32(Float32(-x) / s) tmp = Float32(0.0) if (t_0 <= Float32(2.0)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / t_0); end return tmp end
function tmp_2 = code(x, s) t_0 = -x / s; tmp = single(0.0); if (t_0 <= single(2.0)) tmp = single(0.5); else tmp = single(1.0) / t_0; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-x}{s}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{t_0}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 2Initial program 99.9%
Taylor expanded in x around 0 52.8%
if 2 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 39.1%
mul-1-neg39.1%
unsub-neg39.1%
Simplified39.1%
Taylor expanded in x around inf 39.1%
mul-1-neg39.1%
distribute-frac-neg39.1%
Simplified39.1%
Final simplification47.1%
(FPCore (x s) :precision binary32 (if (<= x -1.000000013351432e-10) (/ 1.0 (/ x s)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.000000013351432e-10f) {
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 <= (-1.000000013351432e-10)) 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(-1.000000013351432e-10)) 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(-1.000000013351432e-10)) 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 -1.000000013351432 \cdot 10^{-10}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.00000001e-10Initial program 99.9%
Taylor expanded in x around 0 46.9%
mul-1-neg46.9%
unsub-neg46.9%
Simplified46.9%
Taylor expanded in x around inf 46.9%
mul-1-neg46.9%
distribute-frac-neg46.9%
Simplified46.9%
inv-pow46.9%
add-sqr-sqrt46.9%
sqrt-unprod52.8%
sqr-neg52.8%
sqrt-unprod-0.0%
add-sqr-sqrt46.9%
Applied egg-rr46.9%
unpow-146.9%
Simplified46.9%
if -1.00000001e-10 < x Initial program 99.8%
Taylor expanded in x around 0 46.9%
Final simplification46.9%
(FPCore (x s) :precision binary32 (if (<= x -1.000000013351432e-10) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.000000013351432e-10f) {
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.000000013351432e-10)) 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.000000013351432e-10)) 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.000000013351432e-10)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.000000013351432 \cdot 10^{-10}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.00000001e-10Initial program 99.9%
Taylor expanded in x around 0 46.9%
mul-1-neg46.9%
unsub-neg46.9%
Simplified46.9%
Taylor expanded in x around inf 42.7%
associate-*r/42.7%
neg-mul-142.7%
Simplified42.7%
if -1.00000001e-10 < x Initial program 99.8%
Taylor expanded in x around 0 46.9%
Final simplification45.5%
(FPCore (x s) :precision binary32 (if (<= x -1.000000013351432e-10) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.000000013351432e-10f) {
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.000000013351432e-10)) 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.000000013351432e-10)) 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(-1.000000013351432e-10)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.000000013351432 \cdot 10^{-10}:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.00000001e-10Initial program 99.9%
Taylor expanded in x around 0 46.9%
mul-1-neg46.9%
unsub-neg46.9%
Simplified46.9%
Taylor expanded in x around inf 46.9%
mul-1-neg46.9%
distribute-frac-neg46.9%
Simplified46.9%
expm1-log1p-u46.9%
expm1-udef94.5%
clear-num94.5%
add-sqr-sqrt94.5%
sqrt-unprod94.5%
sqr-neg94.5%
sqrt-unprod-0.0%
add-sqr-sqrt93.4%
Applied egg-rr93.4%
expm1-def42.7%
expm1-log1p42.7%
Simplified42.7%
if -1.00000001e-10 < x Initial program 99.8%
Taylor expanded in x around 0 46.9%
Final simplification45.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 33.5%
Final simplification33.5%
herbie shell --seed 2024010
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))