fix potential overflow in sqrt