Re: What error code to return for Math.sqrt() overflow condition?

Administrator

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.