Class: Fraction
Overview
Constant Summary
collapse
- @@fractionSeparator =
" "
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from SGML
#sgml_id, #tog_sgml_id
Methods inherited from Numeric
#irrational?, #rational?, #sgml_id, #to_Frac, #to_N, #to_N0, #to_R, #to_Z
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
2202
2203
2204
|
# File 'lib/m500.rb', line 2202
def a
@a
end
|
#absolute ⇒ Object
Returns the value of attribute absolute.
2202
2203
2204
|
# File 'lib/m500.rb', line 2202
def absolute
@absolute
end
|
#b ⇒ Object
Returns the value of attribute b.
2202
2203
2204
|
# File 'lib/m500.rb', line 2202
def b
@b
end
|
#integer ⇒ Object
Returns the value of attribute integer.
2202
2203
2204
|
# File 'lib/m500.rb', line 2202
def integer
@integer
end
|
#properfraction ⇒ Object
Returns the value of attribute properfraction.
2202
2203
2204
|
# File 'lib/m500.rb', line 2202
def properfraction
@properfraction
end
|
Class Method Details
.fractionSeparator ⇒ Object
2148
2149
2150
|
# File 'lib/m500.rb', line 2148
def Fraction::fractionSeparator
@@fractioinSeparator
end
|
.new!(a, b = Quotient(0,1), c = 1) ⇒ Object
2140
2141
2142
2143
2144
2145
2146
|
# File 'lib/m500.rb', line 2140
def Fraction.new!(a, b = Quotient(0,1),c=1)
if a.kind_of?(Array)
new(a,"canonical form")
else
new(a, b,c)
end
end
|
Instance Method Details
#*(a) ⇒ Object
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
|
# File 'lib/m500.rb', line 2275
def * (a)
if a.kind_of?(Fraction)
(self.to_Q*a.to_Q).to_Frac
elsif a.kind_of?(Quotient)
num = @properfraction.numerator * a.numerator
den = @properfraction.denominator * a.denominator
Fraction(num, den)
elsif a.kind_of?(Integer) or a.kind_of?(Fixnum)
if a == 0 then
0
elsif a == 1
self
else
self * Fraction(Zahlen(a), Quotient(0,1))
end
elsif a.kind_of?(Float)
self.to_f * a
elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
naught
elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
infinity
else
x, y = a.coerce(self)
(x * y).to_Frac
end
end
|
#+(a) ⇒ Object
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
|
# File 'lib/m500.rb', line 2205
def + (a)
if a.kind_of?(Fraction)
if a.integer == 0 and a.properfraction == Quotient(0,1) then
return self
else
(self.to_Q + a.to_Q).to_Frac
end
elsif a.kind_of?(Quotient)
if a.numerator == 0 and a == Quotient(0,1) then
return self
else
(self.to_Q + a).to_Frac
end
else
(self.to_Q + a.to_Q).to_Frac
end
end
|
#-(a) ⇒ Object
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
|
# File 'lib/m500.rb', line 2258
def - (a)
if a.kind_of?(Fraction)
if a.integer == 0 and a.properfraction == Quotient(0,1) then
return self
else
(self.to_Q - a.to_Q).to_Frac
end
elsif a.kind_of?(Quotient)
if a.numerator == 0 and a == Quotient(0,1) then
return self
else
(self.to_Q - a).to_Frac
end
else
(self.to_Q - a.to_Q).to_Frac
end
end
|
#/(a) ⇒ Object
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
|
# File 'lib/m500.rb', line 2301
def / (a)
if a.kind_of?(Fraction)
self/a.to_Q
elsif a.kind_of?(Quotient)
num = @properfraction.numerator * a.denominator
den = @properfraction.denominator * a.numerator
Fraction(num, den) unless a.numerator == 0
nan if a.numerator == 0
elsif a.kind_of?(Integer) or a.kind_of?(Fixnum)
self * Fraction.new!(1,a)
elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
nan
elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
nan
else
x, y = a.coerce(self)
(x / y).to_Frac
end
end
|
#<=>(other) ⇒ Object
2378
2379
2380
|
# File 'lib/m500.rb', line 2378
def <=>(other)
self.to_Q <=>other.to_Q
end
|
#addition(a) ⇒ Object
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
|
# File 'lib/m500.rb', line 2222
def addition (a)
if a.kind_of?(Fraction)
if a.integer == 0 and a.properfraction == Quotient(0,1) then
return self
else
abs = 0
temp = Quotient(@absolute *@properfraction.numerator,@properfraction.denominator) + Quotient(a.absolute * a.properfraction.numerator, a.properfraction.denominator)
temp2 = Fraction(temp.numerator,temp.denominator)
pfrac = temp2.properfraction
int = (@absolute*@integer) + (a.absolute*a.integer) + (temp2.integer)
if int.abs>1 and int>0
abs = 1
elsif int.abs>1 and int<0
abs= -1
else
abs = (@absolute * a.absolute)
end
Fraction.new!([abs,int.abs.to_i,pfrac],"canonical form")
end
elsif a.kind_of?(Quotient)
num = @properfraction.numerator * a.denominator
num_a = a.numerator * @properfraction.denominator
Fraction(num + num_a, @properfraction.denominator * a.denominator)
elsif a.kind_of?(Integer)or a.kind_of?(Fixnum)
a == 0 ? self: self + Fraction.new!(a, Quotient(0,1))
elsif a.kind_of?(Float)
Float(self) + a
elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
self
elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
infinity
else
x, y = a.coerce(self)
(x + y).to_Frac
end
end
|
#coerce(other) ⇒ Object
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
|
# File 'lib/m500.rb', line 2320
def coerce(other)
if Natural === other or Counting === other or Zahlen === other
[Quotient(other),self]
elsif Integer === other
[Zahlen(other),self]
elsif Quotient === other
[other,self.to_Q]
else
[Float(other),self.to_f]
end
end
|
#hash ⇒ Object
2381
2382
2383
|
# File 'lib/m500.rb', line 2381
def hash
@absolute.hash ^ @integer.hash ^ @properfraction.hash
end
|
#inspect ⇒ Object
2365
2366
2367
2368
2369
2370
2371
|
# File 'lib/m500.rb', line 2365
def inspect
if @absolute == 1
sprintf("Fraction(\"%s %s/%s\")",(@integer).to_s,@properfraction.numerator, @properfraction.denominator)
elsif @absolute == -1
sprintf("Fraction(\"-1(%s %s/%s)\")",(@integer).to_s,@properfraction.numerator, @properfraction.denominator)
end
end
|
#is_0? ⇒ Boolean
2331
2332
2333
|
# File 'lib/m500.rb', line 2331
def is_0?
@integer === 0 and @properfraction.is_0? ? true : false
end
|
#setdelta ⇒ Object
2372
2373
2374
|
# File 'lib/m500.rb', line 2372
def setdelta
self.to_Q.setdelta
end
|
#succ ⇒ Object
2375
2376
2377
|
# File 'lib/m500.rb', line 2375
def succ
self.to_Q.succ
end
|
#to_Dec ⇒ Object
2337
2338
2339
|
# File 'lib/m500.rb', line 2337
def to_Dec
self.to_Q.to_Dec
end
|
#to_f ⇒ Object
2340
2341
2342
|
# File 'lib/m500.rb', line 2340
def to_f
self.to_Q.to_f
end
|
#to_K ⇒ Object
2346
2347
2348
|
# File 'lib/m500.rb', line 2346
def to_K
Kettenbruch(self)
end
|
#to_Q ⇒ Object
2334
2335
2336
|
# File 'lib/m500.rb', line 2334
def to_Q
@to_Q ||= Quotient(@absolute*@integer,1) + (@properfraction*@absolute)
end
|
#to_s ⇒ Object
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
|
# File 'lib/m500.rb', line 2349
def to_s
t = ""
unless a.nil? or b.nil?
t = "#{@a.to_s}/#{@b.to_s} = "
end
if @b == 1
@a.to_s
elsif @absolute <0
"#{t}#{@absolute}*(#{@integer} + (#{@properfraction}))"
else
"#{t}#{@integer.to_s} + (#{@properfraction.to_s})"
end
end
|
#to_sgml ⇒ Object
2152
2153
2154
|
# File 'lib/m500.rb', line 2152
def to_sgml
"<mn #{sgml_id}class='mixed_fraction'><mn>#{@integer.to_s}</mn>#{@properfraction.to_sgml}<mn>"
end
|
#to_Sig ⇒ Object
2343
2344
2345
|
# File 'lib/m500.rb', line 2343
def to_Sig
Sigma(self.to_Q)
end
|