Doing sqrt like that will not work for such big values, because sqrt returns a Float and its precision will not suffice here. I would advice you to implement your own sqrt function. There are several algorithms out there suggesting how to do that, but I personally thing using a binary search for computing the reverse for a function is the easiest:
def sqrt a
begv = 1
endv = a
while endv > begv + 1
mid = (endv + begv)/2
if mid ** 2 <= a
begv = mid
else
endv = mid
end
end
return begv
end
Alternatively you may try using BigDecimal for the sqrt(simply raise to power 0.5
), but I like above method better as it does not involve any double calculations.