
(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 5 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 (/ -1.0 s))))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((x * (-1.0f / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + exp((x * ((-1.0e0) / s))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(x * Float32(Float32(-1.0) / s))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((x * (single(-1.0) / s)))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{x \cdot \frac{-1}{s}}}
\end{array}
Initial program 99.8%
frac-2neg99.8%
div-inv99.8%
remove-double-neg99.8%
Applied egg-rr99.8%
Final simplification99.8%
(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.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (if (<= (- x) -5.00000011871114e-33) 0.5 (/ 1.0 (- 2.0 (/ x s)))))
float code(float x, float s) {
float tmp;
if (-x <= -5.00000011871114e-33f) {
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 <= (-5.00000011871114e-33)) 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(-5.00000011871114e-33)) 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 <= single(-5.00000011871114e-33)) 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}\;-x \leq -5.00000011871114 \cdot 10^{-33}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\end{array}
\end{array}
if (neg.f32 x) < -5.00000012e-33Initial program 99.9%
Taylor expanded in x around 0 41.7%
if -5.00000012e-33 < (neg.f32 x) Initial program 99.7%
Taylor expanded in x around 0 61.2%
neg-mul-161.2%
unsub-neg61.2%
Simplified61.2%
(FPCore (x s) :precision binary32 (if (<= (- x) 0.0020000000949949026) 0.5 (- (/ s x))))
float code(float x, float s) {
float tmp;
if (-x <= 0.0020000000949949026f) {
tmp = 0.5f;
} else {
tmp = -(s / x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (-x <= 0.0020000000949949026e0) then
tmp = 0.5e0
else
tmp = -(s / x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(0.0020000000949949026)) tmp = Float32(0.5); else tmp = Float32(-Float32(s / x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(0.0020000000949949026)) tmp = single(0.5); else tmp = -(s / x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq 0.0020000000949949026:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;-\frac{s}{x}\\
\end{array}
\end{array}
if (neg.f32 x) < 0.00200000009Initial program 99.8%
Taylor expanded in x around 0 47.2%
if 0.00200000009 < (neg.f32 x) Initial program 100.0%
Taylor expanded in x around 0 59.3%
neg-mul-159.3%
unsub-neg59.3%
Simplified59.3%
Taylor expanded in x around inf 54.2%
neg-mul-154.2%
distribute-neg-frac254.2%
Simplified54.2%
Final simplification48.7%
(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 38.4%
herbie shell --seed 2024111
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))