| Class | Rational |
| In: |
lib/rational.rb
|
| Parent: | Numeric |
Rational implements a rational class for numbers.
A rational number is a number that can be expressed as a fraction p/q where p and q are integers and q != 0. A rational number p/q is said to have numerator p and denominator q. Numbers that are not rational are called irrational numbers. (mathworld.wolfram.com/RationalNumber.html)
To create a Rational Number:
Rational(a,b) # -> a/b Rational.new!(a,b) # -> a/b
Examples:
Rational(5,6) # -> 5/6 Rational(5) # -> 5/1
Rational numbers are reduced to their lowest terms:
Rational(6,10) # -> 3/5
But not if you use the unusual method "new!":
Rational.new!(6,10) # -> 6/10
Division by zero is obviously not allowed:
Rational(3,0) # -> ZeroDivisionError
| denominator | [R] | |
| numerator | [R] |
This method is actually private.
# File lib/rational.rb, line 102
102: def initialize(num, den)
103: if den < 0
104: num = -num
105: den = -den
106: end
107: if num.kind_of?(Integer) and den.kind_of?(Integer)
108: @numerator = num
109: @denominator = den
110: else
111: @numerator = num.to_i
112: @denominator = den.to_i
113: end
114: end
Reduces the given numerator and denominator to their lowest terms. Use Rational() instead.
# File lib/rational.rb, line 71
71: def Rational.reduce(num, den = 1)
72: raise ZeroDivisionError, "denominator is zero" if den == 0
73:
74: if den < 0
75: num = -num
76: den = -den
77: end
78: gcd = num.gcd(den)
79: num = num.div(gcd)
80: den = den.div(gcd)
81: if den == 1 && defined?(Unify)
82: num
83: else
84: new!(num, den)
85: end
86: end
Returns the remainder when this value is divided by other.
Examples:
r = Rational(7,4) # -> Rational(7,4) r % Rational(1,2) # -> Rational(1,4) r % 1 # -> Rational(3,4) r % Rational(1,7) # -> Rational(1,28) r % 0.26 # -> 0.19
# File lib/rational.rb, line 253
253: def % (other)
254: value = (self / other).to_i
255: return self - other * value
256: end
Returns the product of this value and a.
Examples:
r = Rational(3,4) # -> Rational(3,4) r * 2 # -> Rational(3,2) r * 4 # -> Rational(3,1) r * 0.5 # -> 0.375 r * Rational(1,2) # -> Rational(3,8)
# File lib/rational.rb, line 173
173: def * (a)
174: if a.kind_of?(Rational)
175: num = @numerator * a.numerator
176: den = @denominator * a.denominator
177: Rational(num, den)
178: elsif a.kind_of?(Integer)
179: self * Rational.new!(a, 1)
180: elsif a.kind_of?(Float)
181: Float(self) * a
182: else
183: x, y = a.coerce(self)
184: x * y
185: end
186: end
Returns this value raised to the given power.
Examples:
r = Rational(3,4) # -> Rational(3,4) r ** 2 # -> Rational(9,16) r ** 2.0 # -> 0.5625 r ** Rational(1,2) # -> 0.866025403784439
# File lib/rational.rb, line 220
220: def ** (other)
221: if other.kind_of?(Rational)
222: Float(self) ** other
223: elsif other.kind_of?(Integer)
224: if other > 0
225: num = @numerator ** other
226: den = @denominator ** other
227: elsif other < 0
228: num = @denominator ** -other
229: den = @numerator ** -other
230: elsif other == 0
231: num = 1
232: den = 1
233: end
234: Rational.new!(num, den)
235: elsif other.kind_of?(Float)
236: Float(self) ** other
237: else
238: x, y = other.coerce(self)
239: x ** y
240: end
241: end
Returns the addition of this value and a.
Examples:
r = Rational(3,4) # -> Rational(3,4) r + 1 # -> Rational(7,4) r + 0.5 # -> 1.25
# File lib/rational.rb, line 124
124: def + (a)
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)
132: Float(self) + a
133: else
134: x, y = a.coerce(self)
135: x + y
136: end
137: end
Returns the difference of this value and a. subtracted.
Examples:
r = Rational(3,4) # -> Rational(3,4) r - 1 # -> Rational(-1,4) r - 0.5 # -> 0.25
# File lib/rational.rb, line 148
148: def - (a)
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)
156: Float(self) - a
157: else
158: x, y = a.coerce(self)
159: x - y
160: end
161: end
Returns the quotient of this value and a.
r = Rational(3,4) # -> Rational(3,4) r / 2 # -> Rational(3,8) r / 2.0 # -> 0.375 r / Rational(1,2) # -> Rational(3,2)
# File lib/rational.rb, line 195
195: def / (a)
196: if a.kind_of?(Rational)
197: num = @numerator * a.denominator
198: den = @denominator * a.numerator
199: Rational(num, den)
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)
204: Float(self) / a
205: else
206: x, y = a.coerce(self)
207: x / y
208: end
209: end
Standard comparison operator.
# File lib/rational.rb, line 305
305: def <=> (other)
306: if other.kind_of?(Rational)
307: num = @numerator * other.denominator
308: num_a = other.numerator * @denominator
309: v = num - num_a
310: if v > 0
311: return 1
312: elsif v < 0
313: return -1
314: else
315: return 0
316: end
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)
323: return x <=> y
324: else
325: return nil
326: end
327: end
Returns true iff this value is numerically equal to other.
But beware:
Rational(1,2) == Rational(4,8) # -> true Rational(1,2) == Rational.new!(4,8) # -> false
Don‘t use Rational.new!
# File lib/rational.rb, line 290
290: def == (other)
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)
296: Float(self) == other
297: else
298: other == self
299: end
300: end
Returns the absolute value.
# File lib/rational.rb, line 273
273: def abs
274: if @numerator > 0
275: Rational.new!(@numerator, @denominator)
276: else
277: Rational.new!(-@numerator, @denominator)
278: end
279: end
# File lib/rational.rb, line 329
329: def coerce(other)
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
334: else
335: super
336: end
337: end
Returns the quotient and remainder.
Examples:
r = Rational(7,4) # -> Rational(7,4) r.divmod Rational(1,2) # -> [3, Rational(1,4)]
# File lib/rational.rb, line 265
265: def divmod(other)
266: value = (self / other).to_i
267: return value, self - other * value
268: end
Returns a hash code for the object.
# File lib/rational.rb, line 395
395: def hash
396: @numerator.hash ^ @denominator.hash
397: end
Returns a reconstructable string representation:
Rational(5,8).inspect # -> "Rational(5, 8)"
# File lib/rational.rb, line 388
388: def inspect
389: sprintf("Rational(%s, %s)", @numerator.inspect, @denominator.inspect)
390: end
Converts the rational to an Integer. Not the nearest integer, the truncated integer. Study the following example carefully:
Rational(+7,4).to_i # -> 1 Rational(-7,4).to_i # -> -2 (-1.75).to_i # -> -1
In other words:
Rational(-7,4) == -1.75 # -> true Rational(-7,4).to_i == (-1.75).to_i # false
# File lib/rational.rb, line 350
350: def to_i
351: Integer(@numerator.div(@denominator))
352: end