Square root can not overflow. The square root of 32767, the largest integer, is 181.
In the presented algorithm, (
y+2^
j)^2 can be greater than 32767, which results in integer overflow. For a safe implementation you do need to check if (
y+2^
j)^2 overflowed into a negative number. If the overflow occurred, treat it as if it was >
x.
t = (y+2^j)^2
if (t <= x and t >= 0) // t < 0 indicates overflow
y = y+2^j
--Mark