
(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 6 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(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 -9999999827968.0) (/ 1.0 (- 2.0 (/ 1.0 (/ s x)))) (/ 1.0 (/ (- 4.0 (* (/ x s) (/ x s))) (+ 2.0 (/ x s))))))
float code(float x, float s) {
float tmp;
if (x <= -9999999827968.0f) {
tmp = 1.0f / (2.0f - (1.0f / (s / x)));
} else {
tmp = 1.0f / ((4.0f - ((x / s) * (x / s))) / (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 <= (-9999999827968.0e0)) then
tmp = 1.0e0 / (2.0e0 - (1.0e0 / (s / x)))
else
tmp = 1.0e0 / ((4.0e0 - ((x / s) * (x / s))) / (2.0e0 + (x / s)))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-9999999827968.0)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(Float32(1.0) / Float32(s / x)))); else tmp = Float32(Float32(1.0) / Float32(Float32(Float32(4.0) - Float32(Float32(x / s) * Float32(x / s))) / Float32(Float32(2.0) + Float32(x / s)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-9999999827968.0)) tmp = single(1.0) / (single(2.0) - (single(1.0) / (s / x))); else tmp = single(1.0) / ((single(4.0) - ((x / s) * (x / s))) / (single(2.0) + (x / s))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9999999827968:\\
\;\;\;\;\frac{1}{2 - \frac{1}{\frac{s}{x}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{4 - \frac{x}{s} \cdot \frac{x}{s}}{2 + \frac{x}{s}}}\\
\end{array}
\end{array}
if x < -9999999830000Initial program 100.0%
Taylor expanded in x around 0 78.7%
neg-mul-178.7%
unsub-neg78.7%
Simplified78.7%
clear-num78.7%
associate-/r/78.7%
Applied egg-rr78.7%
associate-*l/78.7%
*-un-lft-identity78.7%
clear-num78.7%
Applied egg-rr78.7%
if -9999999830000 < x Initial program 99.8%
Taylor expanded in x around 0 36.4%
neg-mul-136.4%
unsub-neg36.4%
Simplified36.4%
clear-num36.4%
associate-/r/36.4%
Applied egg-rr36.4%
sub-neg36.4%
flip-+45.0%
metadata-eval45.0%
associate-*l/45.0%
*-un-lft-identity45.0%
associate-*l/45.0%
*-un-lft-identity45.0%
distribute-neg-frac245.0%
distribute-neg-frac245.0%
associate-*l/45.0%
*-un-lft-identity45.0%
distribute-neg-frac245.0%
Applied egg-rr45.0%
Final simplification49.1%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (- 3.0 (/ x s)) -1.0)))
float code(float x, float s) {
return 1.0f / ((3.0f - (x / s)) + -1.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((3.0e0 - (x / s)) + (-1.0e0))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(3.0) - Float32(x / s)) + Float32(-1.0))) end
function tmp = code(x, s) tmp = single(1.0) / ((single(3.0) - (x / s)) + single(-1.0)); end
\begin{array}{l}
\\
\frac{1}{\left(3 - \frac{x}{s}\right) + -1}
\end{array}
Initial program 99.9%
Taylor expanded in x around 0 41.6%
neg-mul-141.6%
unsub-neg41.6%
Simplified41.6%
clear-num41.6%
associate-/r/41.6%
Applied egg-rr41.6%
expm1-log1p-u39.6%
associate-*l/39.6%
*-un-lft-identity39.6%
Applied egg-rr39.6%
expm1-undefine39.6%
sub-neg39.6%
log1p-undefine39.6%
rem-exp-log41.5%
associate-+r-41.6%
metadata-eval41.6%
metadata-eval41.6%
Simplified41.6%
(FPCore (x s) :precision binary32 (/ 1.0 (- 2.0 (/ x s))))
float code(float x, float s) {
return 1.0f / (2.0f - (x / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (2.0e0 - (x / s))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(x / s))) end
function tmp = code(x, s) tmp = single(1.0) / (single(2.0) - (x / s)); end
\begin{array}{l}
\\
\frac{1}{2 - \frac{x}{s}}
\end{array}
Initial program 99.9%
Taylor expanded in x around 0 41.6%
neg-mul-141.6%
unsub-neg41.6%
Simplified41.6%
(FPCore (x s) :precision binary32 (+ 0.5 (/ 0.25 (/ s x))))
float code(float x, float s) {
return 0.5f + (0.25f / (s / x));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 + (0.25e0 / (s / x))
end function
function code(x, s) return Float32(Float32(0.5) + Float32(Float32(0.25) / Float32(s / x))) end
function tmp = code(x, s) tmp = single(0.5) + (single(0.25) / (s / x)); end
\begin{array}{l}
\\
0.5 + \frac{0.25}{\frac{s}{x}}
\end{array}
Initial program 99.9%
Taylor expanded in x around 0 32.0%
associate-*r/32.0%
Simplified32.0%
*-commutative32.0%
associate-/l*32.0%
Applied egg-rr32.0%
*-commutative32.0%
div-inv32.0%
associate-*l*32.0%
associate-*l/32.0%
*-un-lft-identity32.0%
clear-num32.0%
div-inv32.0%
Applied egg-rr32.0%
(FPCore (x s) :precision binary32 (+ 0.5 (* x (/ 0.25 s))))
float code(float x, float s) {
return 0.5f + (x * (0.25f / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 + (x * (0.25e0 / s))
end function
function code(x, s) return Float32(Float32(0.5) + Float32(x * Float32(Float32(0.25) / s))) end
function tmp = code(x, s) tmp = single(0.5) + (x * (single(0.25) / s)); end
\begin{array}{l}
\\
0.5 + x \cdot \frac{0.25}{s}
\end{array}
Initial program 99.9%
Taylor expanded in x around 0 32.0%
associate-*r/32.0%
Simplified32.0%
*-commutative32.0%
associate-/l*32.0%
Applied egg-rr32.0%
herbie shell --seed 2024179
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))