Floating point numbers are imprecise, and square root algorithms only deliver approximations. It is well possible that, say, sqrt(1000000.0) doesn't return exactly 1000.0, but, say, 1000.0001 or perhaps 999.9999. The first is not a problem, because converting it to an integer will still yield 1000. However, integer conversion always rounds toward zero, no matter how small the difference is; thus, 999.9999 will yield 999 when converted to an integer. 999*999 is not 1000000, so the test will incorrectly fail. Adding 0.5 to the result ensures that the result will be correct either way. (The error will not go beyond 0.5 until the numbers become very large - probably too large for an integer anyway.)