summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/complex.rb41
-rw-r--r--lib/mathn.rb5
-rw-r--r--lib/rational.rb75
3 files changed, 91 insertions, 30 deletions
diff --git a/lib/complex.rb b/lib/complex.rb
index eb2d5d61d6..9a621c033f 100644
--- a/lib/complex.rb
+++ b/lib/complex.rb
@@ -104,6 +104,10 @@ class Complex < Numeric
@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 @@ class Complex < Numeric
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 @@ class Complex < Numeric
#
# 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 @@ class Complex < Numeric
x % y
end
end
-
+=end
+
#--
# def divmod(other)
# if other.kind_of?(Complex)
@@ -312,8 +323,6 @@ class Complex < Numeric
end
alias conj conjugate
- undef <=>
-
#
# Test for numerical equality (<tt>a == a + 0<i>i</i></tt>).
#
@@ -410,8 +419,34 @@ class Complex < Numeric
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
diff --git a/lib/mathn.rb b/lib/mathn.rb
index 3e24dd03f3..724d37ea6f 100644
--- a/lib/mathn.rb
+++ b/lib/mathn.rb
@@ -121,11 +121,6 @@ end
class Rational
Unify = true
- remove_method :inspect
- def inspect
- format "%s/%s", numerator.inspect, denominator.inspect
- end
-
alias power! **
def ** (other)
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