Project

General

Profile

Bug #4767 » 0001-enhanced-mathn-docs.patch

sz (Sandor Szücs), 05/23/2011 03:34 AM

View differences:

lib/mathn.rb
##
# = mathn
#
# mathn is a library for changing the way Ruby does math.
# mathn is a library for changing the way Ruby does math. If you need
# more precise rounding with multiple division or exponentiation
# operations, then mathn is the right tool. It makes sense to use this
# library if you can use it's late rounding. Mathn does not convert
# Fixnums into Floats as long as you do not convert it yourself.
# Instead of using Float as intermediate value it use Rational as
# value representation.
#
# Example Fixnum with intermediate Float:
#
# 20 / 9 * 3 * 14 / 7 * 3 / 2 #=> 18
#
# Example: using mathn Fixnum/Rational:
#
# require 'mathn'
# 20 / 9 * 3 * 14 / 7 * 3 / 2 #=> 20
#
# == Usage
#
......
#
# 3 / 2
#
# will return (3/2) instead of the usual 1.
# will return +Rational+ (3/2) instead of the usual +Fixnum+ 1.
#
# == Copyright
#
......
Math = CMath
end
##
# Fixnum's division and exponentiation are enhanced to return more
# precise values in mathematical formulas.
#
# 2/3*3 #=> 0
# require 'mathn'
# 2/3*3 #=> 2
#
class Fixnum
remove_method :/
##
# +/+ defines the Rational division for Fixnum.
#
# 1/3 #=> (1/3)
#
alias / quo
alias power! ** unless method_defined? :power!
......
end
# Bignum's division and exponentiation are enhanced to return more
# precise values in mathematical formulas.
class Bignum
remove_method :/
##
# +/+ defines the Rational division for Bignum.
#
# (2**72) / ((2**70) * 3) #=> 4/3
#
alias / quo
alias power! ** unless method_defined? :power!
......
end
##
# Rational changes that simplfies the usage of Rational opaerations.
#
# normal behaviour:
#
# Rational.new!(1,3) ** 2 #=> Rational(1, 9)
# (1 / 3) ** 2 #=> 0
#
# mathn behaviour:
#
# (1 / 3) ** 2 #=> 1/9
#
class Rational
remove_method :**
##
# exponentiate by +other+
#
# (1/3) ** 2 #=> 1/9
#
def ** (other)
if other.kind_of?(Rational)
other2 = other
......
end
end
##
# Changes of the ruby Math module.
#
# 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
#
# using mathn library 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)
#
module Math
remove_method(:sqrt)
##
# compute the square root of +a+
# Compute 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
def sqrt(a)
if a.kind_of?(Complex)
abs = sqrt(a.real*a.real + a.imag*a.imag)
......
end
end
def rsqrt(a) # :nodoc:
# Compute square root of a non negative number. This method is
# internally used by +Math.sqrt+.
def rsqrt(a)
if a.kind_of?(Float)
sqrt!(a)
elsif a.kind_of?(Rational)
......
module_function :rsqrt
end
##
# Float is changed to know Complex numbers.
class Float
alias power! **
    (1-1/1)