From 6125552c27b40a8da9e162af2655feca82ac16d3 Mon Sep 17 00:00:00 2001 From: tadf Date: Sun, 16 Mar 2008 00:23:43 +0000 Subject: both complex and rational are now builtin classes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15783 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/complex.rb | 620 ++++++++++---------------------------------------------- lib/mathn.rb | 9 +- lib/rational.rb | 528 +---------------------------------------------- 3 files changed, 117 insertions(+), 1040 deletions(-) (limited to 'lib') diff --git a/lib/complex.rb b/lib/complex.rb index 9a621c033f..505b0120e3 100644 --- a/lib/complex.rb +++ b/lib/complex.rb @@ -1,473 +1,90 @@ -# -# complex.rb - -# $Release Version: 0.5 $ -# $Revision: 1.3 $ -# by Keiju ISHITSUKA(SHL Japan Inc.) -# -# ---- -# -# complex.rb implements the Complex class for complex numbers. Additionally, -# some methods in other Numeric classes are redefined or added to allow greater -# interoperability with Complex numbers. -# -# Complex numbers can be created in the following manner: -# - Complex(a, b) -# - Complex.polar(radius, theta) -# -# Additionally, note the following: -# - Complex::I (the mathematical constant i) -# - Numeric#im (e.g. 5.im -> 0+5i) -# -# The following +Math+ module methods are redefined to handle Complex arguments. -# They will work as normal with non-Complex arguments. -# sqrt exp cos sin tan log log10 -# cosh sinh tanh acos asin atan atan2 acosh asinh atanh -# - - -# -# Numeric is a built-in class on which Fixnum, Bignum, etc., are based. Here -# some methods are added so that all number types can be treated to some extent -# as Complex numbers. -# -class Numeric - # - # Returns a Complex number (0,self). - # - def im - Complex(0, self) - end - - # - # The real part of a complex number, i.e. self. - # - def real - self - end - - # - # The imaginary part of a complex number, i.e. 0. - # - def image - 0 - end - alias imag image - - # - # See Complex#arg. - # - def arg - if self >= 0 - return 0 - else - return Math::PI - end - end - alias angle arg - - # - # See Complex#polar. - # - def polar - return abs, arg - end - - # - # See Complex#conjugate (short answer: returns self). - # - def conjugate - self - end - alias conj conjugate -end - - -# -# Creates a Complex number. +a+ and +b+ should be Numeric. The result will be -# a+bi. -# -def Complex(a, b = 0) - if b == 0 and (a.kind_of?(Complex) or defined? Complex::Unify) - a - elsif a.scalar? and b.scalar? - # Don't delete for -0.0 - Complex.new(a, b) - else - Complex.new( a.real-b.imag, a.imag+b.real ) - end -end - -# -# The complex number class. See complex.rb for an overview. -# -class Complex < Numeric - @RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-' - - undef step - undef <, <=, <=>, >, >= - undef between? - undef div, divmod, modulo - undef floor, truncate, ceil, round - - def scalar? - false - end - - def Complex.generic?(other) # :nodoc: - other.kind_of?(Integer) or - other.kind_of?(Float) or - (defined?(Rational) and other.kind_of?(Rational)) - end - - # - # Creates a +Complex+ number in terms of +r+ (radius) and +theta+ (angle). - # - def Complex.polar(r, theta) - Complex(r*Math.cos(theta), r*Math.sin(theta)) - end - - # - # Creates a +Complex+ number a+bi. - # - def Complex.new!(a, b=0) - new(a,b) - end - - def initialize(a, b) - raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric - raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex - raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric - raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex - @real = a - @image = b - end - - # - # Addition with real or complex number. - # - def + (other) - if other.kind_of?(Complex) - re = @real + other.real - im = @image + other.image - Complex(re, im) - elsif Complex.generic?(other) - Complex(@real + other, @image) - else - x , y = other.coerce(self) - x + y - end - end - - # - # Subtraction with real or complex number. - # - def - (other) - if other.kind_of?(Complex) - re = @real - other.real - im = @image - other.image - Complex(re, im) - elsif Complex.generic?(other) - Complex(@real - other, @image) - else - x , y = other.coerce(self) - x - y - end - end - - # - # Multiplication with real or complex number. - # - def * (other) - if other.kind_of?(Complex) - re = @real*other.real - @image*other.image - im = @real*other.image + @image*other.real - Complex(re, im) - elsif Complex.generic?(other) - Complex(@real * other, @image * other) - else - x , y = other.coerce(self) - x * y - end - end - - # - # Division by real or complex number. - # - def / (other) - if other.kind_of?(Complex) - self*other.conjugate/other.abs2 - elsif Complex.generic?(other) - Complex(@real/other, @image/other) - else - x, y = other.coerce(self) - x/y - end - end +class Integer - def quo(other) - Complex(@real.quo(1), @image.quo(1)) / other - end - - # - # Raise this complex number to the given (real or complex) power. - # - def ** (other) - if other == 0 - return Complex(1) - end - if other.kind_of?(Complex) - r, theta = polar - ore = other.real - oim = other.image - nr = Math.exp!(ore*Math.log!(r) - oim * theta) - ntheta = theta*ore + oim*Math.log!(r) - Complex.polar(nr, ntheta) - elsif other.kind_of?(Integer) - if other > 0 - x = self - z = x - n = other - 1 - while n != 0 - while (div, mod = n.divmod(2) - mod == 0) - x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image) - n = div - end - z *= x - n -= 1 - end - z - else - if defined? Rational - (Rational(1) / self) ** -other - else - self ** Float(other) - end - end - elsif Complex.generic?(other) - r, theta = polar - Complex.polar(r**other, theta*other) - else - x, y = other.coerce(self) - x**y - end - end - - # - # Remainder after division by a real or complex number. - # - -=begin - def % (other) - if other.kind_of?(Complex) - Complex(@real % other.real, @image % other.image) - elsif Complex.generic?(other) - Complex(@real % other, @image % other) - else - x , y = other.coerce(self) - x % y - end - end -=end - -#-- -# def divmod(other) -# if other.kind_of?(Complex) -# rdiv, rmod = @real.divmod(other.real) -# idiv, imod = @image.divmod(other.image) -# return Complex(rdiv, idiv), Complex(rmod, rmod) -# elsif Complex.generic?(other) -# Complex(@real.divmod(other), @image.divmod(other)) -# else -# x , y = other.coerce(self) -# x.divmod(y) -# end -# end -#++ - - # - # Absolute value (aka modulus): distance from the zero point on the complex - # plane. - # - def abs - Math.hypot(@real, @image) - end - - # - # Square of the absolute value. - # - def abs2 - @real*@real + @image*@image - end - - # - # Argument (angle from (1,0) on the complex plane). - # - def arg - Math.atan2!(@image, @real) - end - alias angle arg - - # - # Returns the absolute value _and_ the argument. - # - def polar - return abs, arg - end - - # - # Complex conjugate (z + z.conjugate = 2 * z.real). - # - def conjugate - Complex(@real, -@image) - end - alias conj conjugate - - # - # Test for numerical equality (a == a + 0i). - # - def == (other) - if other.kind_of?(Complex) - @real == other.real and @image == other.image - elsif Complex.generic?(other) - @real == other and @image == 0 - else - other == self + def gcd(other) + min = self.abs + max = other.abs + while min > 0 + tmp = min + min = max % min + max = tmp end + max end - # - # Attempts to coerce +other+ to a Complex number. - # - def coerce(other) - if Complex.generic?(other) - return Complex.new!(other), self + def lcm(other) + if self.zero? or other.zero? + 0 else - super + (self.div(self.gcd(other)) * other).abs end end - # - # FIXME - # - def denominator - @real.denominator.lcm(@image.denominator) - end - - # - # FIXME - # - def numerator - cd = denominator - Complex(@real.numerator*(cd/@real.denominator), - @image.numerator*(cd/@image.denominator)) - end - - # - # Standard string representation of the complex number. - # - def to_s - if @real != 0 - if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1 - if @image >= 0 - @real.to_s+"+("+@image.to_s+")i" - else - @real.to_s+"-("+(-@image).to_s+")i" - end - else - if @image >= 0 - @real.to_s+"+"+@image.to_s+"i" - else - @real.to_s+"-"+(-@image).to_s+"i" - end - end + def gcdlcm(other) + gcd = self.gcd(other) + if self.zero? or other.zero? + [gcd, 0] else - if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1 - "("+@image.to_s+")i" - else - @image.to_s+"i" - end + [gcd, (self.div(gcd) * other).abs] end end - - # - # Returns a hash code for the complex number. - # - def hash - @real.hash ^ @image.hash - end - - # - # Returns "Complex(real, image)". - # - def inspect - sprintf("Complex(%s, %s)", @real.inspect, @image.inspect) - end - - - # - # +I+ is the imaginary number. It exists at point (0,1) on the complex plane. - # - I = Complex(0,1) - - # The real part of a complex number. - attr_reader :real - - # The imaginary part of a complex number. - attr_reader :image - alias imag image - -end - -class Integer - - unless defined?(1.numerator) - def numerator() self end - def denominator() 1 end - - def gcd(other) - min = self.abs - max = other.abs - while min > 0 - tmp = min - min = max % min - max = tmp - end - max - end - - def lcm(other) - if self.zero? or other.zero? - 0 - else - (self.div(self.gcd(other)) * other).abs - end - end - - end end module Math - alias sqrt! sqrt + alias exp! exp alias log! log alias log10! log10 - alias cos! cos + alias sqrt! sqrt + alias sin! sin + alias cos! cos alias tan! tan - alias cosh! cosh + alias sinh! sinh + alias cosh! cosh alias tanh! tanh - alias acos! acos + alias asin! asin + alias acos! acos alias atan! atan alias atan2! atan2 - alias acosh! acosh + alias asinh! asinh - alias atanh! atanh + alias acosh! acosh + alias atanh! atanh + + def exp(z) + if Complex.generic?(z) + exp!(z) + else + Complex(exp!(z.real) * cos!(z.image), + exp!(z.real) * sin!(z.image)) + end + end + + def log(*args) + z, b = args + if Complex.generic?(z) and z >= 0 and (b.nil? or b >= 0) + log!(*args) + else + r, theta = z.polar + a = Complex(log!(r.abs), theta) + if b + a /= log(b) + end + a + end + end + + def log10(z) + if Complex.generic?(z) + log10!(z) + else + log(z) / log!(10) + end + end - # Redefined to handle a Complex argument. def sqrt(z) if Complex.generic?(z) if z >= 0 @@ -481,41 +98,29 @@ module Math else r = z.abs x = z.real - Complex( sqrt!((r+x)/2), sqrt!((r-x)/2) ) + Complex(sqrt!((r + x) / 2), sqrt!((r - x) / 2)) end end end - - # Redefined to handle a Complex argument. - def exp(z) + + def sin(z) if Complex.generic?(z) - exp!(z) + sin!(z) else - Complex(exp!(z.real) * cos!(z.image), exp!(z.real) * sin!(z.image)) + Complex(sin!(z.real) * cosh!(z.image), + cos!(z.real) * sinh!(z.image)) end end - - # Redefined to handle a Complex argument. + def cos(z) if Complex.generic?(z) cos!(z) else - Complex(cos!(z.real)*cosh!(z.image), - -sin!(z.real)*sinh!(z.image)) + Complex(cos!(z.real) * cosh!(z.image), + -sin!(z.real) * sinh!(z.image)) end end - - # Redefined to handle a Complex argument. - def sin(z) - if Complex.generic?(z) - sin!(z) - else - Complex(sin!(z.real)*cosh!(z.image), - cos!(z.real)*sinh!(z.image)) - end - end - - # Redefined to handle a Complex argument. + def tan(z) if Complex.generic?(z) tan!(z) @@ -528,7 +133,8 @@ module Math if Complex.generic?(z) sinh!(z) else - Complex( sinh!(z.real)*cos!(z.image), cosh!(z.real)*sin!(z.image) ) + Complex(sinh!(z.real) * cos!(z.image), + cosh!(z.real) * sin!(z.image)) end end @@ -536,7 +142,8 @@ module Math if Complex.generic?(z) cosh!(z) else - Complex( cosh!(z.real)*cos!(z.image), sinh!(z.real)*sin!(z.image) ) + Complex(cosh!(z.real) * cos!(z.image), + sinh!(z.real) * sin!(z.image)) end end @@ -544,42 +151,23 @@ module Math if Complex.generic?(z) tanh!(z) else - sinh(z)/cosh(z) - end - end - - # Redefined to handle a Complex argument. - def log(z) - if Complex.generic?(z) and z >= 0 - log!(z) - else - r, theta = z.polar - Complex(log!(r.abs), theta) - end - end - - # Redefined to handle a Complex argument. - def log10(z) - if Complex.generic?(z) - log10!(z) - else - log(z)/log!(10) + sinh(z) / cosh(z) end end - def acos(z) + def asin(z) if Complex.generic?(z) and z >= -1 and z <= 1 - acos!(z) + asin!(z) else - -1.0.im * log( z + 1.0.im * sqrt(1.0-z*z) ) + -1.0.im * log(1.0.im * z + sqrt(1.0 - z * z)) end end - def asin(z) + def acos(z) if Complex.generic?(z) and z >= -1 and z <= 1 - asin!(z) + acos!(z) else - -1.0.im * log( 1.0.im * z + sqrt(1.0-z*z) ) + -1.0.im * log(z + 1.0.im * sqrt(1.0 - z * z)) end end @@ -587,7 +175,7 @@ module Math if Complex.generic?(z) atan!(z) else - 1.0.im * log( (1.0.im+z) / (1.0.im-z) ) / 2.0 + 1.0.im * log((1.0.im + z) / (1.0.im - z)) / 2.0 end end @@ -595,7 +183,7 @@ module Math if Complex.generic?(y) and Complex.generic?(x) atan2!(y,x) else - -1.0.im * log( (x+1.0.im*y) / sqrt(x*x+y*y) ) + -1.0.im * log((x + 1.0.im * y) / sqrt(x * x + y * y)) end end @@ -603,7 +191,7 @@ module Math if Complex.generic?(z) and z >= 1 acosh!(z) else - log( z + sqrt(z*z-1.0) ) + log(z + sqrt(z * z - 1.0)) end end @@ -611,7 +199,7 @@ module Math if Complex.generic?(z) asinh!(z) else - log( z + sqrt(1.0+z*z) ) + log(z + sqrt(1.0 + z * z)) end end @@ -619,49 +207,47 @@ module Math if Complex.generic?(z) and z >= -1 and z <= 1 atanh!(z) else - log( (1.0+z) / (1.0-z) ) / 2.0 + log((1.0 + z) / (1.0 - z)) / 2.0 end end - module_function :sqrt! - module_function :sqrt module_function :exp! module_function :exp module_function :log! module_function :log module_function :log10! module_function :log10 - module_function :cosh! - module_function :cosh - module_function :cos! - module_function :cos - module_function :sinh! - module_function :sinh + module_function :sqrt! + module_function :sqrt + module_function :sin! module_function :sin + module_function :cos! + module_function :cos module_function :tan! module_function :tan + + module_function :sinh! + module_function :sinh + module_function :cosh! + module_function :cosh module_function :tanh! module_function :tanh - module_function :acos! - module_function :acos + module_function :asin! module_function :asin + module_function :acos! + module_function :acos module_function :atan! module_function :atan module_function :atan2! module_function :atan2 - module_function :acosh! - module_function :acosh + module_function :asinh! module_function :asinh + module_function :acosh! + module_function :acosh module_function :atanh! module_function :atanh - -end -# Documentation comments: -# - source: original (researched from pickaxe) -# - a couple of fixme's -# - RDoc output for Bignum etc. is a bit short, with nothing but an -# (undocumented) alias. No big deal. +end diff --git a/lib/mathn.rb b/lib/mathn.rb index 724d37ea6f..f3be55eb6d 100644 --- a/lib/mathn.rb +++ b/lib/mathn.rb @@ -127,7 +127,7 @@ class Rational if other.kind_of?(Rational) other2 = other if self < 0 - return Complex.new!(self, 0) ** other + return Complex.__send__(:new!, self, 0) ** other elsif other == 0 return Rational(1,1) elsif self == 0 @@ -175,7 +175,7 @@ class Rational num = 1 den = 1 end - Rational.new!(num, den) + Rational(num, den) elsif other.kind_of?(Float) Float(self) ** other else @@ -187,7 +187,7 @@ class Rational def power2(other) if other.kind_of?(Rational) if self < 0 - return Complex(self, 0) ** other + return Complex.__send__(:new!, self, 0) ** other elsif other == 0 return Rational(1,1) elsif self == 0 @@ -219,7 +219,7 @@ class Rational num = 1 den = 1 end - Rational.new!(num, den) + Rational(num, den) elsif other.kind_of?(Float) Float(self) ** other else @@ -306,4 +306,3 @@ end class Complex Unify = true end - diff --git a/lib/rational.rb b/lib/rational.rb index 59588528ab..b12bf7ef38 100644 --- a/lib/rational.rb +++ b/lib/rational.rb @@ -1,469 +1,23 @@ -# -# rational.rb - -# $Release Version: 0.5 $ -# $Revision: 1.7 $ -# by Keiju ISHITSUKA(SHL Japan Inc.) -# -# Documentation by Kevin Jackson and Gavin Sinclair. -# -# When you require 'rational', all interactions between numbers -# potentially return a rational result. For example: -# -# 1.quo(2) # -> 0.5 -# require 'rational' -# 1.quo(2) # -> Rational(1,2) -# -# See Rational for full documentation. -# - -# -# Creates a Rational number (i.e. a fraction). +a+ and +b+ should be Integers: -# -# Rational(1,3) # -> 1/3 -# -# Note: trying to construct a Rational with floating point or real values -# produces errors: -# -# Rational(1.1, 2.3) # -> NoMethodError -# -def Rational(a, b = 1) - if a.kind_of?(Rational) && b == 1 - a - else - Rational.reduce(a, b) - end -end - -# -# Rational implements a rational class for numbers. -# -# A rational number is a number that can be expressed as a fraction p/q -# where p and q are integers and q != 0. A rational number p/q is said to have -# numerator p and denominator q. Numbers that are not rational are called -# irrational numbers. (http://mathworld.wolfram.com/RationalNumber.html) -# -# To create a Rational Number: -# Rational(a,b) # -> a/b -# Rational.new!(a,b) # -> a/b -# -# Examples: -# Rational(5,6) # -> 5/6 -# Rational(5) # -> 5/1 -# -# Rational numbers are reduced to their lowest terms: -# Rational(6,10) # -> 3/5 -# -# But not if you use the unusual method "new!": -# Rational.new!(6,10) # -> 6/10 -# -# Division by zero is obviously not allowed: -# Rational(3,0) # -> ZeroDivisionError -# -class Rational < Numeric - @RCS_ID='-$Id: rational.rb,v 1.7 1999/08/24 12:49:28 keiju Exp keiju $-' - - # - # Reduces the given numerator and denominator to their lowest terms. Use - # Rational() instead. - # - def Rational.reduce(num, den = 1) - raise ZeroDivisionError, "denominator is zero" if den == 0 - - if den < 0 - num = -num - den = -den - end - gcd = num.gcd(den) - num = num.div(gcd) - den = den.div(gcd) - if den == 1 && defined?(Unify) - num - else - new!(num, den) - end - end - - # - # Implements the constructor. This method does not reduce to lowest terms or - # check for division by zero. Therefore #Rational() should be preferred in - # normal use. - # - def Rational.new!(num, den = 1) - new(num, den) - end - - private_class_method :new - - # - # This method is actually private. - # - def initialize(num, den) - if den < 0 - num = -num - den = -den - end - if num.kind_of?(Integer) and den.kind_of?(Integer) - @numerator = num - @denominator = den - else - @numerator = num.to_i - @denominator = den.to_i - end - end - - # - # Returns the addition of this value and +a+. - # - # Examples: - # r = Rational(3,4) # -> Rational(3,4) - # r + 1 # -> Rational(7,4) - # r + 0.5 # -> 1.25 - # - def + (a) - if a.kind_of?(Rational) - num = @numerator * a.denominator - num_a = a.numerator * @denominator - Rational(num + num_a, @denominator * a.denominator) - elsif a.kind_of?(Integer) - self + Rational.new!(a, 1) - elsif a.kind_of?(Float) - Float(self) + a - else - x, y = a.coerce(self) - x + y - end - end - - # - # Returns the difference of this value and +a+. - # subtracted. - # - # Examples: - # r = Rational(3,4) # -> Rational(3,4) - # r - 1 # -> Rational(-1,4) - # r - 0.5 # -> 0.25 - # - def - (a) - if a.kind_of?(Rational) - num = @numerator * a.denominator - num_a = a.numerator * @denominator - Rational(num - num_a, @denominator*a.denominator) - elsif a.kind_of?(Integer) - self - Rational.new!(a, 1) - elsif a.kind_of?(Float) - Float(self) - a - else - x, y = a.coerce(self) - x - y - end - end - - # - # Returns the product of this value and +a+. - # - # Examples: - # r = Rational(3,4) # -> Rational(3,4) - # r * 2 # -> Rational(3,2) - # r * 4 # -> Rational(3,1) - # r * 0.5 # -> 0.375 - # r * Rational(1,2) # -> Rational(3,8) - # - def * (a) - if a.kind_of?(Rational) - num = @numerator * a.numerator - den = @denominator * a.denominator - Rational(num, den) - elsif a.kind_of?(Integer) - self * Rational.new!(a, 1) - elsif a.kind_of?(Float) - Float(self) * a - else - x, y = a.coerce(self) - x * y - end - end - - # - # Returns the quotient of this value and +a+. - # r = Rational(3,4) # -> Rational(3,4) - # r / 2 # -> Rational(3,8) - # r / 2.0 # -> 0.375 - # r / Rational(1,2) # -> Rational(3,2) - # - def / (a) - if a.kind_of?(Rational) - num = @numerator * a.denominator - den = @denominator * a.numerator - Rational(num, den) - elsif a.kind_of?(Integer) - raise ZeroDivisionError, "division by zero" if a == 0 - self / Rational.new!(a, 1) - elsif a.kind_of?(Float) - Float(self) / a - else - x, y = a.coerce(self) - x / y - end - end - - # - # Returns this value raised to the given power. - # - # Examples: - # r = Rational(3,4) # -> Rational(3,4) - # r ** 2 # -> Rational(9,16) - # r ** 2.0 # -> 0.5625 - # r ** Rational(1,2) # -> 0.866025403784439 - # - def ** (other) - if other.kind_of?(Rational) - Float(self) ** other - elsif other.kind_of?(Integer) - if other > 0 - num = @numerator ** other - den = @denominator ** other - elsif other < 0 - num = @denominator ** -other - den = @numerator ** -other - elsif other == 0 - num = 1 - den = 1 - end - Rational.new!(num, den) - elsif other.kind_of?(Float) - Float(self) ** other - else - x, y = other.coerce(self) - x ** y - end - end - - def div(other) - (self / other).floor - end - - # - # Returns the remainder when this value is divided by +other+. - # - # Examples: - # r = Rational(7,4) # -> Rational(7,4) - # r % Rational(1,2) # -> Rational(1,4) - # r % 1 # -> Rational(3,4) - # r % Rational(1,7) # -> Rational(1,28) - # r % 0.26 # -> 0.19 - # - def % (other) - value = (self / other).floor - return self - other * value - end - - # - # Returns the quotient _and_ remainder. - # - # Examples: - # r = Rational(7,4) # -> Rational(7,4) - # r.divmod Rational(1,2) # -> [3, Rational(1,4)] - # - def divmod(other) - value = (self / other).floor - return value, self - other * value - end - - # - # Returns the absolute value. - # - def abs - if @numerator > 0 - self - else - Rational.new!(-@numerator, @denominator) - end - end - - # - # Returns +true+ iff this value is numerically equal to +other+. - # - # But beware: - # Rational(1,2) == Rational(4,8) # -> true - # Rational(1,2) == Rational.new!(4,8) # -> false - # - # Don't use Rational.new! - # - def == (other) - if other.kind_of?(Rational) - @numerator == other.numerator and @denominator == other.denominator - elsif other.kind_of?(Integer) - self == Rational.new!(other, 1) - elsif other.kind_of?(Float) - Float(self) == other - else - other == self - end - end - - # - # Standard comparison operator. - # - def <=> (other) - if other.kind_of?(Rational) - num = @numerator * other.denominator - num_a = other.numerator * @denominator - v = num - num_a - if v > 0 - return 1 - elsif v < 0 - return -1 - else - return 0 - end - elsif other.kind_of?(Integer) - return self <=> Rational.new!(other, 1) - elsif other.kind_of?(Float) - return Float(self) <=> other - elsif defined? other.coerce - x, y = other.coerce(self) - return x <=> y - else - return nil - end - end - - def coerce(other) - if other.kind_of?(Float) - return other, self.to_f - elsif other.kind_of?(Integer) - return Rational.new!(other, 1), self - else - super - end - end - - # - # Converts the rational to an Integer. Not the _nearest_ integer, the - # truncated integer. Study the following example carefully: - # Rational(+7,4).to_i # -> 1 - # Rational(-7,4).to_i # -> -2 - # (-1.75).to_i # -> -1 - # - # In other words: - # Rational(-7,4) == -1.75 # -> true - # Rational(-7,4).to_i == (-1.75).to_i # false - # - - def floor() - @numerator.div(@denominator) - end - - def ceil() - -((-@numerator).div(@denominator)) - end - - def truncate() - if @numerator < 0 - return -((-@numerator).div(@denominator)) - end - @numerator.div(@denominator) - end - - alias_method :to_i, :truncate - - def round() - if @numerator < 0 - num = -@numerator - num = num * 2 + @denominator - den = @denominator * 2 - -(num.div(den)) - else - num = @numerator * 2 + @denominator - den = @denominator * 2 - num.div(den) - end - end +class Fixnum - # - # Converts the rational to a Float. - # - def to_f - @numerator.quof(@denominator) - end + alias quof fdiv - # - # Returns a string representation of the rational number. - # - # Example: - # Rational(3,4).to_s # "3/4" - # Rational(8).to_s # "8" - # - def to_s - if @denominator == 1 - @numerator.to_s - else - @numerator.to_s+"/"+@denominator.to_s - end - end + alias power! ** + alias rpower ** - # - # Returns +self+. - # - def to_r - self - end +end - # - # Returns a reconstructable string representation: - # - # Rational(5,8).inspect # -> "Rational(5, 8)" - # - def inspect - sprintf("Rational(%s, %s)", @numerator.inspect, @denominator.inspect) - end +class Bignum - # - # Returns a hash code for the object. - # - def hash - @numerator.hash ^ @denominator.hash - end + alias quof fdiv - attr :numerator - attr :denominator + alias power! ** + alias rpower ** - private :initialize end class Integer - # - # In an integer, the value _is_ the numerator of its rational equivalent. - # Therefore, this method returns +self+. - # - def numerator - self - end - - # - # In an integer, the denominator is 1. Therefore, this method returns 1. - # - def denominator - 1 - end - # - # Returns a Rational representation of this integer. - # - def to_r - Rational(self, 1) - end - - # - # Returns the greatest common denominator of the two numbers (+self+ - # and +n+). - # - # Examples: - # 72.gcd 168 # -> 24 - # 19.gcd 36 # -> 1 - # - # The result is positive, no matter the sign of the arguments. - # def gcd(other) min = self.abs max = other.abs @@ -475,10 +29,6 @@ class Integer max end - # Examples: - # 6.lcm 7 # -> 42 - # 6.lcm 9 # -> 18 - # def lcm(other) if self.zero? or other.zero? 0 @@ -486,15 +36,7 @@ class Integer (self.div(self.gcd(other)) * other).abs end end - - # - # Returns the GCD _and_ the LCM (see #gcd and #lcm) of the two arguments - # (+self+ and +other+). This is more efficient than calculating them - # separately. - # - # Example: - # 6.gcdlcm 9 # -> [3, 18] - # + def gcdlcm(other) gcd = self.gcd(other) if self.zero? or other.zero? @@ -503,55 +45,5 @@ class Integer [gcd, (self.div(gcd) * other).abs] end end -end - -class Fixnum - alias quof quo - remove_method :quo - - # If Rational is defined, returns a Rational number instead of a Float. - def quo(other) - Rational.new!(self, 1) / other - end - alias rdiv quo - # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0). - def rpower (other) - if other >= 0 - self.power!(other) - else - Rational.new!(self, 1)**other - end - end -end - -class Bignum - alias quof quo - remove_method :quo - - # If Rational is defined, returns a Rational number instead of a Float. - def quo(other) - Rational.new!(self, 1) / other - end - alias rdiv quo - - # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0). - def rpower (other) - if other >= 0 - self.power!(other) - else - Rational.new!(self, 1)**other - end - end -end - -unless defined? 1.power! - class Fixnum - alias power! ** - alias ** rpower - end - class Bignum - alias power! ** - alias ** rpower - end end -- cgit v1.2.3