
(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 13 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(x / Float32(-s)))))) end
\begin{array}{l}
\\
e^{-\mathsf{log1p}\left(e^{\frac{x}{-s}}\right)}
\end{array}
Initial program 99.9%
div-inv99.9%
exp-prod82.0%
neg-mul-182.0%
exp-prod82.0%
pow-pow99.9%
div-inv99.9%
Applied egg-rr99.9%
add-exp-log99.8%
log-rec99.9%
log1p-expm1-u99.9%
log1p-define99.9%
pow-exp99.9%
expm1-log1p-u99.9%
neg-mul-199.9%
distribute-neg-frac299.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (pow (exp -2.0) (/ (/ x s) 2.0)))))
float code(float x, float s) {
return 1.0f / (1.0f + powf(expf(-2.0f), ((x / s) / 2.0f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + (exp((-2.0e0)) ** ((x / s) / 2.0e0)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + (exp(Float32(-2.0)) ^ Float32(Float32(x / s) / Float32(2.0))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + (exp(single(-2.0)) ^ ((x / s) / single(2.0)))); end
\begin{array}{l}
\\
\frac{1}{1 + {\left(e^{-2}\right)}^{\left(\frac{\frac{x}{s}}{2}\right)}}
\end{array}
Initial program 99.9%
div-inv99.9%
exp-prod82.0%
neg-mul-182.0%
exp-prod82.0%
pow-pow99.9%
div-inv99.9%
Applied egg-rr99.9%
add-sqr-sqrt99.8%
sqrt-unprod99.9%
pow-prod-down99.8%
prod-exp99.8%
metadata-eval99.8%
Applied egg-rr99.8%
sqrt-pow199.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (pow (exp -1.0) (/ x s)))))
float code(float x, float s) {
return 1.0f / (1.0f + powf(expf(-1.0f), (x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + (exp((-1.0e0)) ** (x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + (exp(Float32(-1.0)) ^ Float32(x / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + (exp(single(-1.0)) ^ (x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + {\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}}
\end{array}
Initial program 99.9%
div-inv99.9%
exp-prod82.0%
neg-mul-182.0%
exp-prod82.0%
pow-pow99.9%
div-inv99.9%
Applied egg-rr99.9%
Final simplification99.9%
(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(x / Float32(-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 (if (<= (/ x (- s)) 0.05000000074505806) 0.5 (/ 1.0 (* x (* (- (* s 2.0) x) (/ 1.0 (* x s)))))))
float code(float x, float s) {
float tmp;
if ((x / -s) <= 0.05000000074505806f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (x * (((s * 2.0f) - x) * (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) <= 0.05000000074505806e0) then
tmp = 0.5e0
else
tmp = 1.0e0 / (x * (((s * 2.0e0) - x) * (1.0e0 / (x * s))))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(x / Float32(-s)) <= Float32(0.05000000074505806)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(Float32(s * Float32(2.0)) - x) * 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(0.05000000074505806)) tmp = single(0.5); else tmp = single(1.0) / (x * (((s * single(2.0)) - x) * (single(1.0) / (x * s)))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{-s} \leq 0.05000000074505806:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot \left(\left(s \cdot 2 - x\right) \cdot \frac{1}{x \cdot s}\right)}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 0.0500000007Initial program 99.8%
Taylor expanded in x around 0 54.8%
if 0.0500000007 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 44.4%
mul-1-neg44.4%
unsub-neg44.4%
Simplified44.4%
Taylor expanded in x around inf 44.4%
associate-*r/44.4%
metadata-eval44.4%
Simplified44.4%
frac-sub56.3%
div-inv59.5%
*-rgt-identity59.5%
Applied egg-rr59.5%
Final simplification56.4%
(FPCore (x s) :precision binary32 (if (<= (/ x (- s)) 0.05000000074505806) 0.5 (/ 1.0 (* x (/ (- (* s 2.0) x) (* x s))))))
float code(float x, float s) {
float tmp;
if ((x / -s) <= 0.05000000074505806f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (x * (((s * 2.0f) - x) / (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) <= 0.05000000074505806e0) then
tmp = 0.5e0
else
tmp = 1.0e0 / (x * (((s * 2.0e0) - x) / (x * s)))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(x / Float32(-s)) <= Float32(0.05000000074505806)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(Float32(s * Float32(2.0)) - x) / Float32(x * s)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((x / -s) <= single(0.05000000074505806)) tmp = single(0.5); else tmp = single(1.0) / (x * (((s * single(2.0)) - x) / (x * s))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{-s} \leq 0.05000000074505806:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot \frac{s \cdot 2 - x}{x \cdot s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 0.0500000007Initial program 99.8%
Taylor expanded in x around 0 54.8%
if 0.0500000007 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 44.4%
mul-1-neg44.4%
unsub-neg44.4%
Simplified44.4%
Taylor expanded in x around inf 44.4%
associate-*r/44.4%
metadata-eval44.4%
Simplified44.4%
frac-sub56.3%
div-inv59.5%
*-rgt-identity59.5%
Applied egg-rr59.5%
associate-*r/56.3%
*-rgt-identity56.3%
Simplified56.3%
Final simplification55.3%
(FPCore (x s) :precision binary32 (if (<= (/ x (- s)) -1.0) (/ 1.0 (* x (/ 2.0 x))) (/ 1.0 (+ -1.0 (- 3.0 (/ x s))))))
float code(float x, float s) {
float tmp;
if ((x / -s) <= -1.0f) {
tmp = 1.0f / (x * (2.0f / x));
} else {
tmp = 1.0f / (-1.0f + (3.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 = 1.0e0 / (x * (2.0e0 / x))
else
tmp = 1.0e0 / ((-1.0e0) + (3.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(Float32(1.0) / Float32(x * Float32(Float32(2.0) / x))); else tmp = Float32(Float32(1.0) / Float32(Float32(-1.0) + Float32(Float32(3.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(1.0) / (x * (single(2.0) / x)); else tmp = single(1.0) / (single(-1.0) + (single(3.0) - (x / s))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{-s} \leq -1:\\
\;\;\;\;\frac{1}{x \cdot \frac{2}{x}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{-1 + \left(3 - \frac{x}{s}\right)}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -1Initial program 99.9%
Taylor expanded in x around 0 5.1%
mul-1-neg5.1%
unsub-neg5.1%
Simplified5.1%
Taylor expanded in x around inf 5.1%
associate-*r/5.1%
metadata-eval5.1%
Simplified5.1%
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 67.9%
mul-1-neg67.9%
unsub-neg67.9%
Simplified67.9%
expm1-log1p-u67.9%
Applied egg-rr67.9%
expm1-undefine67.9%
sub-neg67.9%
log1p-undefine67.9%
rem-exp-log68.0%
associate-+r-68.0%
metadata-eval68.0%
metadata-eval68.0%
Simplified68.0%
Final simplification52.9%
(FPCore (x s) :precision binary32 (if (<= (/ x (- s)) -1.0) (/ 1.0 (* x (/ 2.0 x))) (/ 1.0 (- 2.0 (/ x s)))))
float code(float x, float s) {
float tmp;
if ((x / -s) <= -1.0f) {
tmp = 1.0f / (x * (2.0f / x));
} 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 = 1.0e0 / (x * (2.0e0 / x))
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(Float32(1.0) / Float32(x * Float32(Float32(2.0) / x))); 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(1.0) / (x * (single(2.0) / x)); 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:\\
\;\;\;\;\frac{1}{x \cdot \frac{2}{x}}\\
\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 5.1%
mul-1-neg5.1%
unsub-neg5.1%
Simplified5.1%
Taylor expanded in x around inf 5.1%
associate-*r/5.1%
metadata-eval5.1%
Simplified5.1%
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 67.9%
mul-1-neg67.9%
unsub-neg67.9%
Simplified67.9%
Final simplification52.8%
(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 54.4%
if 2 < (/.f32 (neg.f32 x) s) Initial program 99.9%
Taylor expanded in x around 0 44.7%
mul-1-neg44.7%
unsub-neg44.7%
Simplified44.7%
Taylor expanded in x around inf 44.7%
associate-*r/44.7%
neg-mul-144.7%
Simplified44.7%
Final simplification51.3%
(FPCore (x s) :precision binary32 (if (<= x -200.0) (/ 1.0 (/ x s)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -200.0f) {
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 <= (-200.0e0)) 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(-200.0)) 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(-200.0)) 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 -200:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -200Initial program 100.0%
Taylor expanded in x around 0 68.1%
mul-1-neg68.1%
unsub-neg68.1%
Simplified68.1%
Taylor expanded in x around inf 64.1%
associate-*r/64.1%
neg-mul-164.1%
Simplified64.1%
clear-num68.1%
inv-pow68.1%
add-sqr-sqrt-0.0%
sqrt-unprod73.4%
sqr-neg73.4%
sqrt-unprod68.1%
add-sqr-sqrt68.1%
Applied egg-rr68.1%
unpow-168.1%
Simplified68.1%
if -200 < x Initial program 99.8%
Taylor expanded in x around 0 47.1%
Final simplification51.1%
(FPCore (x s) :precision binary32 (if (<= x -0.009999999776482582) (/ s (- x)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -0.009999999776482582f) {
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.009999999776482582e0)) 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.009999999776482582)) tmp = Float32(s / Float32(-x)); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-0.009999999776482582)) tmp = s / -x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.009999999776482582:\\
\;\;\;\;\frac{s}{-x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -0.00999999978Initial program 99.9%
Taylor expanded in x around 0 64.8%
mul-1-neg64.8%
unsub-neg64.8%
Simplified64.8%
Taylor expanded in x around inf 61.0%
associate-*r/61.0%
neg-mul-161.0%
Simplified61.0%
if -0.00999999978 < x Initial program 99.8%
Taylor expanded in x around 0 47.6%
Final simplification50.4%
(FPCore (x s) :precision binary32 (if (<= x -200.0) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -200.0f) {
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 <= (-200.0e0)) 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(-200.0)) 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(-200.0)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -200:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -200Initial program 100.0%
Taylor expanded in x around 0 68.1%
mul-1-neg68.1%
unsub-neg68.1%
Simplified68.1%
Taylor expanded in x around inf 64.1%
associate-*r/64.1%
neg-mul-164.1%
Simplified64.1%
div-inv64.1%
add-sqr-sqrt-0.0%
sqrt-unprod71.5%
sqr-neg71.5%
sqrt-unprod64.1%
add-sqr-sqrt64.1%
Applied egg-rr64.1%
associate-*r/64.1%
*-rgt-identity64.1%
Simplified64.1%
if -200 < x Initial program 99.8%
Taylor expanded in x around 0 47.1%
Final simplification50.3%
(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 39.3%
Final simplification39.3%
herbie shell --seed 2024077
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))