class Fraction

Attributes

a[RW]
absolute[RW]
b[RW]
integer[RW]
properfraction[RW]

Public Class Methods

fractionSeparator() click to toggle source
# File lib/m500.rb, line 2236
def Fraction::fractionSeparator
  @@fractioinSeparator
end
new!(a, b = Quotient(0,1),c=1) click to toggle source
# File lib/m500.rb, line 2228
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

Private Class Methods

new(a, b,c=1) click to toggle source
# File lib/m500.rb, line 2243
def initialize(a, b,c=1)
  if a.kind_of?(Fraction)
    a
  elsif a.kind_of?(Zahlen) and b.kind_of?(Natural) 
    @a = a
    @b = b
    t = Quotient(a,b)
    @absolute = -1 * c       if t < 0 
    @absolute = c       if t >= 0 
    @integer = t.numerator.divmod(t.denominator).at(0)
    @properfraction = Quotient(t.numerator.divmod(t.denominator).at(1),t.denominator)
  elsif a.kind_of?(Zahlen) and b.kind_of?(Quotient) and (c ==1 or c ==-1)
    @a = nil
    @b = nil
    @absolute = c
    @integer = a + b.numerator.divmod(b.denominator).at(0)
    @properfraction = Quotient(b.numerator.divmod(b.denominator).at(1),b.denominator)
  elsif a.kind_of?(Numeric) and b.kind_of?(Quotient)
    a = a.to_Z
    if a>0
      @a = nil
      @b = nil
      @absolute = 1
      @integer = a + b.numerator.divmod(b.denominator).at(0)
      @properfraction = Quotient(b.numerator.divmod(b.denominator).at(1),b.denominator)
    elsif a<0
      a = a*(-1)
      @a = nil
      @b = nil
      @absolute = -1
      @integer = a + b.numerator.divmod(b.denominator).at(0)
      @properfraction = Quotient(b.numerator.divmod(b.denominator).at(1),b.denominator)
    else
    end
  elsif a.kind_of?(Array)and b =="canonical form"
    @a = nil
    @b = nil
    @absolute = a.at(0)
    @integer = a.at(1)
    @properfraction = a.at(2)
  elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
    Fraction(Zahlen(0),Quotient(b))
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    infinity
  end
  @to_Q
end

Public Instance Methods

*(a) click to toggle source
# File lib/m500.rb, line 2363
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) click to toggle source
# File lib/m500.rb, line 2293
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) click to toggle source
# File lib/m500.rb, line 2346
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) click to toggle source
# File lib/m500.rb, line 2389
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) click to toggle source
# File lib/m500.rb, line 2466
def <=>(other)
  self.to_Q <=>other.to_Q
end
addition(a) click to toggle source
# File lib/m500.rb, line 2310
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) click to toggle source
# File lib/m500.rb, line 2408
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() click to toggle source
# File lib/m500.rb, line 2469
def hash
  @absolute.hash ^ @integer.hash ^ @properfraction.hash
end
inspect() click to toggle source
# File lib/m500.rb, line 2453
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?() click to toggle source
# File lib/m500.rb, line 2419
def is_0?
  @integer === 0 and @properfraction.is_0? ? true : false
end
setdelta() click to toggle source
# File lib/m500.rb, line 2460
def setdelta
  self.to_Q.setdelta
end
succ() click to toggle source
# File lib/m500.rb, line 2463
def succ
  self.to_Q.succ
end
to_Dec() click to toggle source
# File lib/m500.rb, line 2425
def to_Dec
  self.to_Q.to_Dec
end
to_K() click to toggle source
# File lib/m500.rb, line 2434
def to_K
  Kettenbruch(self)
end
to_Q() click to toggle source
# File lib/m500.rb, line 2422
def to_Q
  @to_Q ||= Quotient(@absolute*@integer,1) + (@properfraction*@absolute)
end
to_Sig() click to toggle source
# File lib/m500.rb, line 2431
def to_Sig
  Sigma(self.to_Q)
end
to_f() click to toggle source
# File lib/m500.rb, line 2428
def to_f
  self.to_Q.to_f
end
to_s() click to toggle source
# File lib/m500.rb, line 2437
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() click to toggle source
# File lib/m500.rb, line 2240
def to_sgml
  "<mn #{sgml_id}class='mixed_fraction'><mn>#{@integer.to_s}</mn>#{@properfraction.to_sgml}<mn>"
end