What is the maximum integer that can be saved in a LaTeX counter?
The maximum value is the 'usual' 2^31-1
long integer value, as well as the negative range from -2^31
, so the full range is 2^32
integers possible.
2^31-1
is 2147483647, which is the largest possible integer usable for counters or \ifnum
and \numexpr
codes.
In the code below I stored this number to the counter \mycounter
and print it several times after using \stepcounter
. After the first \stepcounter
the register overflows and the number is set to -2147483648
, being the 'largest' negative number possible. A subsequent \stepcounter
works normally then.
The e-TeX
standard extended the limit of 256 registers (count, skip etc.) to 32568 possible registers (for each type)
\documentclass{article}
\newcounter{mycounter}
\begin{document}
\setcounter{mycounter}{2147483647}
\themycounter % prints 2147483647
\stepcounter{mycounter} % Now the overflow will occur
\themycounter % prints -2147483648
\stepcounter{mycounter}
\themycounter % -2147483647
\end{document}
According to The TEXbook:
TEX has 256 registers called
\count0
to\count255
, each capable of containing integers between -2147483647 and +2147483647, inclusive; i.e., the magnitudes should be less than 231.