
(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 9 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)))))
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
(let* ((t_0 (/ (- x) s)))
(if (<= t_0 5.999999941330714e-10)
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 <= 5.999999941330714e-10f) {
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 <= 5.999999941330714e-10) 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(5.999999941330714e-10)) 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(5.999999941330714e-10)) 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 5.999999941330714 \cdot 10^{-10}:\\
\;\;\;\;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) < 5.99999994e-10Initial program 100.0%
Taylor expanded in x around 0 49.4%
if 5.99999994e-10 < (/.f32 (neg.f32 x) s) < 1.99999994e38Initial program 99.8%
Taylor expanded in x around 0 22.0%
mul-1-neg22.0%
unsub-neg22.0%
Simplified22.0%
sub-neg22.0%
flip-+59.8%
metadata-eval59.8%
distribute-neg-frac59.8%
distribute-neg-frac59.8%
distribute-neg-frac59.8%
Applied egg-rr59.8%
clear-num48.0%
frac-2neg48.0%
frac-times51.5%
*-un-lft-identity51.5%
remove-double-neg51.5%
add-sqr-sqrt51.5%
sqrt-unprod51.4%
sqr-neg51.4%
sqrt-unprod-0.0%
add-sqr-sqrt51.5%
add-sqr-sqrt-0.0%
sqrt-unprod63.3%
sqr-neg63.3%
sqrt-unprod51.5%
add-sqr-sqrt51.5%
Applied egg-rr63.2%
frac-2neg63.2%
associate-*l/73.7%
add-sqr-sqrt73.7%
sqrt-unprod66.3%
sqr-neg66.3%
sqrt-unprod-0.0%
add-sqr-sqrt73.6%
Applied egg-rr73.6%
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%
associate-/r/87.9%
add-sqr-sqrt87.9%
sqrt-unprod92.2%
sqr-neg92.2%
sqrt-unprod-0.0%
add-sqr-sqrt87.9%
Applied egg-rr87.9%
associate-/r/100.0%
Applied egg-rr100.0%
Final simplification62.0%
(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 (* 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 <= -1.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 <= (-1.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(-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(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(-1.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 -1:\\
\;\;\;\;0.5\\
\mathbf{elif}\;t_0 \leq 1.9999999360571385 \cdot 10^{+38}:\\
\;\;\;\;\frac{1}{\frac{4 - \frac{x}{s \cdot \frac{s}{x}}}{2 + \frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -1Initial program 100.0%
Taylor expanded in x around 0 28.1%
if -1 < (/.f32 (neg.f32 x) s) < 1.99999994e38Initial program 99.8%
Taylor expanded in x around 0 50.7%
mul-1-neg50.7%
unsub-neg50.7%
Simplified50.7%
sub-neg50.7%
flip-+74.4%
metadata-eval74.4%
distribute-neg-frac74.4%
distribute-neg-frac74.4%
distribute-neg-frac74.4%
Applied egg-rr74.4%
clear-num33.3%
frac-2neg33.3%
frac-times35.4%
*-un-lft-identity35.4%
remove-double-neg35.4%
add-sqr-sqrt33.0%
sqrt-unprod35.4%
sqr-neg35.4%
sqrt-unprod2.4%
add-sqr-sqrt35.4%
add-sqr-sqrt-0.0%
sqrt-unprod42.6%
sqr-neg42.6%
sqrt-unprod35.4%
add-sqr-sqrt35.4%
Applied egg-rr76.6%
Taylor expanded in x around 0 76.6%
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%
associate-/r/87.9%
add-sqr-sqrt87.9%
sqrt-unprod92.2%
sqr-neg92.2%
sqrt-unprod-0.0%
add-sqr-sqrt87.9%
Applied egg-rr87.9%
associate-/r/100.0%
Applied egg-rr100.0%
Final simplification59.3%
(FPCore (x s)
:precision binary32
(let* ((t_0 (/ (- x) s)))
(if (<= t_0 2.0)
0.5
(if (<= t_0 1.9999999360571385e+38)
(/ 1.0 (/ (- 4.0 (/ x (* s (/ s x)))) (/ x s)))
(/ 1.0 (/ x s))))))
float code(float x, float s) {
float t_0 = -x / s;
float tmp;
if (t_0 <= 2.0f) {
tmp = 0.5f;
} else if (t_0 <= 1.9999999360571385e+38f) {
tmp = 1.0f / ((4.0f - (x / (s * (s / x)))) / (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 <= 2.0e0) then
tmp = 0.5e0
else if (t_0 <= 1.9999999360571385e+38) then
tmp = 1.0e0 / ((4.0e0 - (x / (s * (s / x)))) / (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(2.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(s * Float32(s / x)))) / 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(2.0)) tmp = single(0.5); elseif (t_0 <= single(1.9999999360571385e+38)) tmp = single(1.0) / ((single(4.0) - (x / (s * (s / x)))) / (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 2:\\
\;\;\;\;0.5\\
\mathbf{elif}\;t_0 \leq 1.9999999360571385 \cdot 10^{+38}:\\
\;\;\;\;\frac{1}{\frac{4 - \frac{x}{s \cdot \frac{s}{x}}}{\frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 2Initial program 99.9%
Taylor expanded in x around 0 50.8%
if 2 < (/.f32 (neg.f32 x) s) < 1.99999994e38Initial program 100.0%
Taylor expanded in x around 0 11.4%
mul-1-neg11.4%
unsub-neg11.4%
Simplified11.4%
sub-neg11.4%
flip-+56.0%
metadata-eval56.0%
distribute-neg-frac56.0%
distribute-neg-frac56.0%
distribute-neg-frac56.0%
Applied egg-rr56.0%
Taylor expanded in x around inf 56.0%
clear-num56.0%
frac-2neg56.0%
frac-times60.1%
*-un-lft-identity60.1%
remove-double-neg60.1%
add-sqr-sqrt60.1%
sqrt-unprod59.9%
sqr-neg59.9%
sqrt-unprod-0.0%
add-sqr-sqrt60.1%
add-sqr-sqrt-0.0%
sqrt-unprod73.9%
sqr-neg73.9%
sqrt-unprod60.1%
add-sqr-sqrt60.1%
Applied egg-rr60.1%
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%
associate-/r/87.9%
add-sqr-sqrt87.9%
sqrt-unprod92.2%
sqr-neg92.2%
sqrt-unprod-0.0%
add-sqr-sqrt87.9%
Applied egg-rr87.9%
associate-/r/100.0%
Applied egg-rr100.0%
Final simplification58.2%
(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 100.0%
Taylor expanded in x around 0 28.1%
if -1 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 59.1%
mul-1-neg59.1%
unsub-neg59.1%
Simplified59.1%
Final simplification46.5%
(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 50.8%
if 2 < (/.f32 (neg.f32 x) s) Initial program 100.0%
Taylor expanded in x around 0 36.2%
mul-1-neg36.2%
unsub-neg36.2%
Simplified36.2%
Taylor expanded in x around inf 36.2%
mul-1-neg36.2%
distribute-frac-neg36.2%
Simplified36.2%
Final simplification45.5%
(FPCore (x s) :precision binary32 (if (<= x -1.9999999949504854e-6) (/ 1.0 (/ x s)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.9999999949504854e-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 <= (-1.9999999949504854e-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(-1.9999999949504854e-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(-1.9999999949504854e-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 -1.9999999949504854 \cdot 10^{-6}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.99999999e-6Initial program 100.0%
Taylor expanded in x around 0 46.4%
mul-1-neg46.4%
unsub-neg46.4%
Simplified46.4%
Taylor expanded in x around inf 46.4%
mul-1-neg46.4%
distribute-frac-neg46.4%
Simplified46.4%
associate-/r/41.7%
add-sqr-sqrt41.7%
sqrt-unprod49.1%
sqr-neg49.1%
sqrt-unprod-0.0%
add-sqr-sqrt41.7%
Applied egg-rr41.7%
associate-/r/46.4%
Applied egg-rr46.4%
if -1.99999999e-6 < x Initial program 99.9%
Taylor expanded in x around 0 44.9%
Final simplification45.3%
(FPCore (x s) :precision binary32 (if (<= x -1.9999999949504854e-6) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.9999999949504854e-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 <= (-1.9999999949504854e-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(-1.9999999949504854e-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(-1.9999999949504854e-6)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.9999999949504854 \cdot 10^{-6}:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.99999999e-6Initial program 100.0%
Taylor expanded in x around 0 46.4%
mul-1-neg46.4%
unsub-neg46.4%
Simplified46.4%
Taylor expanded in x around inf 46.4%
mul-1-neg46.4%
distribute-frac-neg46.4%
Simplified46.4%
clear-num41.7%
frac-2neg41.7%
remove-double-neg41.7%
expm1-log1p-u41.7%
expm1-udef97.3%
add-sqr-sqrt-0.0%
sqrt-unprod95.9%
sqr-neg95.9%
sqrt-unprod95.9%
add-sqr-sqrt95.9%
Applied egg-rr95.9%
expm1-def41.7%
expm1-log1p41.7%
Simplified41.7%
if -1.99999999e-6 < x Initial program 99.9%
Taylor expanded in x around 0 44.9%
Final simplification44.0%
(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 34.6%
Final simplification34.6%
herbie shell --seed 2024021
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))