From 2b72892ac0de9ff1d05f2b21ed85ef09aa41c57e Mon Sep 17 00:00:00 2001 From: tadf Date: Tue, 12 Feb 2008 11:47:12 +0000 Subject: * lib/rational.rb (floor, ceil, truncate, round): do not use definitions of Numeric. * lib/rational.rb (to_i): should returns truncated self. * lib/complex.rb (numerator): requires Integer#{numerator,denominator}. * lib/complex.rb (quo): do not use definition of Numeric. * lib/complex.rb (>, >=, <, <=, between?, div, divmod, modulo, floor, ceil, truncate, round): undef'ed. * lib/mathn.rb (Rational#inspect): removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15446 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/rational.rb | 75 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 22 deletions(-) (limited to 'lib/rational.rb') diff --git a/lib/rational.rb b/lib/rational.rb index 1adac61e83..59588528ab 100644 --- a/lib/rational.rb +++ b/lib/rational.rb @@ -238,6 +238,10 @@ class Rational < Numeric end end + def div(other) + (self / other).floor + end + # # Returns the remainder when this value is divided by +other+. # @@ -249,7 +253,7 @@ class Rational < Numeric # r % 0.26 # -> 0.19 # def % (other) - value = (self / other).to_i + value = (self / other).floor return self - other * value end @@ -261,7 +265,7 @@ class Rational < Numeric # r.divmod Rational(1,2) # -> [3, Rational(1,4)] # def divmod(other) - value = (self / other).to_i + value = (self / other).floor return value, self - other * value end @@ -270,7 +274,7 @@ class Rational < Numeric # def abs if @numerator > 0 - Rational.new!(@numerator, @denominator) + self else Rational.new!(-@numerator, @denominator) end @@ -345,8 +349,35 @@ class Rational < Numeric # Rational(-7,4) == -1.75 # -> true # Rational(-7,4).to_i == (-1.75).to_i # false # - def to_i - Integer(@numerator.div(@denominator)) + + 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 # @@ -476,10 +507,11 @@ end class Fixnum alias quof quo - undef quo - # If Rational is defined, returns a Rational number instead of a Fixnum. + remove_method :quo + + # If Rational is defined, returns a Rational number instead of a Float. def quo(other) - Rational.new!(self,1) / other + Rational.new!(self, 1) / other end alias rdiv quo @@ -488,26 +520,18 @@ class Fixnum if other >= 0 self.power!(other) else - Rational.new!(self,1)**other + Rational.new!(self, 1)**other end end - - unless defined? 1.power! - alias power! ** - alias ** rpower - end end class Bignum - unless defined? Complex - alias power! ** - end - alias quof quo - undef quo - # If Rational is defined, returns a Rational number instead of a Bignum. + remove_method :quo + + # If Rational is defined, returns a Rational number instead of a Float. def quo(other) - Rational.new!(self,1) / other + Rational.new!(self, 1) / other end alias rdiv quo @@ -519,8 +543,15 @@ class Bignum Rational.new!(self, 1)**other end end +end - unless defined? Complex +unless defined? 1.power! + class Fixnum + alias power! ** + alias ** rpower + end + class Bignum + alias power! ** alias ** rpower end end -- cgit v1.2.3