Discussion
Why does adding one to 16,777,217 seem to make the number smaller?
You probably spotted the letters L and f next to the numbers in the code. They don’t change the numerical values we’re dealing with, but they do change the way those numbers are stored in memory—L is for Long, and f is for Float.
To add a Float to a Long, Kotlin converts both values to the Float type. With only a limited number of significant figures, a Float can’t store the exact value for a number of this magnitude, and the operation ends up being performed with imprecise rounded values.
Significant Bits
The choice to use a Long instead of an Int here is just to demonstrate the L suffix and doesn’t change the outcome of the puzzle. 16,777,217 is well within the range ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access