Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: rational.rb
#
[0] Fix | Delete
# rational.rb -
[1] Fix | Delete
# $Release Version: 0.5 $
[2] Fix | Delete
# $Revision: 1.7 $
[3] Fix | Delete
# $Date: 1999/08/24 12:49:28 $
[4] Fix | Delete
# by Keiju ISHITSUKA(SHL Japan Inc.)
[5] Fix | Delete
#
[6] Fix | Delete
# Documentation by Kevin Jackson and Gavin Sinclair.
[7] Fix | Delete
#
[8] Fix | Delete
# When you <tt>require 'rational'</tt>, all interactions between numbers
[9] Fix | Delete
# potentially return a rational result. For example:
[10] Fix | Delete
#
[11] Fix | Delete
# 1.quo(2) # -> 0.5
[12] Fix | Delete
# require 'rational'
[13] Fix | Delete
# 1.quo(2) # -> Rational(1,2)
[14] Fix | Delete
#
[15] Fix | Delete
# See Rational for full documentation.
[16] Fix | Delete
#
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
#
[20] Fix | Delete
# Creates a Rational number (i.e. a fraction). +a+ and +b+ should be Integers:
[21] Fix | Delete
#
[22] Fix | Delete
# Rational(1,3) # -> 1/3
[23] Fix | Delete
#
[24] Fix | Delete
# Note: trying to construct a Rational with floating point or real values
[25] Fix | Delete
# produces errors:
[26] Fix | Delete
#
[27] Fix | Delete
# Rational(1.1, 2.3) # -> NoMethodError
[28] Fix | Delete
#
[29] Fix | Delete
def Rational(a, b = 1)
[30] Fix | Delete
if a.kind_of?(Rational) && b == 1
[31] Fix | Delete
a
[32] Fix | Delete
else
[33] Fix | Delete
Rational.reduce(a, b)
[34] Fix | Delete
end
[35] Fix | Delete
end
[36] Fix | Delete
[37] Fix | Delete
#
[38] Fix | Delete
# Rational implements a rational class for numbers.
[39] Fix | Delete
#
[40] Fix | Delete
# <em>A rational number is a number that can be expressed as a fraction p/q
[41] Fix | Delete
# where p and q are integers and q != 0. A rational number p/q is said to have
[42] Fix | Delete
# numerator p and denominator q. Numbers that are not rational are called
[43] Fix | Delete
# irrational numbers.</em> (http://mathworld.wolfram.com/RationalNumber.html)
[44] Fix | Delete
#
[45] Fix | Delete
# To create a Rational Number:
[46] Fix | Delete
# Rational(a,b) # -> a/b
[47] Fix | Delete
# Rational.new!(a,b) # -> a/b
[48] Fix | Delete
#
[49] Fix | Delete
# Examples:
[50] Fix | Delete
# Rational(5,6) # -> 5/6
[51] Fix | Delete
# Rational(5) # -> 5/1
[52] Fix | Delete
#
[53] Fix | Delete
# Rational numbers are reduced to their lowest terms:
[54] Fix | Delete
# Rational(6,10) # -> 3/5
[55] Fix | Delete
#
[56] Fix | Delete
# But not if you use the unusual method "new!":
[57] Fix | Delete
# Rational.new!(6,10) # -> 6/10
[58] Fix | Delete
#
[59] Fix | Delete
# Division by zero is obviously not allowed:
[60] Fix | Delete
# Rational(3,0) # -> ZeroDivisionError
[61] Fix | Delete
#
[62] Fix | Delete
class Rational < Numeric
[63] Fix | Delete
@RCS_ID='-$Id: rational.rb,v 1.7 1999/08/24 12:49:28 keiju Exp keiju $-'
[64] Fix | Delete
[65] Fix | Delete
#
[66] Fix | Delete
# Reduces the given numerator and denominator to their lowest terms. Use
[67] Fix | Delete
# Rational() instead.
[68] Fix | Delete
#
[69] Fix | Delete
def Rational.reduce(num, den = 1)
[70] Fix | Delete
raise ZeroDivisionError, "denominator is zero" if den == 0
[71] Fix | Delete
[72] Fix | Delete
if den < 0
[73] Fix | Delete
num = -num
[74] Fix | Delete
den = -den
[75] Fix | Delete
end
[76] Fix | Delete
gcd = num.gcd(den)
[77] Fix | Delete
num = num.div(gcd)
[78] Fix | Delete
den = den.div(gcd)
[79] Fix | Delete
if den == 1 && defined?(Unify)
[80] Fix | Delete
num
[81] Fix | Delete
else
[82] Fix | Delete
new!(num, den)
[83] Fix | Delete
end
[84] Fix | Delete
end
[85] Fix | Delete
[86] Fix | Delete
#
[87] Fix | Delete
# Implements the constructor. This method does not reduce to lowest terms or
[88] Fix | Delete
# check for division by zero. Therefore #Rational() should be preferred in
[89] Fix | Delete
# normal use.
[90] Fix | Delete
#
[91] Fix | Delete
def Rational.new!(num, den = 1)
[92] Fix | Delete
new(num, den)
[93] Fix | Delete
end
[94] Fix | Delete
[95] Fix | Delete
private_class_method :new
[96] Fix | Delete
[97] Fix | Delete
#
[98] Fix | Delete
# This method is actually private.
[99] Fix | Delete
#
[100] Fix | Delete
def initialize(num, den)
[101] Fix | Delete
if den < 0
[102] Fix | Delete
num = -num
[103] Fix | Delete
den = -den
[104] Fix | Delete
end
[105] Fix | Delete
if num.kind_of?(Integer) and den.kind_of?(Integer)
[106] Fix | Delete
@numerator = num
[107] Fix | Delete
@denominator = den
[108] Fix | Delete
else
[109] Fix | Delete
@numerator = num.to_i
[110] Fix | Delete
@denominator = den.to_i
[111] Fix | Delete
end
[112] Fix | Delete
end
[113] Fix | Delete
[114] Fix | Delete
#
[115] Fix | Delete
# Returns the addition of this value and +a+.
[116] Fix | Delete
#
[117] Fix | Delete
# Examples:
[118] Fix | Delete
# r = Rational(3,4) # -> Rational(3,4)
[119] Fix | Delete
# r + 1 # -> Rational(7,4)
[120] Fix | Delete
# r + 0.5 # -> 1.25
[121] Fix | Delete
#
[122] Fix | Delete
def + (a)
[123] Fix | Delete
if a.kind_of?(Rational)
[124] Fix | Delete
num = @numerator * a.denominator
[125] Fix | Delete
num_a = a.numerator * @denominator
[126] Fix | Delete
Rational(num + num_a, @denominator * a.denominator)
[127] Fix | Delete
elsif a.kind_of?(Integer)
[128] Fix | Delete
self + Rational.new!(a, 1)
[129] Fix | Delete
elsif a.kind_of?(Float)
[130] Fix | Delete
Float(self) + a
[131] Fix | Delete
else
[132] Fix | Delete
x, y = a.coerce(self)
[133] Fix | Delete
x + y
[134] Fix | Delete
end
[135] Fix | Delete
end
[136] Fix | Delete
[137] Fix | Delete
#
[138] Fix | Delete
# Returns the difference of this value and +a+.
[139] Fix | Delete
# subtracted.
[140] Fix | Delete
#
[141] Fix | Delete
# Examples:
[142] Fix | Delete
# r = Rational(3,4) # -> Rational(3,4)
[143] Fix | Delete
# r - 1 # -> Rational(-1,4)
[144] Fix | Delete
# r - 0.5 # -> 0.25
[145] Fix | Delete
#
[146] Fix | Delete
def - (a)
[147] Fix | Delete
if a.kind_of?(Rational)
[148] Fix | Delete
num = @numerator * a.denominator
[149] Fix | Delete
num_a = a.numerator * @denominator
[150] Fix | Delete
Rational(num - num_a, @denominator*a.denominator)
[151] Fix | Delete
elsif a.kind_of?(Integer)
[152] Fix | Delete
self - Rational.new!(a, 1)
[153] Fix | Delete
elsif a.kind_of?(Float)
[154] Fix | Delete
Float(self) - a
[155] Fix | Delete
else
[156] Fix | Delete
x, y = a.coerce(self)
[157] Fix | Delete
x - y
[158] Fix | Delete
end
[159] Fix | Delete
end
[160] Fix | Delete
[161] Fix | Delete
#
[162] Fix | Delete
# Returns the product of this value and +a+.
[163] Fix | Delete
#
[164] Fix | Delete
# Examples:
[165] Fix | Delete
# r = Rational(3,4) # -> Rational(3,4)
[166] Fix | Delete
# r * 2 # -> Rational(3,2)
[167] Fix | Delete
# r * 4 # -> Rational(3,1)
[168] Fix | Delete
# r * 0.5 # -> 0.375
[169] Fix | Delete
# r * Rational(1,2) # -> Rational(3,8)
[170] Fix | Delete
#
[171] Fix | Delete
def * (a)
[172] Fix | Delete
if a.kind_of?(Rational)
[173] Fix | Delete
num = @numerator * a.numerator
[174] Fix | Delete
den = @denominator * a.denominator
[175] Fix | Delete
Rational(num, den)
[176] Fix | Delete
elsif a.kind_of?(Integer)
[177] Fix | Delete
self * Rational.new!(a, 1)
[178] Fix | Delete
elsif a.kind_of?(Float)
[179] Fix | Delete
Float(self) * a
[180] Fix | Delete
else
[181] Fix | Delete
x, y = a.coerce(self)
[182] Fix | Delete
x * y
[183] Fix | Delete
end
[184] Fix | Delete
end
[185] Fix | Delete
[186] Fix | Delete
#
[187] Fix | Delete
# Returns the quotient of this value and +a+.
[188] Fix | Delete
# r = Rational(3,4) # -> Rational(3,4)
[189] Fix | Delete
# r / 2 # -> Rational(3,8)
[190] Fix | Delete
# r / 2.0 # -> 0.375
[191] Fix | Delete
# r / Rational(1,2) # -> Rational(3,2)
[192] Fix | Delete
#
[193] Fix | Delete
def / (a)
[194] Fix | Delete
if a.kind_of?(Rational)
[195] Fix | Delete
num = @numerator * a.denominator
[196] Fix | Delete
den = @denominator * a.numerator
[197] Fix | Delete
Rational(num, den)
[198] Fix | Delete
elsif a.kind_of?(Integer)
[199] Fix | Delete
raise ZeroDivisionError, "division by zero" if a == 0
[200] Fix | Delete
self / Rational.new!(a, 1)
[201] Fix | Delete
elsif a.kind_of?(Float)
[202] Fix | Delete
Float(self) / a
[203] Fix | Delete
else
[204] Fix | Delete
x, y = a.coerce(self)
[205] Fix | Delete
x / y
[206] Fix | Delete
end
[207] Fix | Delete
end
[208] Fix | Delete
[209] Fix | Delete
#
[210] Fix | Delete
# Returns this value raised to the given power.
[211] Fix | Delete
#
[212] Fix | Delete
# Examples:
[213] Fix | Delete
# r = Rational(3,4) # -> Rational(3,4)
[214] Fix | Delete
# r ** 2 # -> Rational(9,16)
[215] Fix | Delete
# r ** 2.0 # -> 0.5625
[216] Fix | Delete
# r ** Rational(1,2) # -> 0.866025403784439
[217] Fix | Delete
#
[218] Fix | Delete
def ** (other)
[219] Fix | Delete
if other.kind_of?(Rational)
[220] Fix | Delete
Float(self) ** other
[221] Fix | Delete
elsif other.kind_of?(Integer)
[222] Fix | Delete
if other > 0
[223] Fix | Delete
num = @numerator ** other
[224] Fix | Delete
den = @denominator ** other
[225] Fix | Delete
elsif other < 0
[226] Fix | Delete
num = @denominator ** -other
[227] Fix | Delete
den = @numerator ** -other
[228] Fix | Delete
elsif other == 0
[229] Fix | Delete
num = 1
[230] Fix | Delete
den = 1
[231] Fix | Delete
end
[232] Fix | Delete
Rational.new!(num, den)
[233] Fix | Delete
elsif other.kind_of?(Float)
[234] Fix | Delete
Float(self) ** other
[235] Fix | Delete
else
[236] Fix | Delete
x, y = other.coerce(self)
[237] Fix | Delete
x ** y
[238] Fix | Delete
end
[239] Fix | Delete
end
[240] Fix | Delete
[241] Fix | Delete
def div(other)
[242] Fix | Delete
(self / other).floor
[243] Fix | Delete
end
[244] Fix | Delete
[245] Fix | Delete
#
[246] Fix | Delete
# Returns the remainder when this value is divided by +other+.
[247] Fix | Delete
#
[248] Fix | Delete
# Examples:
[249] Fix | Delete
# r = Rational(7,4) # -> Rational(7,4)
[250] Fix | Delete
# r % Rational(1,2) # -> Rational(1,4)
[251] Fix | Delete
# r % 1 # -> Rational(3,4)
[252] Fix | Delete
# r % Rational(1,7) # -> Rational(1,28)
[253] Fix | Delete
# r % 0.26 # -> 0.19
[254] Fix | Delete
#
[255] Fix | Delete
def % (other)
[256] Fix | Delete
value = (self / other).floor
[257] Fix | Delete
return self - other * value
[258] Fix | Delete
end
[259] Fix | Delete
[260] Fix | Delete
#
[261] Fix | Delete
# Returns the quotient _and_ remainder.
[262] Fix | Delete
#
[263] Fix | Delete
# Examples:
[264] Fix | Delete
# r = Rational(7,4) # -> Rational(7,4)
[265] Fix | Delete
# r.divmod Rational(1,2) # -> [3, Rational(1,4)]
[266] Fix | Delete
#
[267] Fix | Delete
def divmod(other)
[268] Fix | Delete
value = (self / other).floor
[269] Fix | Delete
return value, self - other * value
[270] Fix | Delete
end
[271] Fix | Delete
[272] Fix | Delete
#
[273] Fix | Delete
# Returns the absolute value.
[274] Fix | Delete
#
[275] Fix | Delete
def abs
[276] Fix | Delete
if @numerator > 0
[277] Fix | Delete
self
[278] Fix | Delete
else
[279] Fix | Delete
Rational.new!(-@numerator, @denominator)
[280] Fix | Delete
end
[281] Fix | Delete
end
[282] Fix | Delete
[283] Fix | Delete
#
[284] Fix | Delete
# Returns +true+ iff this value is numerically equal to +other+.
[285] Fix | Delete
#
[286] Fix | Delete
# But beware:
[287] Fix | Delete
# Rational(1,2) == Rational(4,8) # -> true
[288] Fix | Delete
# Rational(1,2) == Rational.new!(4,8) # -> false
[289] Fix | Delete
#
[290] Fix | Delete
# Don't use Rational.new!
[291] Fix | Delete
#
[292] Fix | Delete
def == (other)
[293] Fix | Delete
if other.kind_of?(Rational)
[294] Fix | Delete
@numerator == other.numerator and @denominator == other.denominator
[295] Fix | Delete
elsif other.kind_of?(Integer)
[296] Fix | Delete
self == Rational.new!(other, 1)
[297] Fix | Delete
elsif other.kind_of?(Float)
[298] Fix | Delete
Float(self) == other
[299] Fix | Delete
else
[300] Fix | Delete
other == self
[301] Fix | Delete
end
[302] Fix | Delete
end
[303] Fix | Delete
[304] Fix | Delete
#
[305] Fix | Delete
# Standard comparison operator.
[306] Fix | Delete
#
[307] Fix | Delete
def <=> (other)
[308] Fix | Delete
if other.kind_of?(Rational)
[309] Fix | Delete
num = @numerator * other.denominator
[310] Fix | Delete
num_a = other.numerator * @denominator
[311] Fix | Delete
v = num - num_a
[312] Fix | Delete
if v > 0
[313] Fix | Delete
return 1
[314] Fix | Delete
elsif v < 0
[315] Fix | Delete
return -1
[316] Fix | Delete
else
[317] Fix | Delete
return 0
[318] Fix | Delete
end
[319] Fix | Delete
elsif other.kind_of?(Integer)
[320] Fix | Delete
return self <=> Rational.new!(other, 1)
[321] Fix | Delete
elsif other.kind_of?(Float)
[322] Fix | Delete
return Float(self) <=> other
[323] Fix | Delete
elsif defined? other.coerce
[324] Fix | Delete
x, y = other.coerce(self)
[325] Fix | Delete
return x <=> y
[326] Fix | Delete
else
[327] Fix | Delete
return nil
[328] Fix | Delete
end
[329] Fix | Delete
end
[330] Fix | Delete
[331] Fix | Delete
def coerce(other)
[332] Fix | Delete
if other.kind_of?(Float)
[333] Fix | Delete
return other, self.to_f
[334] Fix | Delete
elsif other.kind_of?(Integer)
[335] Fix | Delete
return Rational.new!(other, 1), self
[336] Fix | Delete
else
[337] Fix | Delete
super
[338] Fix | Delete
end
[339] Fix | Delete
end
[340] Fix | Delete
[341] Fix | Delete
#
[342] Fix | Delete
# Converts the rational to an Integer. Not the _nearest_ integer, the
[343] Fix | Delete
# truncated integer. Study the following example carefully:
[344] Fix | Delete
# Rational(+7,4).to_i # -> 1
[345] Fix | Delete
# Rational(-7,4).to_i # -> -1
[346] Fix | Delete
# (-1.75).to_i # -> -1
[347] Fix | Delete
#
[348] Fix | Delete
# In other words:
[349] Fix | Delete
# Rational(-7,4) == -1.75 # -> true
[350] Fix | Delete
# Rational(-7,4).to_i == (-1.75).to_i # -> true
[351] Fix | Delete
#
[352] Fix | Delete
[353] Fix | Delete
[354] Fix | Delete
def floor()
[355] Fix | Delete
@numerator.div(@denominator)
[356] Fix | Delete
end
[357] Fix | Delete
[358] Fix | Delete
def ceil()
[359] Fix | Delete
-((-@numerator).div(@denominator))
[360] Fix | Delete
end
[361] Fix | Delete
[362] Fix | Delete
def truncate()
[363] Fix | Delete
if @numerator < 0
[364] Fix | Delete
return -((-@numerator).div(@denominator))
[365] Fix | Delete
end
[366] Fix | Delete
@numerator.div(@denominator)
[367] Fix | Delete
end
[368] Fix | Delete
[369] Fix | Delete
alias_method :to_i, :truncate
[370] Fix | Delete
[371] Fix | Delete
def round()
[372] Fix | Delete
if @numerator < 0
[373] Fix | Delete
num = -@numerator
[374] Fix | Delete
num = num * 2 + @denominator
[375] Fix | Delete
den = @denominator * 2
[376] Fix | Delete
-(num.div(den))
[377] Fix | Delete
else
[378] Fix | Delete
num = @numerator * 2 + @denominator
[379] Fix | Delete
den = @denominator * 2
[380] Fix | Delete
num.div(den)
[381] Fix | Delete
end
[382] Fix | Delete
end
[383] Fix | Delete
[384] Fix | Delete
#
[385] Fix | Delete
# Converts the rational to a Float.
[386] Fix | Delete
#
[387] Fix | Delete
def to_f
[388] Fix | Delete
@numerator.fdiv(@denominator)
[389] Fix | Delete
end
[390] Fix | Delete
[391] Fix | Delete
#
[392] Fix | Delete
# Returns a string representation of the rational number.
[393] Fix | Delete
#
[394] Fix | Delete
# Example:
[395] Fix | Delete
# Rational(3,4).to_s # "3/4"
[396] Fix | Delete
# Rational(8).to_s # "8"
[397] Fix | Delete
#
[398] Fix | Delete
def to_s
[399] Fix | Delete
if @denominator == 1
[400] Fix | Delete
@numerator.to_s
[401] Fix | Delete
else
[402] Fix | Delete
@numerator.to_s+"/"+@denominator.to_s
[403] Fix | Delete
end
[404] Fix | Delete
end
[405] Fix | Delete
[406] Fix | Delete
#
[407] Fix | Delete
# Returns +self+.
[408] Fix | Delete
#
[409] Fix | Delete
def to_r
[410] Fix | Delete
self
[411] Fix | Delete
end
[412] Fix | Delete
[413] Fix | Delete
#
[414] Fix | Delete
# Returns a reconstructable string representation:
[415] Fix | Delete
#
[416] Fix | Delete
# Rational(5,8).inspect # -> "Rational(5, 8)"
[417] Fix | Delete
#
[418] Fix | Delete
def inspect
[419] Fix | Delete
sprintf("Rational(%s, %s)", @numerator.inspect, @denominator.inspect)
[420] Fix | Delete
end
[421] Fix | Delete
[422] Fix | Delete
#
[423] Fix | Delete
# Returns a hash code for the object.
[424] Fix | Delete
#
[425] Fix | Delete
def hash
[426] Fix | Delete
@numerator.hash ^ @denominator.hash
[427] Fix | Delete
end
[428] Fix | Delete
[429] Fix | Delete
attr :numerator
[430] Fix | Delete
attr :denominator
[431] Fix | Delete
[432] Fix | Delete
private :initialize
[433] Fix | Delete
end
[434] Fix | Delete
[435] Fix | Delete
class Integer
[436] Fix | Delete
#
[437] Fix | Delete
# In an integer, the value _is_ the numerator of its rational equivalent.
[438] Fix | Delete
# Therefore, this method returns +self+.
[439] Fix | Delete
#
[440] Fix | Delete
def numerator
[441] Fix | Delete
self
[442] Fix | Delete
end
[443] Fix | Delete
[444] Fix | Delete
#
[445] Fix | Delete
# In an integer, the denominator is 1. Therefore, this method returns 1.
[446] Fix | Delete
#
[447] Fix | Delete
def denominator
[448] Fix | Delete
1
[449] Fix | Delete
end
[450] Fix | Delete
[451] Fix | Delete
#
[452] Fix | Delete
# Returns a Rational representation of this integer.
[453] Fix | Delete
#
[454] Fix | Delete
def to_r
[455] Fix | Delete
Rational(self, 1)
[456] Fix | Delete
end
[457] Fix | Delete
[458] Fix | Delete
#
[459] Fix | Delete
# Returns the <em>greatest common denominator</em> of the two numbers (+self+
[460] Fix | Delete
# and +n+).
[461] Fix | Delete
#
[462] Fix | Delete
# Examples:
[463] Fix | Delete
# 72.gcd 168 # -> 24
[464] Fix | Delete
# 19.gcd 36 # -> 1
[465] Fix | Delete
#
[466] Fix | Delete
# The result is positive, no matter the sign of the arguments.
[467] Fix | Delete
#
[468] Fix | Delete
def gcd(other)
[469] Fix | Delete
min = self.abs
[470] Fix | Delete
max = other.abs
[471] Fix | Delete
while min > 0
[472] Fix | Delete
tmp = min
[473] Fix | Delete
min = max % min
[474] Fix | Delete
max = tmp
[475] Fix | Delete
end
[476] Fix | Delete
max
[477] Fix | Delete
end
[478] Fix | Delete
[479] Fix | Delete
#
[480] Fix | Delete
# Returns the <em>lowest common multiple</em> (LCM) of the two arguments
[481] Fix | Delete
# (+self+ and +other+).
[482] Fix | Delete
#
[483] Fix | Delete
# Examples:
[484] Fix | Delete
# 6.lcm 7 # -> 42
[485] Fix | Delete
# 6.lcm 9 # -> 18
[486] Fix | Delete
#
[487] Fix | Delete
def lcm(other)
[488] Fix | Delete
if self.zero? or other.zero?
[489] Fix | Delete
0
[490] Fix | Delete
else
[491] Fix | Delete
(self.div(self.gcd(other)) * other).abs
[492] Fix | Delete
end
[493] Fix | Delete
end
[494] Fix | Delete
[495] Fix | Delete
#
[496] Fix | Delete
# Returns the GCD _and_ the LCM (see #gcd and #lcm) of the two arguments
[497] Fix | Delete
# (+self+ and +other+). This is more efficient than calculating them
[498] Fix | Delete
# separately.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function