3 # $Release Version: 0.5 $
5 # $Date: 1999/08/24 12:49:28 $
6 # by Keiju ISHITSUKA(SHL Japan Inc.)
8 # Documentation by Kevin Jackson and Gavin Sinclair.
10 # When you <tt>require 'rational'</tt>, all interactions between numbers
11 # potentially return a rational result. For example:
15 # 1.quo(2) # -> Rational(1,2)
17 # See Rational for full documentation.
22 # Creates a Rational number (i.e. a fraction). +a+ and +b+ should be Integers:
24 # Rational(1,3) # -> 1/3
26 # Note: trying to construct a Rational with floating point or real values
29 # Rational(1.1, 2.3) # -> NoMethodError
31 def Rational(a, b = 1)
32 if a.kind_of?(Rational) && b == 1
40 # Rational implements a rational class for numbers.
42 # <em>A rational number is a number that can be expressed as a fraction p/q
43 # where p and q are integers and q != 0. A rational number p/q is said to have
44 # numerator p and denominator q. Numbers that are not rational are called
45 # irrational numbers.</em> (http://mathworld.wolfram.com/RationalNumber.html)
47 # To create a Rational Number:
48 # Rational(a,b) # -> a/b
49 # Rational.new!(a,b) # -> a/b
52 # Rational(5,6) # -> 5/6
53 # Rational(5) # -> 5/1
55 # Rational numbers are reduced to their lowest terms:
56 # Rational(6,10) # -> 3/5
58 # But not if you use the unusual method "new!":
59 # Rational.new!(6,10) # -> 6/10
61 # Division by zero is obviously not allowed:
62 # Rational(3,0) # -> ZeroDivisionError
64 class Rational < Numeric
65 @RCS_ID='-$Id: rational.rb,v 1.7 1999/08/24 12:49:28 keiju Exp keiju $-'
68 # Reduces the given numerator and denominator to their lowest terms. Use
71 def Rational.reduce(num, den = 1)
72 raise ZeroDivisionError, "denominator is zero" if den == 0
81 if den == 1 && defined?(Unify)
89 # Implements the constructor. This method does not reduce to lowest terms or
90 # check for division by zero. Therefore #Rational() should be preferred in
93 def Rational.new!(num, den = 1)
97 private_class_method :new
100 # This method is actually private.
102 def initialize(num, den)
107 if num.kind_of?(Integer) and den.kind_of?(Integer)
111 @numerator = num.to_i
112 @denominator = den.to_i
117 # Returns the addition of this value and +a+.
120 # r = Rational(3,4) # -> Rational(3,4)
121 # r + 1 # -> Rational(7,4)
125 if a.kind_of?(Rational)
126 num = @numerator * a.denominator
127 num_a = a.numerator * @denominator
128 Rational(num + num_a, @denominator * a.denominator)
129 elsif a.kind_of?(Integer)
130 self + Rational.new!(a, 1)
131 elsif a.kind_of?(Float)
134 x, y = a.coerce(self)
140 # Returns the difference of this value and +a+.
144 # r = Rational(3,4) # -> Rational(3,4)
145 # r - 1 # -> Rational(-1,4)
149 if a.kind_of?(Rational)
150 num = @numerator * a.denominator
151 num_a = a.numerator * @denominator
152 Rational(num - num_a, @denominator*a.denominator)
153 elsif a.kind_of?(Integer)
154 self - Rational.new!(a, 1)
155 elsif a.kind_of?(Float)
158 x, y = a.coerce(self)
164 # Returns the product of this value and +a+.
167 # r = Rational(3,4) # -> Rational(3,4)
168 # r * 2 # -> Rational(3,2)
169 # r * 4 # -> Rational(3,1)
171 # r * Rational(1,2) # -> Rational(3,8)
174 if a.kind_of?(Rational)
175 num = @numerator * a.numerator
176 den = @denominator * a.denominator
178 elsif a.kind_of?(Integer)
179 self * Rational.new!(a, 1)
180 elsif a.kind_of?(Float)
183 x, y = a.coerce(self)
189 # Returns the quotient of this value and +a+.
190 # r = Rational(3,4) # -> Rational(3,4)
191 # r / 2 # -> Rational(3,8)
193 # r / Rational(1,2) # -> Rational(3,2)
196 if a.kind_of?(Rational)
197 num = @numerator * a.denominator
198 den = @denominator * a.numerator
200 elsif a.kind_of?(Integer)
201 raise ZeroDivisionError, "division by zero" if a == 0
202 self / Rational.new!(a, 1)
203 elsif a.kind_of?(Float)
206 x, y = a.coerce(self)
212 # Returns this value raised to the given power.
215 # r = Rational(3,4) # -> Rational(3,4)
216 # r ** 2 # -> Rational(9,16)
217 # r ** 2.0 # -> 0.5625
218 # r ** Rational(1,2) # -> 0.866025403784439
221 if other.kind_of?(Rational)
223 elsif other.kind_of?(Integer)
225 num = @numerator ** other
226 den = @denominator ** other
228 num = @denominator ** -other
229 den = @numerator ** -other
234 Rational.new!(num, den)
235 elsif other.kind_of?(Float)
238 x, y = other.coerce(self)
244 # Returns the remainder when this value is divided by +other+.
247 # r = Rational(7,4) # -> Rational(7,4)
248 # r % Rational(1,2) # -> Rational(1,4)
249 # r % 1 # -> Rational(3,4)
250 # r % Rational(1,7) # -> Rational(1,28)
254 value = (self / other).to_i
255 return self - other * value
259 # Returns the quotient _and_ remainder.
262 # r = Rational(7,4) # -> Rational(7,4)
263 # r.divmod Rational(1,2) # -> [3, Rational(1,4)]
266 value = (self / other).to_i
267 return value, self - other * value
271 # Returns the absolute value.
275 Rational.new!(@numerator, @denominator)
277 Rational.new!(-@numerator, @denominator)
282 # Returns +true+ iff this value is numerically equal to +other+.
285 # Rational(1,2) == Rational(4,8) # -> true
286 # Rational(1,2) == Rational.new!(4,8) # -> false
288 # Don't use Rational.new!
291 if other.kind_of?(Rational)
292 @numerator == other.numerator and @denominator == other.denominator
293 elsif other.kind_of?(Integer)
294 self == Rational.new!(other, 1)
295 elsif other.kind_of?(Float)
303 # Standard comparison operator.
306 if other.kind_of?(Rational)
307 num = @numerator * other.denominator
308 num_a = other.numerator * @denominator
317 elsif other.kind_of?(Integer)
318 return self <=> Rational.new!(other, 1)
319 elsif other.kind_of?(Float)
320 return Float(self) <=> other
321 elsif defined? other.coerce
322 x, y = other.coerce(self)
330 if other.kind_of?(Float)
331 return other, self.to_f
332 elsif other.kind_of?(Integer)
333 return Rational.new!(other, 1), self
340 # Converts the rational to an Integer. Not the _nearest_ integer, the
341 # truncated integer. Study the following example carefully:
342 # Rational(+7,4).to_i # -> 1
343 # Rational(-7,4).to_i # -> -2
344 # (-1.75).to_i # -> -1
347 # Rational(-7,4) == -1.75 # -> true
348 # Rational(-7,4).to_i == (-1.75).to_i # false
351 Integer(@numerator.div(@denominator))
355 # Converts the rational to a Float.
358 @numerator.to_f/@denominator.to_f
362 # Returns a string representation of the rational number.
365 # Rational(3,4).to_s # "3/4"
366 # Rational(8).to_s # "8"
372 @numerator.to_s+"/"+@denominator.to_s
384 # Returns a reconstructable string representation:
386 # Rational(5,8).inspect # -> "Rational(5, 8)"
389 sprintf("Rational(%s, %s)", @numerator.inspect, @denominator.inspect)
393 # Returns a hash code for the object.
396 @numerator.hash ^ @denominator.hash
407 # In an integer, the value _is_ the numerator of its rational equivalent.
408 # Therefore, this method returns +self+.
415 # In an integer, the denominator is 1. Therefore, this method returns 1.
422 # Returns a Rational representation of this integer.
429 # Returns the <em>greatest common denominator</em> of the two numbers (+self+
436 # The result is positive, no matter the sign of the arguments.
450 # Returns the <em>lowest common multiple</em> (LCM) of the two arguments
451 # (+self+ and +other+).
458 if self.zero? or other.zero?
461 (self.div(self.gcd(other)) * other).abs
466 # Returns the GCD _and_ the LCM (see #gcd and #lcm) of the two arguments
467 # (+self+ and +other+). This is more efficient than calculating them
471 # 6.gcdlcm 9 # -> [3, 18]
474 gcd = self.gcd(other)
475 if self.zero? or other.zero?
478 [gcd, (self.div(gcd) * other).abs]
485 # If Rational is defined, returns a Rational number instead of a Fixnum.
487 Rational.new!(self,1) / other
491 # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0).
496 Rational.new!(self,1)**other
500 unless defined? 1.power!
507 unless defined? Complex
512 # If Rational is defined, returns a Rational number instead of a Bignum.
514 Rational.new!(self,1) / other
518 # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0).
523 Rational.new!(self, 1)**other
527 unless defined? Complex