Integer count in a closed interval (source code)

= Integer count in a closed interval
{title2=$|[a,b]\cap\mathbb Z|=\max(0,\lfloor b\rfloor-\lceil a\rceil+1)$}

For <real numbers> $a\leq b$, the <integers> in the <closed interval> $[a,b]$ begin at $\lceil a\rceil$ and end at $\lfloor b\rfloor$, if there are any. Their number is therefore
$$
|[a,b]\cap\mathbb Z|=\max\!\left(0,\lfloor b\rfloor-\lceil a\rceil+1\right).
$$
The extra one includes both ends of the resulting <integer interval>. The <maximum> with zero handles an empty <intersection>: $[0.2,0.8]$ contains no <integer>, even though it has positive length.

For $[-2.7,1.1]$, the <ceiling function> selects $-2$ and the <floor function> selects $1$, giving $1-(-2)+1=4$. The four points are $-2,-1,0,1$. For $[-2,2]$, the count is five, not four: the real length $2-(-2)$ and the number of <integer> points are different quantities.

The endpoint convention matters. For the half-open <interval> $[a,b)$ with $a\leq b$, the last allowed <integer> is $\lceil b\rceil-1$, so the count becomes $\max(0,\lceil b\rceil-\lceil a\rceil)$. This handles an <integer> endpoint $b$ without including it accidentally. For example, $[-2,2)$ has four <integer> points.