
(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 (+ (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(x / Float32(-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 -50.0)
0.5
(if (<= t_0 2.0)
(+ 0.5 (* (/ x s) 0.25))
(if (<= t_0 INFINITY)
(/ -1.0 (/ (- (/ x (* s (/ s x))) 4.0) (/ x s)))
(/ 1.0 (/ x s)))))))
float code(float x, float s) {
float t_0 = x / -s;
float tmp;
if (t_0 <= -50.0f) {
tmp = 0.5f;
} else if (t_0 <= 2.0f) {
tmp = 0.5f + ((x / s) * 0.25f);
} else if (t_0 <= ((float) INFINITY)) {
tmp = -1.0f / (((x / (s * (s / x))) - 4.0f) / (x / s));
} else {
tmp = 1.0f / (x / s);
}
return tmp;
}
function code(x, s) t_0 = Float32(x / Float32(-s)) tmp = Float32(0.0) if (t_0 <= Float32(-50.0)) tmp = Float32(0.5); elseif (t_0 <= Float32(2.0)) tmp = Float32(Float32(0.5) + Float32(Float32(x / s) * Float32(0.25))); elseif (t_0 <= Float32(Inf)) tmp = Float32(Float32(-1.0) / Float32(Float32(Float32(x / Float32(s * Float32(s / x))) - Float32(4.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(-50.0)) tmp = single(0.5); elseif (t_0 <= single(2.0)) tmp = single(0.5) + ((x / s) * single(0.25)); elseif (t_0 <= single(Inf)) tmp = single(-1.0) / (((x / (s * (s / x))) - single(4.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 -50:\\
\;\;\;\;0.5\\
\mathbf{elif}\;t\_0 \leq 2:\\
\;\;\;\;0.5 + \frac{x}{s} \cdot 0.25\\
\mathbf{elif}\;t\_0 \leq \infty:\\
\;\;\;\;\frac{-1}{\frac{\frac{x}{s \cdot \frac{s}{x}} - 4}{\frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -50Initial program 100.0%
Taylor expanded in x around 0 28.1%
if -50 < (/.f32 (neg.f32 x) s) < 2Initial program 99.6%
Taylor expanded in x around 0 94.2%
if 2 < (/.f32 (neg.f32 x) s) < +inf.0Initial program 100.0%
Taylor expanded in x around 0 40.1%
mul-1-neg40.1%
unsub-neg40.1%
Simplified40.1%
sub-neg40.1%
flip-+45.2%
metadata-eval45.2%
distribute-neg-frac45.2%
distribute-neg-frac45.2%
distribute-neg-frac45.2%
Applied egg-rr45.2%
Taylor expanded in x around inf 45.2%
frac-times51.7%
sqr-neg51.7%
frac-times45.2%
clear-num45.2%
frac-times48.5%
*-un-lft-identity48.5%
Applied egg-rr48.5%
if +inf.0 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 42.6%
mul-1-neg42.6%
unsub-neg42.6%
Simplified42.6%
Taylor expanded in x around inf 16.2%
associate-*r/16.2%
neg-mul-116.2%
Simplified16.2%
add-sqr-sqrt-0.0%
sqrt-unprod25.2%
sqr-neg25.2%
sqrt-unprod16.8%
add-sqr-sqrt16.8%
clear-num18.2%
inv-pow18.2%
Applied egg-rr18.2%
unpow-118.2%
Simplified18.2%
Final simplification55.2%
(FPCore (x s)
:precision binary32
(let* ((t_0 (/ x (- s))))
(if (<= t_0 -1.0)
0.5
(if (<= t_0 INFINITY)
(/ 1.0 (/ (+ 4.0 (* x (* (/ x s) (/ -1.0 s)))) (+ (/ x s) 2.0)))
(/ 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 <= ((float) INFINITY)) {
tmp = 1.0f / ((4.0f + (x * ((x / s) * (-1.0f / s)))) / ((x / s) + 2.0f));
} else {
tmp = 1.0f / (x / s);
}
return tmp;
}
function code(x, s) t_0 = Float32(x / Float32(-s)) tmp = Float32(0.0) if (t_0 <= Float32(-1.0)) tmp = Float32(0.5); elseif (t_0 <= Float32(Inf)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(4.0) + Float32(x * Float32(Float32(x / s) * Float32(Float32(-1.0) / s)))) / Float32(Float32(x / s) + Float32(2.0)))); 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(Inf)) tmp = single(1.0) / ((single(4.0) + (x * ((x / s) * (single(-1.0) / s)))) / ((x / s) + single(2.0))); 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 \infty:\\
\;\;\;\;\frac{1}{\frac{4 + x \cdot \left(\frac{x}{s} \cdot \frac{-1}{s}\right)}{\frac{x}{s} + 2}}\\
\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) < +inf.0Initial program 99.8%
Taylor expanded in x around 0 64.9%
mul-1-neg64.9%
unsub-neg64.9%
Simplified64.9%
sub-neg64.9%
flip-+67.5%
metadata-eval67.5%
distribute-neg-frac67.5%
distribute-neg-frac67.5%
distribute-neg-frac67.5%
Applied egg-rr67.5%
frac-times28.8%
sqr-neg28.8%
frac-times26.5%
clear-num26.5%
frac-times28.2%
*-un-lft-identity28.2%
Applied egg-rr69.2%
*-un-lft-identity69.2%
frac-times67.5%
clear-num67.5%
div-inv68.1%
associate-*l*70.9%
Applied egg-rr70.9%
if +inf.0 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 42.6%
mul-1-neg42.6%
unsub-neg42.6%
Simplified42.6%
Taylor expanded in x around inf 16.2%
associate-*r/16.2%
neg-mul-116.2%
Simplified16.2%
add-sqr-sqrt-0.0%
sqrt-unprod25.2%
sqr-neg25.2%
sqrt-unprod16.8%
add-sqr-sqrt16.8%
clear-num18.2%
inv-pow18.2%
Applied egg-rr18.2%
unpow-118.2%
Simplified18.2%
Final simplification54.9%
(FPCore (x s)
:precision binary32
(let* ((t_0 (/ x (- s))))
(if (<= t_0 -1.0)
0.5
(if (<= t_0 INFINITY)
(/ 1.0 (/ (- 4.0 (* x (/ (/ x s) s))) (+ (/ x s) 2.0)))
(/ 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 <= ((float) INFINITY)) {
tmp = 1.0f / ((4.0f - (x * ((x / s) / s))) / ((x / s) + 2.0f));
} else {
tmp = 1.0f / (x / s);
}
return tmp;
}
function code(x, s) t_0 = Float32(x / Float32(-s)) tmp = Float32(0.0) if (t_0 <= Float32(-1.0)) tmp = Float32(0.5); elseif (t_0 <= Float32(Inf)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(4.0) - Float32(x * Float32(Float32(x / s) / s))) / Float32(Float32(x / s) + Float32(2.0)))); 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(Inf)) tmp = single(1.0) / ((single(4.0) - (x * ((x / s) / s))) / ((x / s) + single(2.0))); 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 \infty:\\
\;\;\;\;\frac{1}{\frac{4 - x \cdot \frac{\frac{x}{s}}{s}}{\frac{x}{s} + 2}}\\
\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) < +inf.0Initial program 99.8%
Taylor expanded in x around 0 64.9%
mul-1-neg64.9%
unsub-neg64.9%
Simplified64.9%
sub-neg64.9%
flip-+67.5%
metadata-eval67.5%
distribute-neg-frac67.5%
distribute-neg-frac67.5%
distribute-neg-frac67.5%
Applied egg-rr67.5%
frac-times28.8%
sqr-neg28.8%
frac-times26.5%
clear-num26.5%
frac-times28.2%
*-un-lft-identity28.2%
Applied egg-rr69.2%
clear-num69.2%
associate-/r/70.9%
associate-/r*70.9%
clear-num70.9%
Applied egg-rr70.9%
if +inf.0 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 42.6%
mul-1-neg42.6%
unsub-neg42.6%
Simplified42.6%
Taylor expanded in x around inf 16.2%
associate-*r/16.2%
neg-mul-116.2%
Simplified16.2%
add-sqr-sqrt-0.0%
sqrt-unprod25.2%
sqr-neg25.2%
sqrt-unprod16.8%
add-sqr-sqrt16.8%
clear-num18.2%
inv-pow18.2%
Applied egg-rr18.2%
unpow-118.2%
Simplified18.2%
Final simplification54.9%
(FPCore (x s)
:precision binary32
(let* ((t_0 (/ x (- s))))
(if (<= t_0 -50.0)
0.5
(if (<= t_0 2.0) (+ 0.5 (* (/ x s) 0.25)) (/ 1.0 (/ x s))))))
float code(float x, float s) {
float t_0 = x / -s;
float tmp;
if (t_0 <= -50.0f) {
tmp = 0.5f;
} else if (t_0 <= 2.0f) {
tmp = 0.5f + ((x / s) * 0.25f);
} 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 <= (-50.0e0)) then
tmp = 0.5e0
else if (t_0 <= 2.0e0) then
tmp = 0.5e0 + ((x / s) * 0.25e0)
else
tmp = 1.0e0 / (x / s)
end if
code = tmp
end function
function code(x, s) t_0 = Float32(x / Float32(-s)) tmp = Float32(0.0) if (t_0 <= Float32(-50.0)) tmp = Float32(0.5); elseif (t_0 <= Float32(2.0)) tmp = Float32(Float32(0.5) + Float32(Float32(x / s) * Float32(0.25))); 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(-50.0)) tmp = single(0.5); elseif (t_0 <= single(2.0)) tmp = single(0.5) + ((x / s) * single(0.25)); 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 -50:\\
\;\;\;\;0.5\\
\mathbf{elif}\;t\_0 \leq 2:\\
\;\;\;\;0.5 + \frac{x}{s} \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -50Initial program 100.0%
Taylor expanded in x around 0 28.1%
if -50 < (/.f32 (neg.f32 x) s) < 2Initial program 99.6%
Taylor expanded in x around 0 94.2%
if 2 < (/.f32 (neg.f32 x) s) Initial program 100.0%
Taylor expanded in x around 0 40.1%
mul-1-neg40.1%
unsub-neg40.1%
Simplified40.1%
Taylor expanded in x around inf 35.8%
associate-*r/35.8%
neg-mul-135.8%
Simplified35.8%
add-sqr-sqrt-0.0%
sqrt-unprod64.2%
sqr-neg64.2%
sqrt-unprod35.8%
add-sqr-sqrt35.8%
clear-num40.1%
inv-pow40.1%
Applied egg-rr40.1%
unpow-140.1%
Simplified40.1%
Final simplification52.6%
(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(x / Float32(-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.8%
Taylor expanded in x around 0 64.9%
mul-1-neg64.9%
unsub-neg64.9%
Simplified64.9%
Final simplification51.1%
(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(x / Float32(-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 52.7%
if 2 < (/.f32 (neg.f32 x) s) Initial program 100.0%
Taylor expanded in x around 0 40.1%
mul-1-neg40.1%
unsub-neg40.1%
Simplified40.1%
Taylor expanded in x around inf 35.8%
associate-*r/35.8%
neg-mul-135.8%
Simplified35.8%
add-sqr-sqrt-0.0%
sqrt-unprod64.2%
sqr-neg64.2%
sqrt-unprod35.8%
add-sqr-sqrt35.8%
clear-num40.1%
inv-pow40.1%
Applied egg-rr40.1%
unpow-140.1%
Simplified40.1%
Final simplification48.7%
(FPCore (x s) :precision binary32 (if (<= x -0.0012000000569969416) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -0.0012000000569969416f) {
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 <= (-0.0012000000569969416e0)) 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(-0.0012000000569969416)) 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(-0.0012000000569969416)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.0012000000569969416:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -0.00120000006Initial program 100.0%
Taylor expanded in x around 0 49.8%
mul-1-neg49.8%
unsub-neg49.8%
Simplified49.8%
Taylor expanded in x around inf 44.0%
associate-*r/44.0%
neg-mul-144.0%
Simplified44.0%
add-sqr-sqrt-0.0%
sqrt-unprod60.7%
sqr-neg60.7%
sqrt-unprod44.0%
add-sqr-sqrt44.0%
add044.0%
Applied egg-rr44.0%
add044.0%
Simplified44.0%
if -0.00120000006 < x Initial program 99.8%
Taylor expanded in x around 0 48.2%
Final simplification47.2%
(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 38.0%
Final simplification38.0%
herbie shell --seed 2024046
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))