Formula for returning the size of a circular array
I've not checked if your formula is correct but the point appears to be that $x\%y$ can only give answers in the range $\{0, \dots, y-1\}$, whereas you're performing a calculation where the answer could be anything in the range $\{0, \dots, y\}$, including&nbsp;$y$. This means that any formula of the form blah%capacity is necessarily wrong.