ruby-changes:3956
From: ko1@a...
Date: Tue, 12 Feb 2008 20:47:34 +0900 (JST)
Subject: [ruby-changes:3956] tadf - Ruby:r15446 (trunk): * lib/rational.rb (floor, ceil, truncate, round): do not use
tadf 2008-02-12 20:47:12 +0900 (Tue, 12 Feb 2008)
New Revision: 15446
Modified files:
trunk/ChangeLog
trunk/lib/complex.rb
trunk/lib/mathn.rb
trunk/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 (>, >=, <, <=, between?, div, divmod, modulo,
floor, ceil, truncate, round): undef'ed.
* lib/mathn.rb (Rational#inspect): removed.
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/complex.rb?r1=15446&r2=15445&diff_format=u
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15446&r2=15445&diff_format=u
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/rational.rb?r1=15446&r2=15445&diff_format=u
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/mathn.rb?r1=15446&r2=15445&diff_format=u
Index: ChangeLog
===================================================================
--- ChangeLog (revision 15445)
+++ ChangeLog (revision 15446)
@@ -1,3 +1,20 @@
+Tue Feb 12 20:32:50 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 (>, >=, <, <=, between?, div, divmod, modulo,
+ floor, ceil, truncate, round): undef'ed.
+
+ * lib/mathn.rb (Rational#inspect): removed.
+
Tue Feb 12 16:48:10 2008 Nobuyoshi Nakada <nobu@r...>
* parse.y (args, mrhs): flattens literal array splats.
Index: lib/rational.rb
===================================================================
--- lib/rational.rb (revision 15445)
+++ lib/rational.rb (revision 15446)
@@ -238,6 +238,10 @@
end
end
+ def div(other)
+ (self / other).floor
+ end
+
#
# Returns the remainder when this value is divided by +other+.
#
@@ -249,7 +253,7 @@
# 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 @@
# 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 @@
#
def abs
if @numerator > 0
- Rational.new!(@numerator, @denominator)
+ self
else
Rational.new!(-@numerator, @denominator)
end
@@ -345,10 +349,37 @@
# 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.
#
@@ -476,10 +507,11 @@
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 @@
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
+ remove_method :quo
- alias quof 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
@@ -519,8 +543,15 @@
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
Index: lib/mathn.rb
===================================================================
--- lib/mathn.rb (revision 15445)
+++ lib/mathn.rb (revision 15446)
@@ -121,11 +121,6 @@
class Rational
Unify = true
- remove_method :inspect
- def inspect
- format "%s/%s", numerator.inspect, denominator.inspect
- end
-
alias power! **
def ** (other)
Index: lib/complex.rb
===================================================================
--- lib/complex.rb (revision 15445)
+++ lib/complex.rb (revision 15446)
@@ -104,6 +104,10 @@
@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
@@ -199,6 +203,10 @@
x/y
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.
@@ -248,6 +256,8 @@
#
# Remainder after division by a real or complex number.
#
+
+=begin
def % (other)
if other.kind_of?(Complex)
Complex(@real % other.real, @image % other.image)
@@ -258,7 +268,8 @@
x % y
end
end
-
+=end
+
#--
# def divmod(other)
# if other.kind_of?(Complex)
@@ -312,8 +323,6 @@
end
alias conj conjugate
- undef <=>
-
#
# Test for numerical equality (<tt>a == a + 0<i>i</i></tt>).
#
@@ -410,9 +419,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/