Class: Fraction

Inherits:
ValueAdd show all
Defined in:
lib/primitive_wrapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ValueAdd

bestow_methods, capture_base_methods

Methods inherited from Value

#!=, #==, #ensure_valid, #freeze, freeze_raise?, ignore_on_freeze, #inspect, #prim_value, raise_on_freeze, #to_wrapper, #type, #type_of?, #unwrap, #val, #val=, #wrapped?, #~

Constructor Details

#initialize(*prms) ⇒ Fraction

Returns a new instance of Fraction.



755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# 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

Class Method Details

.approximate_by_float(p1, pcnt = nil) ⇒ Object



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# 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

Instance Method Details

#%(other) ⇒ Object



816
817
818
# File 'lib/primitive_wrapper.rb', line 816

def %(other)
  Fraction.new(@value % other)
end

#*(other) ⇒ Object



808
809
810
# File 'lib/primitive_wrapper.rb', line 808

def *(other)
  Fraction.new(@value * other)
end

#**(other) ⇒ Object



812
813
814
# File 'lib/primitive_wrapper.rb', line 812

def **(other)
  Fraction.new(@value ** other)
end

#+(other) ⇒ Object



800
801
802
# File 'lib/primitive_wrapper.rb', line 800

def +(other)
  Fraction.new(@value + other)
end

#+@Object



824
825
826
# File 'lib/primitive_wrapper.rb', line 824

def +@
  Fraction.new(@value)
end

#-(other) ⇒ Object



804
805
806
# File 'lib/primitive_wrapper.rb', line 804

def -(other)
  Fraction.new(@value - other)
end

#-@Object



828
829
830
# File 'lib/primitive_wrapper.rb', line 828

def -@
  Fraction.new(-@value)
end

#/(other) ⇒ Object



820
821
822
# File 'lib/primitive_wrapper.rb', line 820

def /(other)
  Fraction.new(@value / other)
end

#abs!Object



790
791
792
793
# File 'lib/primitive_wrapper.rb', line 790

def abs!
  @value = @value.abs
  self
end

#coerce(other) ⇒ Object



724
725
726
# File 'lib/primitive_wrapper.rb', line 724

def coerce(other)
  [Fraction.new(other.prim_value), self.prim_value]
end

#negate!Object



795
796
797
798
# File 'lib/primitive_wrapper.rb', line 795

def negate!
  @value = -@value.abs
  self
end

#replace(other) ⇒ Object



786
787
788
# File 'lib/primitive_wrapper.rb', line 786

def replace(other)    
  @value = Rational(other.prim_value)
end

#to_fractionObject



832
833
834
# File 'lib/primitive_wrapper.rb', line 832

def to_fraction
  self
end

#to_s(paren = true) ⇒ Object



836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# 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) ⇒ Object



863
864
865
# File 'lib/primitive_wrapper.rb', line 863

def valid_type(prm)
  return true
end