
(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 -9.999999887266023e-27) (/ 1.0 (+ 2.0 (- (* x (* 0.5 (/ x (* s s)))) (/ x s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -9.999999887266023e-27f) {
tmp = 1.0f / (2.0f + ((x * (0.5f * (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 <= (-9.999999887266023e-27)) then
tmp = 1.0e0 / (2.0e0 + ((x * (0.5e0 * (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(-9.999999887266023e-27)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(x * Float32(Float32(0.5) * Float32(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(-9.999999887266023e-27)) tmp = single(1.0) / (single(2.0) + ((x * (single(0.5) * (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 -9.999999887266023 \cdot 10^{-27}:\\
\;\;\;\;\frac{1}{2 + \left(x \cdot \left(0.5 \cdot \frac{x}{s \cdot s}\right) - \frac{x}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -9.99999989e-27Initial program 100.0%
Taylor expanded in x around 0 80.9%
mul-1-neg80.9%
unsub-neg80.9%
associate-*r/80.0%
unpow280.0%
unpow280.0%
Simplified80.0%
associate-*r*80.0%
*-un-lft-identity80.0%
times-frac82.5%
Applied egg-rr82.5%
sub-neg82.5%
/-rgt-identity82.5%
*-commutative82.5%
Applied egg-rr82.5%
unsub-neg82.5%
associate-*l*82.5%
Applied egg-rr82.5%
if -9.99999989e-27 < x Initial program 99.9%
Taylor expanded in x around 0 48.2%
Final simplification64.0%
(FPCore (x s) :precision binary32 (if (<= x -1.9999999996399175e-23) (/ 1.0 (+ 2.0 (- (* (/ 0.5 (* s s)) (* x x)) (/ x s)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.9999999996399175e-23f) {
tmp = 1.0f / (2.0f + (((0.5f / (s * s)) * (x * 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 <= (-1.9999999996399175e-23)) then
tmp = 1.0e0 / (2.0e0 + (((0.5e0 / (s * s)) * (x * x)) - (x / s)))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-1.9999999996399175e-23)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(Float32(0.5) / Float32(s * s)) * Float32(x * 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(-1.9999999996399175e-23)) tmp = single(1.0) / (single(2.0) + (((single(0.5) / (s * s)) * (x * x)) - (x / s))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.9999999996399175 \cdot 10^{-23}:\\
\;\;\;\;\frac{1}{2 + \left(\frac{0.5}{s \cdot s} \cdot \left(x \cdot x\right) - \frac{x}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -2e-23Initial program 100.0%
Taylor expanded in x around 0 82.0%
mul-1-neg82.0%
unsub-neg82.0%
associate-*r/81.1%
unpow281.1%
unpow281.1%
Simplified81.1%
associate-/l*82.0%
associate-/r/85.2%
Applied egg-rr85.2%
if -2e-23 < x Initial program 99.9%
Taylor expanded in x around 0 48.4%
Final simplification64.7%
(FPCore (x s) :precision binary32 (if (<= x 4.9999998413276127e-20) (/ 1.0 (+ 2.0 (* (/ x s) (+ (/ x (/ s 0.5)) -1.0)))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= 4.9999998413276127e-20f) {
tmp = 1.0f / (2.0f + ((x / s) * ((x / (s / 0.5f)) + -1.0f)));
} 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.9999998413276127e-20) then
tmp = 1.0e0 / (2.0e0 + ((x / s) * ((x / (s / 0.5e0)) + (-1.0e0))))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.9999998413276127e-20)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(x / s) * Float32(Float32(x / Float32(s / Float32(0.5))) + Float32(-1.0))))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(4.9999998413276127e-20)) tmp = single(1.0) / (single(2.0) + ((x / s) * ((x / (s / single(0.5))) + single(-1.0)))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.9999998413276127 \cdot 10^{-20}:\\
\;\;\;\;\frac{1}{2 + \frac{x}{s} \cdot \left(\frac{x}{\frac{s}{0.5}} + -1\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < 4.99999984e-20Initial program 99.9%
Taylor expanded in x around 0 77.5%
mul-1-neg77.5%
unsub-neg77.5%
associate-*r/76.9%
unpow276.9%
unpow276.9%
Simplified76.9%
associate-/l*77.5%
frac-sub61.4%
times-frac69.8%
times-frac73.9%
Applied egg-rr73.9%
associate-*l*82.5%
*-commutative82.5%
Simplified82.5%
Taylor expanded in s around 0 82.5%
Taylor expanded in s around 0 77.5%
unpow277.5%
unpow277.5%
associate-*r/76.9%
associate-*l*76.9%
times-frac77.2%
distribute-rgt-out77.2%
*-commutative77.2%
associate-/l*77.2%
Simplified77.2%
if 4.99999984e-20 < x Initial program 100.0%
Taylor expanded in x around 0 32.6%
Final simplification61.0%
(FPCore (x s) :precision binary32 (if (<= x 4.9999998413276127e-20) (/ 1.0 (+ 2.0 (/ (- (* x 0.5) s) (* s (/ s x))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= 4.9999998413276127e-20f) {
tmp = 1.0f / (2.0f + (((x * 0.5f) - s) / (s * (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 <= 4.9999998413276127e-20) then
tmp = 1.0e0 / (2.0e0 + (((x * 0.5e0) - s) / (s * (s / x))))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.9999998413276127e-20)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(x * Float32(0.5)) - s) / Float32(s * 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(4.9999998413276127e-20)) tmp = single(1.0) / (single(2.0) + (((x * single(0.5)) - s) / (s * (s / x)))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.9999998413276127 \cdot 10^{-20}:\\
\;\;\;\;\frac{1}{2 + \frac{x \cdot 0.5 - s}{s \cdot \frac{s}{x}}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < 4.99999984e-20Initial program 99.9%
Taylor expanded in x around 0 77.5%
mul-1-neg77.5%
unsub-neg77.5%
associate-*r/76.9%
unpow276.9%
unpow276.9%
Simplified76.9%
associate-/r*76.6%
clear-num76.6%
frac-sub61.2%
*-un-lft-identity61.2%
times-frac61.2%
metadata-eval61.2%
associate-/l*61.7%
*-commutative61.7%
*-un-lft-identity61.7%
Applied egg-rr61.7%
associate-*l*61.7%
associate-/r/61.7%
Simplified61.7%
Taylor expanded in x around 0 77.6%
if 4.99999984e-20 < x Initial program 100.0%
Taylor expanded in x around 0 32.6%
Final simplification61.3%
(FPCore (x s) :precision binary32 (if (<= x -1.0000000180025095e-35) (/ 1.0 (+ 2.0 (* (/ 0.5 s) (/ x (/ s x))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.0000000180025095e-35f) {
tmp = 1.0f / (2.0f + ((0.5f / 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 <= (-1.0000000180025095e-35)) then
tmp = 1.0e0 / (2.0e0 + ((0.5e0 / 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(-1.0000000180025095e-35)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(0.5) / 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(-1.0000000180025095e-35)) tmp = single(1.0) / (single(2.0) + ((single(0.5) / s) * (x / (s / x)))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.0000000180025095 \cdot 10^{-35}:\\
\;\;\;\;\frac{1}{2 + \frac{0.5}{s} \cdot \frac{x}{\frac{s}{x}}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.00000002e-35Initial program 99.9%
Taylor expanded in x around 0 80.3%
mul-1-neg80.3%
unsub-neg80.3%
associate-*r/79.6%
unpow279.6%
unpow279.6%
Simplified79.6%
associate-/l*80.3%
frac-sub73.2%
times-frac76.9%
times-frac78.7%
Applied egg-rr78.7%
associate-*l*83.3%
*-commutative83.3%
Simplified83.3%
Taylor expanded in s around 0 79.1%
unpow279.1%
associate-*r/78.3%
times-frac74.6%
unpow274.6%
associate-/l*74.7%
Simplified74.7%
if -1.00000002e-35 < x Initial program 100.0%
Taylor expanded in x around 0 44.7%
Final simplification59.9%
(FPCore (x s) :precision binary32 (if (<= x -5.999999941330714e-10) (* 2.0 (* (/ s x) (/ s x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -5.999999941330714e-10f) {
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 <= (-5.999999941330714e-10)) 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(-5.999999941330714e-10)) 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(-5.999999941330714e-10)) 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 -5.999999941330714 \cdot 10^{-10}:\\
\;\;\;\;2 \cdot \left(\frac{s}{x} \cdot \frac{s}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -5.99999994e-10Initial program 100.0%
Taylor expanded in x around 0 83.4%
mul-1-neg83.4%
unsub-neg83.4%
associate-*r/83.4%
unpow283.4%
unpow283.4%
Simplified83.4%
Taylor expanded in x around inf 81.2%
unpow281.2%
unpow281.2%
Simplified81.2%
frac-times81.2%
Applied egg-rr81.2%
if -5.99999994e-10 < x Initial program 99.9%
Taylor expanded in x around 0 47.4%
Final simplification58.7%
(FPCore (x s) :precision binary32 (if (<= x -3.300000037447892e-18) (* 2.0 (/ (* s s) (* x x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -3.300000037447892e-18f) {
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 <= (-3.300000037447892e-18)) 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(-3.300000037447892e-18)) tmp = Float32(Float32(2.0) * Float32(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(-3.300000037447892e-18)) 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 -3.300000037447892 \cdot 10^{-18}:\\
\;\;\;\;2 \cdot \frac{s \cdot s}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -3.30000004e-18Initial program 100.0%
Taylor expanded in x around 0 82.2%
mul-1-neg82.2%
unsub-neg82.2%
associate-*r/82.2%
unpow282.2%
unpow282.2%
Simplified82.2%
Taylor expanded in x around inf 73.8%
unpow273.8%
unpow273.8%
Simplified73.8%
if -3.30000004e-18 < x Initial program 99.9%
Taylor expanded in x around 0 48.6%
Final simplification58.9%
(FPCore (x s) :precision binary32 (if (<= x -3.300000037447892e-18) (/ (* 2.0 (* s s)) (* x x)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -3.300000037447892e-18f) {
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 <= (-3.300000037447892e-18)) 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(-3.300000037447892e-18)) 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(-3.300000037447892e-18)) 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 -3.300000037447892 \cdot 10^{-18}:\\
\;\;\;\;\frac{2 \cdot \left(s \cdot s\right)}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -3.30000004e-18Initial program 100.0%
Taylor expanded in x around 0 82.2%
mul-1-neg82.2%
unsub-neg82.2%
associate-*r/82.2%
unpow282.2%
unpow282.2%
Simplified82.2%
Taylor expanded in x around inf 73.8%
unpow273.8%
unpow273.8%
Simplified73.8%
associate-*r/73.8%
Applied egg-rr73.8%
if -3.30000004e-18 < x Initial program 99.9%
Taylor expanded in x around 0 48.6%
Final simplification58.9%
(FPCore (x s) :precision binary32 (if (<= x -1.0000000180025095e-35) (/ 1.0 (- 2.0 (/ x s))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.0000000180025095e-35f) {
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 <= (-1.0000000180025095e-35)) 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(-1.0000000180025095e-35)) 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(-1.0000000180025095e-35)) 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 -1.0000000180025095 \cdot 10^{-35}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.00000002e-35Initial program 99.9%
Taylor expanded in x around 0 56.1%
mul-1-neg56.1%
unsub-neg56.1%
Simplified56.1%
if -1.00000002e-35 < x Initial program 100.0%
Taylor expanded in x around 0 44.7%
Final simplification50.5%
(FPCore (x s) :precision binary32 (if (<= x -7.999999773744548e-9) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -7.999999773744548e-9f) {
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.999999773744548e-9)) 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.999999773744548e-9)) 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.999999773744548e-9)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.999999773744548 \cdot 10^{-9}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -7.99999977e-9Initial program 100.0%
Taylor expanded in x around 0 55.0%
mul-1-neg55.0%
unsub-neg55.0%
Simplified55.0%
Taylor expanded in x around inf 51.5%
associate-*r/51.5%
neg-mul-151.5%
Simplified51.5%
if -7.99999977e-9 < x Initial program 99.9%
Taylor expanded in x around 0 47.0%
Final simplification48.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 34.2%
Final simplification34.2%
herbie shell --seed 2023221
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))