Java For-loop changes numeric result when changing type of loop variable

Actually, your first loop would have int overflow in the calculation of (2 * i + 1) when i is large enough, so I wouldn’t rely on the output of it.

The second loop, on the other hand, produces a more correct output, since (2 * j + 1) doesn’t overflow, since it performs long multiplication.

This makes the “int – loop” calculates PI more accurate than “long – loop”

That’s probably just a coincidence, since the calculations in the int loop overflow.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)