I wrote this little program to calculate pi.
While playing with the code and trying to find the most exact result, I found a point where my computer couldn’t calalculate a result. It could do 33554430 repetitions within seconds, but if i increased the for loop to 33554431 it didn’t output anything.
So is 33554430 a special number?
public class CalculatePi{ public static void main(String[] args){ float pi=0; int sign=1; for(float i=1; i <= 33554430; i+=2){ pi += (sign*(1.0/i)); sign*= -1; } pi *= 4; System.out.println(pi); } }
Answer
You are getting and endless loop, because during the comparison i <= 33554431
, the int
value 33554431
is promoted to a float
value which is “too precise” for float and will actually equal to 33554432
.
Then, when you try to increase the value by +2
, the float
just isn’t precise enough to increment from the value 33554432
. To illustrate my point:
float f = 33554432; System.out.println(f); //33554432 f += 2; System.out.println(f); //33554432
So the value f
doesn’t increase due to its precision limitation. If you’d increase it by, say 11
, you’d get 33554444
(and not 33554443
) as that is the closest number expressible with that precision.
So is 33554430 a special number?
Sort of, not 33554430 but rather 33554432. First “special number” for float is 16777217
, which is the first positive integer that cannot be represented as a float
(equals 16777216
as float). So, if you’d increment your i
variable by 1
, this is the number you’d get stuck on. Now, since you are incrementing by 2
, the number you get stuck on is 16777216 * 2 = 33554432
.