
(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 12 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 (if (<= x -4.000000094968912e-32) (/ 1.0 (+ 2.0 (- (* 0.5 (/ x (* (/ 1.0 x) (* s s)))) (/ x s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.000000094968912e-32f) {
tmp = 1.0f / (2.0f + ((0.5f * (x / ((1.0f / x) * (s * s)))) - (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 <= (-4.000000094968912e-32)) then
tmp = 1.0e0 / (2.0e0 + ((0.5e0 * (x / ((1.0e0 / x) * (s * s)))) - (x / s)))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.000000094968912e-32)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(0.5) * Float32(x / Float32(Float32(Float32(1.0) / x) * Float32(s * s)))) - 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(-4.000000094968912e-32)) tmp = single(1.0) / (single(2.0) + ((single(0.5) * (x / ((single(1.0) / x) * (s * s)))) - (x / s))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.000000094968912 \cdot 10^{-32}:\\
\;\;\;\;\frac{1}{2 + \left(0.5 \cdot \frac{x}{\frac{1}{x} \cdot \left(s \cdot s\right)} - \frac{x}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.00000009e-32Initial program 99.9%
Taylor expanded in x around 0 77.6%
mul-1-neg77.6%
unsub-neg77.6%
unpow277.6%
unpow277.6%
times-frac72.3%
Simplified72.3%
frac-times77.6%
associate-/l*84.3%
Applied egg-rr84.3%
clear-num84.3%
associate-/r/84.3%
Applied egg-rr84.3%
if -4.00000009e-32 < x Initial program 99.9%
Taylor expanded in x around 0 45.3%
Final simplification61.9%
(FPCore (x s) :precision binary32 (if (<= x 9.999999682655225e-21) (/ 1.0 (+ 2.0 (- (* 0.5 (/ x (* s (/ s x)))) (/ x s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= 9.999999682655225e-21f) {
tmp = 1.0f / (2.0f + ((0.5f * (x / (s * (s / x)))) - (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 <= 9.999999682655225e-21) then
tmp = 1.0e0 / (2.0e0 + ((0.5e0 * (x / (s * (s / x)))) - (x / s)))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(9.999999682655225e-21)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(0.5) * Float32(x / Float32(s * Float32(s / x)))) - 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(9.999999682655225e-21)) tmp = single(1.0) / (single(2.0) + ((single(0.5) * (x / (s * (s / x)))) - (x / s))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.999999682655225 \cdot 10^{-21}:\\
\;\;\;\;\frac{1}{2 + \left(0.5 \cdot \frac{x}{s \cdot \frac{s}{x}} - \frac{x}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < 9.99999968e-21Initial program 99.9%
Taylor expanded in x around 0 73.0%
mul-1-neg73.0%
unsub-neg73.0%
unpow273.0%
unpow273.0%
times-frac74.6%
Simplified74.6%
clear-num74.6%
frac-times79.6%
*-un-lft-identity79.6%
Applied egg-rr79.6%
if 9.99999968e-21 < x Initial program 100.0%
Taylor expanded in x around 0 32.2%
Final simplification60.3%
(FPCore (x s) :precision binary32 (if (<= x -4.000000094968912e-32) (/ 1.0 (+ 2.0 (- (* 0.5 (/ x (/ (* s s) x))) (/ x s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.000000094968912e-32f) {
tmp = 1.0f / (2.0f + ((0.5f * (x / ((s * s) / x))) - (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 <= (-4.000000094968912e-32)) then
tmp = 1.0e0 / (2.0e0 + ((0.5e0 * (x / ((s * s) / x))) - (x / s)))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.000000094968912e-32)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(0.5) * Float32(x / Float32(Float32(s * s) / x))) - 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(-4.000000094968912e-32)) tmp = single(1.0) / (single(2.0) + ((single(0.5) * (x / ((s * s) / x))) - (x / s))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.000000094968912 \cdot 10^{-32}:\\
\;\;\;\;\frac{1}{2 + \left(0.5 \cdot \frac{x}{\frac{s \cdot s}{x}} - \frac{x}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.00000009e-32Initial program 99.9%
Taylor expanded in x around 0 77.6%
mul-1-neg77.6%
unsub-neg77.6%
unpow277.6%
unpow277.6%
times-frac72.3%
Simplified72.3%
frac-times77.6%
associate-/l*84.3%
Applied egg-rr84.3%
if -4.00000009e-32 < x Initial program 99.9%
Taylor expanded in x around 0 45.3%
Final simplification61.9%
(FPCore (x s) :precision binary32 (if (<= x -5.000000156871975e-23) (/ 1.0 (+ 2.0 (/ (* 0.5 (* x x)) (* s s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -5.000000156871975e-23f) {
tmp = 1.0f / (2.0f + ((0.5f * (x * x)) / (s * 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 <= (-5.000000156871975e-23)) then
tmp = 1.0e0 / (2.0e0 + ((0.5e0 * (x * x)) / (s * s)))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-5.000000156871975e-23)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(0.5) * Float32(x * x)) / Float32(s * s)))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-5.000000156871975e-23)) tmp = single(1.0) / (single(2.0) + ((single(0.5) * (x * x)) / (s * s))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.000000156871975 \cdot 10^{-23}:\\
\;\;\;\;\frac{1}{2 + \frac{0.5 \cdot \left(x \cdot x\right)}{s \cdot s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -5.00000016e-23Initial program 99.9%
Taylor expanded in x around 0 84.2%
mul-1-neg84.2%
unsub-neg84.2%
unpow284.2%
unpow284.2%
times-frac74.1%
Simplified74.1%
clear-num74.1%
frac-times80.0%
*-un-lft-identity80.0%
Applied egg-rr80.0%
Taylor expanded in x around inf 83.1%
unpow283.1%
unpow283.1%
associate-*r/83.1%
Simplified83.1%
if -5.00000016e-23 < x Initial program 99.9%
Taylor expanded in x around 0 47.6%
Final simplification59.4%
(FPCore (x s) :precision binary32 (if (<= x -7.999999968033578e-12) (* 2.0 (* (/ s x) (/ s x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -7.999999968033578e-12f) {
tmp = 2.0f * ((s / x) * (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 <= (-7.999999968033578e-12)) then
tmp = 2.0e0 * ((s / x) * (s / x))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-7.999999968033578e-12)) tmp = Float32(Float32(2.0) * Float32(Float32(s / x) * 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(-7.999999968033578e-12)) tmp = single(2.0) * ((s / x) * (s / x)); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.999999968033578 \cdot 10^{-12}:\\
\;\;\;\;2 \cdot \left(\frac{s}{x} \cdot \frac{s}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -7.99999997e-12Initial program 100.0%
Taylor expanded in x around 0 80.6%
mul-1-neg80.6%
unsub-neg80.6%
unpow280.6%
unpow280.6%
times-frac78.2%
Simplified78.2%
Taylor expanded in x around inf 80.1%
unpow280.1%
unpow280.1%
times-frac76.6%
Simplified76.6%
if -7.99999997e-12 < x Initial program 99.9%
Taylor expanded in x around 0 47.9%
Final simplification55.1%
(FPCore (x s) :precision binary32 (if (<= x -7.999999968033578e-12) (* 2.0 (/ s (/ x (/ s x)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -7.999999968033578e-12f) {
tmp = 2.0f * (s / (x / (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 <= (-7.999999968033578e-12)) then
tmp = 2.0e0 * (s / (x / (s / x)))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-7.999999968033578e-12)) tmp = Float32(Float32(2.0) * Float32(s / Float32(x / 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(-7.999999968033578e-12)) tmp = single(2.0) * (s / (x / (s / x))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.999999968033578 \cdot 10^{-12}:\\
\;\;\;\;2 \cdot \frac{s}{\frac{x}{\frac{s}{x}}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -7.99999997e-12Initial program 100.0%
Taylor expanded in x around 0 80.6%
mul-1-neg80.6%
unsub-neg80.6%
unpow280.6%
unpow280.6%
times-frac78.2%
Simplified78.2%
Taylor expanded in x around inf 80.1%
unpow280.1%
unpow280.1%
times-frac76.6%
Simplified76.6%
associate-*l/79.1%
associate-/l*77.0%
Applied egg-rr77.0%
if -7.99999997e-12 < x Initial program 99.9%
Taylor expanded in x around 0 47.9%
Final simplification55.1%
(FPCore (x s) :precision binary32 (if (<= x -4.99999991225835e-15) (* (/ (* s s) x) (/ 2.0 x)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.99999991225835e-15f) {
tmp = ((s * s) / x) * (2.0f / 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 <= (-4.99999991225835e-15)) then
tmp = ((s * s) / x) * (2.0e0 / x)
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.99999991225835e-15)) tmp = Float32(Float32(Float32(s * s) / x) * Float32(Float32(2.0) / x)); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-4.99999991225835e-15)) tmp = ((s * s) / x) * (single(2.0) / x); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.99999991225835 \cdot 10^{-15}:\\
\;\;\;\;\frac{s \cdot s}{x} \cdot \frac{2}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999991e-15Initial program 100.0%
Taylor expanded in x around 0 82.7%
mul-1-neg82.7%
unsub-neg82.7%
unpow282.7%
unpow282.7%
times-frac76.9%
Simplified76.9%
Taylor expanded in x around inf 77.3%
unpow277.3%
unpow277.3%
times-frac69.9%
Simplified69.9%
*-commutative69.9%
frac-times77.3%
associate-*l/77.4%
Applied egg-rr77.4%
times-frac77.4%
Applied egg-rr77.4%
if -4.99999991e-15 < x Initial program 99.9%
Taylor expanded in x around 0 48.0%
Final simplification56.3%
(FPCore (x s) :precision binary32 (if (<= x -4.99999991225835e-15) (/ (* 2.0 (* s s)) (* x x)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -4.99999991225835e-15f) {
tmp = (2.0f * (s * s)) / (x * 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 <= (-4.99999991225835e-15)) then
tmp = (2.0e0 * (s * s)) / (x * x)
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4.99999991225835e-15)) tmp = Float32(Float32(Float32(2.0) * Float32(s * s)) / Float32(x * x)); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-4.99999991225835e-15)) tmp = (single(2.0) * (s * s)) / (x * x); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.99999991225835 \cdot 10^{-15}:\\
\;\;\;\;\frac{2 \cdot \left(s \cdot s\right)}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -4.99999991e-15Initial program 100.0%
Taylor expanded in x around 0 82.7%
mul-1-neg82.7%
unsub-neg82.7%
unpow282.7%
unpow282.7%
times-frac76.9%
Simplified76.9%
Taylor expanded in x around inf 77.3%
unpow277.3%
unpow277.3%
times-frac69.9%
Simplified69.9%
*-commutative69.9%
frac-times77.3%
associate-*l/77.4%
Applied egg-rr77.4%
if -4.99999991e-15 < x Initial program 99.9%
Taylor expanded in x around 0 48.0%
Final simplification56.3%
(FPCore (x s) :precision binary32 (if (<= x 3.999999964390316e-37) (/ 1.0 (- 2.0 (/ x s))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= 3.999999964390316e-37f) {
tmp = 1.0f / (2.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 <= 3.999999964390316e-37) then
tmp = 1.0e0 / (2.0e0 - (x / s))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(3.999999964390316e-37)) tmp = Float32(Float32(1.0) / Float32(Float32(2.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(3.999999964390316e-37)) tmp = single(1.0) / (single(2.0) - (x / s)); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.999999964390316 \cdot 10^{-37}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < 3.99999996e-37Initial program 99.9%
Taylor expanded in x around 0 55.8%
mul-1-neg55.8%
unsub-neg55.8%
Simplified55.8%
if 3.99999996e-37 < x Initial program 99.9%
Taylor expanded in x around 0 40.6%
Final simplification47.9%
(FPCore (x s) :precision binary32 (if (<= x -7.999999968033578e-12) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -7.999999968033578e-12f) {
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 <= (-7.999999968033578e-12)) 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(-7.999999968033578e-12)) tmp = Float32(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(-7.999999968033578e-12)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.999999968033578 \cdot 10^{-12}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -7.99999997e-12Initial program 100.0%
Taylor expanded in x around 0 43.8%
mul-1-neg43.8%
unsub-neg43.8%
Simplified43.8%
Taylor expanded in x around inf 42.5%
associate-*r/42.5%
neg-mul-142.5%
Simplified42.5%
if -7.99999997e-12 < x Initial program 99.9%
Taylor expanded in x around 0 47.9%
Final simplification46.5%
(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 37.5%
Final simplification37.5%
herbie shell --seed 2023200
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))