ruby-changes:3999
From: ko1@a...
Date: Fri, 15 Feb 2008 20:40:33 +0900 (JST)
Subject: [ruby-changes:3999] tadf - Ruby:r15489 (ruby_1_8): * lib/rational.rb (floor, ceil, truncate, round): do not use
tadf 2008-02-15 20:39:50 +0900 (Fri, 15 Feb 2008)
New Revision: 15489
Modified files:
branches/ruby_1_8/ChangeLog
branches/ruby_1_8/lib/complex.rb
branches/ruby_1_8/lib/rational.rb
Log:
* 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.
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=15489&r2=15488&diff_format=u
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/lib/complex.rb?r1=15489&r2=15488&diff_format=u
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/lib/rational.rb?r1=15489&r2=15488&diff_format=u
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog (revision 15488)
+++ ruby_1_8/ChangeLog (revision 15489)
@@ -1,3 +1,18 @@
+Fri Feb 15 20:37:06 2008 Tadayoshi Funaba <tadf@d...>
+
+ * 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.
+
Fri Feb 15 15:23:12 2008 Nobuyoshi Nakada <nobu@r...>
* ext/iconv/iconv.c (iconv_convert): check upper bound. a patch from
Index: ruby_1_8/lib/rational.rb
===================================================================
--- ruby_1_8/lib/rational.rb (revision 15488)
+++ ruby_1_8/lib/rational.rb (revision 15489)
@@ -240,6 +240,10 @@
end
end
+ def div(other)
+ (self / other).floor
+ end
+
#
# Returns the remainder when this value is divided by +other+.
#
@@ -251,7 +255,7 @@
# 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 @@
# 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 @@
#
def abs
if @numerator > 0
- Rational.new!(@numerator, @denominator)
+ self
else
Rational.new!(-@numerator, @denominator)
end
@@ -347,10 +351,38 @@
# 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
+
#
# Converts the rational to a Float.
#
@@ -481,10 +513,11 @@
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 @@
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 @@
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
Index: ruby_1_8/lib/complex.rb
===================================================================
--- ruby_1_8/lib/complex.rb (revision 15488)
+++ ruby_1_8/lib/complex.rb (revision 15489)
@@ -102,6 +102,8 @@
@RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-'
undef step
+ undef div, divmod
+ undef floor, truncate, ceil, round
def Complex.generic?(other) # :nodoc:
other.kind_of?(Integer) or
@@ -194,6 +196,10 @@
end
end
+ def quo(other)
+ Complex(@real.quo(1), @image.quo(1)) / other
+ end
+
#
# Raise this complex number to the given (real or complex) power.
#
@@ -409,9 +415,35 @@
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
--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/