In Files

  • mathn.rb

Methods

Class/Module Index [+]

Quicksearch

Math

When mathn is required, the Math module changes as follows:

Standard Math module behaviour:

Math.sqrt(4/9)     # => 0.0
Math.sqrt(4.0/9.0) # => 0.666666666666667
Math.sqrt(- 4/9)   # => Errno::EDOM: Numerical argument out of domain - sqrt

After require ‘mathn’, this is changed to:

require 'mathn'
Math.sqrt(4/9)      # => 2/3
Math.sqrt(4.0/9.0)  # => 0.666666666666667
Math.sqrt(- 4/9)    # => Complex(0, 2/3)

Public Class Methods

sqrt(a) click to toggle source

Computes the square root of a. It makes use of Complex and Rational to have no rounding errors if possible.

Math.sqrt(4/9)      # => 2/3
Math.sqrt(- 4/9)    # => Complex(0, 2/3)
Math.sqrt(4.0/9.0)  # => 0.666666666666667
 
               # File mathn.rb, line 104
def sqrt(a)
  if a.kind_of?(Complex)
    sqrt!(a)
  elsif a.respond_to?(:nan?) and a.nan?
    a
  elsif a >= 0
    rsqrt(a)
  else
    Complex(0,rsqrt(-a))
  end
end