The Math.sqrt
function can't compute the square root of negative numbers:
irb> Math.sqrt(-1)
Math::DomainError: Numerical argument is out of domain - "sqrt"
...
You have to use the CMath
module that return complex numbers as needed:
irb> require 'cmath'
irb> CMath.sqrt(-1)
# => (0+1.0i)
irb> CMath.sqrt(-1).class
# => Complex
irb> CMath.sqrt(1).class
# => Float