method
sqrt
sqrt(a)
private
Hide source
# File lib/mathn.rb, line 121 def sqrt(a) if a.kind_of?(Complex) abs = sqrt(a.real*a.real + a.imag*a.imag) # if not abs.kind_of?(Rational) # return a**Rational(1,2) # end x = sqrt((a.real + abs)/Rational(2)) y = sqrt((-a.real + abs)/Rational(2)) # if !(x.kind_of?(Rational) and y.kind_of?(Rational)) # return a**Rational(1,2) # end if a.imag >= 0 Complex(x, y) else Complex(x, -y) end elsif a.respond_to?(:nan?) and a.nan? a elsif a >= 0 rsqrt(a) else Complex(0,rsqrt(-a)) end end