1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
tadf 2008-02-12 11:47:12 +00:00
parent 7d4defbe1b
commit 2b72892ac0
4 changed files with 123 additions and 45 deletions

View file

@ -1,3 +1,20 @@
Tue Feb 12 20:32:50 2008 Tadayoshi Funaba <tadf@dotrb.org>
* 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@ruby-lang.org> Tue Feb 12 16:48:10 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (args, mrhs): flattens literal array splats. * parse.y (args, mrhs): flattens literal array splats.

View file

@ -104,6 +104,10 @@ class Complex < Numeric
@RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-' @RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-'
undef step undef step
undef <, <=, <=>, >, >=
undef between?
undef div, divmod, modulo
undef floor, truncate, ceil, round
def scalar? def scalar?
false false
@ -200,6 +204,10 @@ class Complex < Numeric
end end
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. # Raise this complex number to the given (real or complex) power.
# #
@ -248,6 +256,8 @@ class Complex < Numeric
# #
# Remainder after division by a real or complex number. # Remainder after division by a real or complex number.
# #
=begin
def % (other) def % (other)
if other.kind_of?(Complex) if other.kind_of?(Complex)
Complex(@real % other.real, @image % other.image) Complex(@real % other.real, @image % other.image)
@ -258,6 +268,7 @@ class Complex < Numeric
x % y x % y
end end
end end
=end
#-- #--
# def divmod(other) # def divmod(other)
@ -312,8 +323,6 @@ class Complex < Numeric
end end
alias conj conjugate alias conj conjugate
undef <=>
# #
# Test for numerical equality (<tt>a == a + 0<i>i</i></tt>). # Test for numerical equality (<tt>a == a + 0<i>i</i></tt>).
# #
@ -410,8 +419,34 @@ class Complex < Numeric
end 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 module Math
alias sqrt! sqrt alias sqrt! sqrt

View file

@ -121,11 +121,6 @@ end
class Rational class Rational
Unify = true Unify = true
remove_method :inspect
def inspect
format "%s/%s", numerator.inspect, denominator.inspect
end
alias power! ** alias power! **
def ** (other) def ** (other)

View file

@ -238,6 +238,10 @@ class Rational < Numeric
end end
end end
def div(other)
(self / other).floor
end
# #
# Returns the remainder when this value is divided by +other+. # Returns the remainder when this value is divided by +other+.
# #
@ -249,7 +253,7 @@ class Rational < Numeric
# r % 0.26 # -> 0.19 # r % 0.26 # -> 0.19
# #
def % (other) def % (other)
value = (self / other).to_i value = (self / other).floor
return self - other * value return self - other * value
end end
@ -261,7 +265,7 @@ class Rational < Numeric
# r.divmod Rational(1,2) # -> [3, Rational(1,4)] # r.divmod Rational(1,2) # -> [3, Rational(1,4)]
# #
def divmod(other) def divmod(other)
value = (self / other).to_i value = (self / other).floor
return value, self - other * value return value, self - other * value
end end
@ -270,7 +274,7 @@ class Rational < Numeric
# #
def abs def abs
if @numerator > 0 if @numerator > 0
Rational.new!(@numerator, @denominator) self
else else
Rational.new!(-@numerator, @denominator) Rational.new!(-@numerator, @denominator)
end end
@ -345,8 +349,35 @@ class Rational < Numeric
# Rational(-7,4) == -1.75 # -> true # Rational(-7,4) == -1.75 # -> true
# Rational(-7,4).to_i == (-1.75).to_i # false # 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 end
# #
@ -476,8 +507,9 @@ end
class Fixnum class Fixnum
alias quof quo alias quof quo
undef quo remove_method :quo
# If Rational is defined, returns a Rational number instead of a Fixnum.
# If Rational is defined, returns a Rational number instead of a Float.
def quo(other) def quo(other)
Rational.new!(self, 1) / other Rational.new!(self, 1) / other
end end
@ -491,21 +523,13 @@ class Fixnum
Rational.new!(self, 1)**other Rational.new!(self, 1)**other
end end
end end
unless defined? 1.power!
alias power! **
alias ** rpower
end
end end
class Bignum class Bignum
unless defined? Complex
alias power! **
end
alias quof quo alias quof quo
undef quo remove_method :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) def quo(other)
Rational.new!(self, 1) / other Rational.new!(self, 1) / other
end end
@ -519,8 +543,15 @@ class Bignum
Rational.new!(self, 1)**other Rational.new!(self, 1)**other
end end
end end
end
unless defined? Complex unless defined? 1.power!
class Fixnum
alias power! **
alias ** rpower
end
class Bignum
alias power! **
alias ** rpower alias ** rpower
end end
end end