class Fraction

Public Class Methods

approximate_by_float(p1, pcnt=nil) click to toggle source
# File lib/primitive_wrapper.rb, line 728
def self.approximate_by_float(p1, pcnt=nil)
  return Fraction.new(p1.prim_value) if pcnt.nil?
  pcnt/= 100.0
  neg = p1.negative?
  p1=p1.abs
  if p1 >=1.0
    top = p1.floor
    bot = 1
  else
    top = 1
    bot = (1/p1).floor
  end
  loop do
    app = top.to_f/bot.to_f
    tst = (app - p1).abs/p1
    break if tst < pcnt
    if app > p1
      bot += 1
    else
      top += 1
    end        
  end
  frac = Fraction.new(top.prim_value,bot.prim_value)
  frac = -frac if neg
  return frac
end
new(*prms) click to toggle source
# File lib/primitive_wrapper.rb, line 755
def initialize(*prms)
  #obj = obj.prim_value
  #ensure_valid(obj)
  #@value = obj
  if 1==prms.count
    @value = Rational(prms[0].prim_value)
  elsif 2==prms.count
    if prms[0].type_of? Float
      if prms[1].type_of? Integer
        sign = prms[0].negative? ? -1:1
        num = prms[0].abs
        whole = num.floor
        frac = num-whole
        num = (frac * prms[1]).round
        frac = Fraction.new(whole, num, prms[1]) * sign
        @value = frac.prim_value
      else
        fract = self.class.approximate_by_float(prms[0].prim_value,prms[1].prim_value)
        @value = fract.prim_value
      end
    else
      @value = Rational(prms[0].prim_value,prms[1].prim_value)
    end
    
  elsif 3==prms.count
    @value = Rational(prms[1].prim_value,prms[2].prim_value) + prms[0].prim_value
  else
    raise("expected 1..3 parameters")
  end
end

Public Instance Methods

%(other) click to toggle source
# File lib/primitive_wrapper.rb, line 816
def %(other)
  Fraction.new(@value % other)
end
*(other) click to toggle source
# File lib/primitive_wrapper.rb, line 808
def *(other)
  Fraction.new(@value * other)
end
**(other) click to toggle source
# File lib/primitive_wrapper.rb, line 812
def **(other)
  Fraction.new(@value ** other)
end
+(other) click to toggle source
# File lib/primitive_wrapper.rb, line 800
def +(other)
  Fraction.new(@value + other)
end
+@() click to toggle source
# File lib/primitive_wrapper.rb, line 824
def +@
  Fraction.new(@value)
end
-(other) click to toggle source
# File lib/primitive_wrapper.rb, line 804
def -(other)
  Fraction.new(@value - other)
end
-@() click to toggle source
# File lib/primitive_wrapper.rb, line 828
def -@
  Fraction.new(-@value)
end
/(other) click to toggle source
# File lib/primitive_wrapper.rb, line 820
def /(other)
  Fraction.new(@value / other)
end
abs!() click to toggle source
# File lib/primitive_wrapper.rb, line 790
def abs!
  @value = @value.abs
  self
end
coerce(other) click to toggle source
# File lib/primitive_wrapper.rb, line 724
def coerce(other)
  [Fraction.new(other.prim_value), self.prim_value]
end
negate!() click to toggle source
# File lib/primitive_wrapper.rb, line 795
def negate!
  @value = -@value.abs
  self
end
replace(other) click to toggle source
# File lib/primitive_wrapper.rb, line 786
def replace(other)    
  @value = Rational(other.prim_value)
end
to_fraction() click to toggle source
# File lib/primitive_wrapper.rb, line 832
def to_fraction
  self
end
to_s(paren=true) click to toggle source
# File lib/primitive_wrapper.rb, line 836
def to_s(paren=true)
  return @value.numerator if @value.denominator==1
  if @value.numerator.abs < @value.denominator
    if paren
      return "(#{@value.numerator}/#{@value.denominator})"
    else
      return "#{@value.numerator}/#{@value.denominator}"
    end    
  end    
  if @value.negative?
    rat = -@value
    whole, rem = rat.numerator.divmod rat.denominator
    if paren
      return "(-#{whole} -#{rem}/#{@value.denominator})"
    else
      return "-#{whole} -#{rem}/#{@value.denominator}"
    end
    
  end
  whole, rem = @value.numerator.divmod @value.denominator
  if paren
    return "(#{whole} #{rem}/#{@value.denominator})"
  else
    return "#{whole} #{rem}/#{@value.denominator}"
  end
end
valid_type(prm) click to toggle source
# File lib/primitive_wrapper.rb, line 863
def valid_type(prm)
  return true
end