It does not work forever for the simple reason that floating point numbers are not perfect.
At some point, k will become large enough, so adding 1 to it will have no effect.
At this point, k will be equal to k+1 , and your loop will exit.
Floating point numbers can be differentiated by one block only if they are in a certain range.
As an example, suppose you have an integer type with 3 decimal digits of precision for a positive integer and a metric with one decimal digit.
In this case, you can perfectly represent numbers from 0 to 999 as 000x10 0 through 999x10 0 (since 10 0 is 1):
What happens if you want to introduce 1000? You need to use 100x10 1 . It is still beautifully presented.
However, there is no exact way to represent 1001 with this diagram, the next number you can imagine is 101x10 1 which equals 1010.
So, when you add 1 to 1000, you will get the closest match, which is 1000.
source share