diff options
author | tadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-02-15 11:39:50 +0000 |
---|---|---|
committer | tadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-02-15 11:39:50 +0000 |
commit | c786b2e6ae95b644270bd7755b3caeb6a25491a4 (patch) | |
tree | 825387a701f62c19c0f3960e8073c539ce0f2a2f /lib/rational.rb | |
parent | ca99b96bc9d2a0c8dc2c9cf8c8b56d58b606e723 (diff) |
* 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 (div, divmod, floor, ceil, truncate, round):
undef'ed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@15489 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rational.rb')
-rw-r--r-- | lib/rational.rb | 74 |
1 files changed, 54 insertions, 20 deletions
diff --git a/lib/rational.rb b/lib/rational.rb index ce754cfa3c..4d0097b02e 100644 --- a/lib/rational.rb +++ b/lib/rational.rb @@ -240,6 +240,10 @@ class Rational < Numeric end end + def div(other) + (self / other).floor + end + # # Returns the remainder when this value is divided by +other+. # @@ -251,7 +255,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 @@ -263,7 +267,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 @@ -272,7 +276,7 @@ class Rational < Numeric # def abs if @numerator > 0 - Rational.new!(@numerator, @denominator) + self else Rational.new!(-@numerator, @denominator) end @@ -347,8 +351,36 @@ 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 # @@ -481,10 +513,11 @@ class Integer end class Fixnum - 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 @@ -493,25 +526,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 + remove_method :quo - undef quo - # If Rational is defined, returns a Rational number instead of a Bignum. + # 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 @@ -524,7 +550,15 @@ class Bignum end end - unless defined? Complex +end + +unless defined? 1.power! + class Fixnum + alias power! ** + alias ** rpower + end + class Bignum + alias power! ** alias ** rpower end end |