Class: Fraction

Inherits:
Numeric show all
Includes:
SGML
Defined in:
lib/m500.rb

Overview

Mixed fraction

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

#aObject

Returns the value of attribute a.



2271
2272
2273
# File 'lib/m500.rb', line 2271

def a
  @a
end

#absoluteObject

Returns the value of attribute absolute.



2271
2272
2273
# File 'lib/m500.rb', line 2271

def absolute
  @absolute
end

#bObject

Returns the value of attribute b.



2271
2272
2273
# File 'lib/m500.rb', line 2271

def b
  @b
end

#integerObject

Returns the value of attribute integer.



2271
2272
2273
# File 'lib/m500.rb', line 2271

def integer
  @integer
end

#properfractionObject

Returns the value of attribute properfraction.



2271
2272
2273
# File 'lib/m500.rb', line 2271

def properfraction
  @properfraction
end

Class Method Details

.fractionSeparatorObject



2217
2218
2219
# File 'lib/m500.rb', line 2217

def Fraction::fractionSeparator
  @@fractioinSeparator
end

.new!(a, b = Quotient(0,1), c = 1) ⇒ Object



2209
2210
2211
2212
2213
2214
2215
# File 'lib/m500.rb', line 2209

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



2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
# File 'lib/m500.rb', line 2344

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



2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
# File 'lib/m500.rb', line 2274

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



2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
# File 'lib/m500.rb', line 2327

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



2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
# File 'lib/m500.rb', line 2370

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



2447
2448
2449
# File 'lib/m500.rb', line 2447

def <=>(other)
  self.to_Q <=>other.to_Q
end

#addition(a) ⇒ Object



2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
# File 'lib/m500.rb', line 2291

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



2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
# File 'lib/m500.rb', line 2389

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

#hashObject



2450
2451
2452
# File 'lib/m500.rb', line 2450

def hash
  @absolute.hash ^ @integer.hash ^ @properfraction.hash
end

#inspectObject



2434
2435
2436
2437
2438
2439
2440
# File 'lib/m500.rb', line 2434

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



2400
2401
2402
# File 'lib/m500.rb', line 2400

def is_0?
  @integer === 0 and @properfraction.is_0? ? true : false
end

#setdeltaObject



2441
2442
2443
# File 'lib/m500.rb', line 2441

def setdelta
  self.to_Q.setdelta
end

#succObject



2444
2445
2446
# File 'lib/m500.rb', line 2444

def succ
  self.to_Q.succ
end

#to_DecObject



2406
2407
2408
# File 'lib/m500.rb', line 2406

def to_Dec
  self.to_Q.to_Dec
end

#to_fObject



2409
2410
2411
# File 'lib/m500.rb', line 2409

def to_f
  self.to_Q.to_f
end

#to_KObject



2415
2416
2417
# File 'lib/m500.rb', line 2415

def to_K
  Kettenbruch(self)
end

#to_QObject



2403
2404
2405
# File 'lib/m500.rb', line 2403

def to_Q
  @to_Q ||= Quotient(@absolute*@integer,1) + (@properfraction*@absolute)
end

#to_sObject



2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
# File 'lib/m500.rb', line 2418

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_sgmlObject



2221
2222
2223
# File 'lib/m500.rb', line 2221

def to_sgml
  "<mn #{sgml_id}class='mixed_fraction'><mn>#{@integer.to_s}</mn>#{@properfraction.to_sgml}<mn>"
end

#to_SigObject



2412
2413
2414
# File 'lib/m500.rb', line 2412

def to_Sig
  Sigma(self.to_Q)
end