class Decimal

Attributes

absolute[RW]
decimalExponent[RW]
decimalfraction[RW]
decimalfraction_1[RW]
integraldecimal[RW]
integraldecimal_1[RW]
patt_succ[RW]
precision[RW]
repetend[RW]
repetend_1[RW]

Public Class Methods

decimalSeparator() click to toggle source
# File lib/m500.rb, line 1863
def Decimal::decimalSeparator
  @@decimalSeparator
end
new!(a,b,c=0,d=0,e=1) click to toggle source
# File lib/m500.rb, line 1854
def Decimal.new!(a,b,c=0,d=0,e=1)
  new(a,b,c,d,e)
end
precision() click to toggle source
# File lib/m500.rb, line 1866
def Decimal::precision
  @@precision
end
precision=(a) click to toggle source
# File lib/m500.rb, line 1869
def Decimal::precision=(a)
  if a.class == Integer then
    @@precision = a 
  else
    @@precision = a.to_i
  end
end

Private Class Methods

new(a,b,c='0',d=0,e=1) click to toggle source
# File lib/m500.rb, line 1877
def initialize(a,b,c='0',d=0,e=1)
  @integraldecimal = a #integraldecimal1 is the equivalent representation
  @decimalfraction = b
  @integraldecimal_1 = a
  @repetend = Proc.new{c}
  @decimalExponent = d
  @absolute = e
  @to_Frac
  if a == 0 and b.nil? and c == 0 and d == 0 then
    @repetend_1= @repetend
    @decimalfraction_1 = @decimalfraction
  else
    if @repetend.call =="0" then
      @repetend_1= Proc.new{"9"}
      @decimalfraction_1 = b-1 unless b.nil?
      @integraldecimal_1 = a-1 if b.nil?
    elsif @repetend.call =="9" then
      @repetend_1= Proc.new{"0"}
      @decimalfraction_1 = b+1 unless b.nil?
      @integraldecimal_1 = a+1 if b.nil?
    else
      @decimalfraction_1 = b
    end
  end
  @setdelta = false
  @deltaIter = 10
end

Public Instance Methods

*(a) click to toggle source
# File lib/m500.rb, line 2080
def * (a)
  if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
    naught
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    infinity
  else
    ((self.to_Q) * (a.to_Q)).to_Dec
  end
end
**(a) click to toggle source
# File lib/m500.rb, line 2092
def ** (a)
  self.to_f ** a.to_f
end
+(a) click to toggle source
# File lib/m500.rb, line 1933
def +(a)
  if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
    naught
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    infinity
  else
    ((self.to_Q) + (a.to_Q)).to_Dec
  end
end
-(a) click to toggle source
# File lib/m500.rb, line 1942
def -(a)
  if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
    naught
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    infinity
    else
    ((self.to_Q) - (a.to_Q)).to_Dec
  end
end
/(a) click to toggle source
# File lib/m500.rb, line 2089
def / (a)
  ((self.to_Q) /(a.to_Q)).to_Dec
end
<=>(other) click to toggle source
# File lib/m500.rb, line 2095
def <=>(other)
  @@delta = Decimal((other - self) /@deltaIter) if @setdelta
  @setdelta = false
  ret = nil
  if other.kind_of?(Decimal)
    if self == other then
      ret = 0
    elsif self.integraldecimal < other.integraldecimal then
      ret = -1
    elsif self.integraldecimal > other.integraldecimal then
      ret = 1
    elsif  self.integraldecimal == other.integraldecimal then
      s = self.to_s
      t = other.to_s
      k = s.length
      l = t.length
      if k>l then
        m = k-l
        eval("#{m}.times{other.patt_succ(t)}")
      elsif k<l then
        m = l-k
        eval("#{m}.times{self.patt_succ(s)}")
      end
       s.gsub("#{@@decimalSeparator}","") <=> t.gsub("#{@@decimalSeparator}","")
    end
  elsif other.kind_of?(Numeric)
    ret = self <=> other.to_Dec
  end
  ret
end
==(other) click to toggle source
# File lib/m500.rb, line 2125
def == (other)
  if other.kind_of?(Decimal) then
    (self.integraldecimal == other.integraldecimal or
      self.integraldecimal == other.integraldecimal_1
     ) and(
           ( self.repetend.call == other.repetend.call and
             self.decimalfraction == other.decimalfraction
             ) or (self.decimalfraction_1 == other.decimalfraction
                   ) or (self.decimalfraction == other.decimalfraction_1
                         )
           )
  elsif other.kind_of?(Float) or other.kind_of?(Quotient)  or other.kind_of?(Fraction) 
    self == other.to_Dec
  else
    other.to_i == self.to_i
  end
end
coerce(other) click to toggle source
# File lib/m500.rb, line 2215
def coerce(other)
  if Natural === other or Counting === other or Zahlen === other
    [Decimal(other.to_i),self]
  elsif Integer === other
    [Zahlen(other),self]
  elsif Quotient === other
    [other,self.to_Q]
  else
    [Float(other),self.to_f]
  end
end
d2Frac() click to toggle source
# File lib/m500.rb, line 2170
def d2Frac
  t0 = Quotient(0,1)
  s = @decimalfraction.to_s.length > 0 ? @decimalfraction.to_s.length - 1 : 0
  s == 0 ? t0 = Quotient(@decimalfraction.nil? ? 0 : @decimalfraction,("1" + ("0"*@decimalExponent)).to_i) : t0 = Quotient(@decimalfraction.nil? ? 0 : @decimalfraction,("1" +("0"* (@decimalfraction.to_s.length)) + ("0"*@decimalExponent)).to_i)
  t1 = Quotient(Zahlen(@repetend.call.to_i),Zahlen((("9"*@repetend.call.length)+("0"* s)+("0"*@decimalExponent)).to_i))
  Fraction(Zahlen(@integraldecimal),(t0 + Fraction(Zahlen(0),t1)),@absolute)
end
inspect() click to toggle source
# File lib/m500.rb, line 2206
def inspect
  t0 = ""
  t0 = "0"*(@decimalExponent - 1) if @decimalExponent>0
  if @absolute == -1 then
    sprintf("Decimal(\"-%s%s%s[%s]\")", @integraldecimal.to_s, @@decimalSeparator,t0 + @decimalfraction.to_s , @repetend.call.to_s)
  else
    sprintf("Decimal(\"%s%s%s[%s]\")", @integraldecimal.to_s, @@decimalSeparator,t0 + @decimalfraction.to_s , @repetend.call.to_s)
  end
end
isRE?(a,re) click to toggle source
# File lib/m500.rb, line 1926
def isRE?(a,re)
  if a =~ re  
    true
  else  
    false
  end  
end
is_0?() click to toggle source
# File lib/m500.rb, line 2155
def is_0?
  @integraldecimal === 0 and @decimalfraction.nil? and  @repetend.call == "0" and   @decimalExponent ==0 ? true : false
end
minus(a) click to toggle source
# File lib/m500.rb, line 2012
def minus(a)
  ret = nil
  pat = nil
  if a.kind_of?(Decimal)
    s0 = ""
    s0 = "0"*a.decimalExponent if a.decimalExponent>0
    s = "#{s0}#{a.decimalfraction}"
    t0 = ""
    t0 = "0"*@decimalExponent if @decimalExponent>0
    t = "#{t0}#{@decimalfraction}"
    if s.length > t.length then
      m = s.length+t.length+1
      if m < self.repetend.call.length then
        t << self.repetend.call.slice(self.repetend.call.length - m)
      elsif m >= self.repetend.call.length
        t << self.repetend.call
        t = eval("#{self.repetend.call.length-m}.times{self.patt_succ(#{t})}")
      end
    elsif s.length < t.length then
      m = t.length-s.length
      if m < a.repetend.call.length then
        t << a.repetend.call.slice(a.repetend.call.length - m)
      elsif m >= a.repetend.call.length
        t << a.repetend.call
        t0 = t.length
        t = eval("#{t0-m}.times{a.patt_succ('0#{@@decimalSeparator}#{t}')}")
      end
    else
      p "raise an interesting and impossible error" unless s.length == t.length
    end
    m = a.repetend.call
    n = self.repetend.call 
    k=m.length
    l=n.length
    (l-1).times{m = m + a.repetend.call }
    (k-1).times{n = n +  self.repetend.call}
    minus = 1
    if self.integraldecimal < a.integraldecimal then
      minus = -1
      pat = m.to_i-n.to_i
      ret = "[" + pat.to_s + "]" unless pat.nil?
      ret = (a.integraldecimal-self.integraldecimal).to_s + @@decimalSeparator + (s.to_i - t.to_i).to_s + ret
    elsif self.integraldecimal > a.integraldecimal then
      pat = n.to_i-m.to_i
      ret = "[" + pat.to_s + "]" unless pat.nil?
      ret = (self.integraldecimal-a.integraldecimal).to_s + @@decimalSeparator + (t.to_i - s.to_i).to_s + ret
    else
      if "1#{t}".to_i < "1#{s}".to_i then
        minus = -1
        pat = n.to_i-m.to_i
        ret = "[" + pat.to_s + "]" unless pat.nil?
        ret = (a.integraldecimal-self.integraldecimal).to_s + @@decimalSeparator  + (s.to_i - t.to_i).to_s + ret
      elsif "1#{t}#{n}".to_i > "1#{s}#{m}".to_i
        pat = m.to_i-n.to_i
        ret = "[" + pat.to_s + "]" unless pat.nil?
        ret = (self.integraldecimal-a.integraldecimal).to_s + @@decimalSeparator  + (t.to_i - s.to_i).to_s + ret
      else
      end 
    end
    ret = ret.to_Dec
    ret.absolute = -1 if minus == -1
  elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
    ret = naught
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    ret = infinity
  end
  ret
end
plus(a) click to toggle source
# File lib/m500.rb, line 1951
def plus(a) #forces Decimal class addition working
  ret = nil
  pat = nil
  if a.kind_of?(Decimal)
    if (self.absolute == 1 and a.absolute == 1) then
      s0 = ""
      s0 = "0"*a.decimalExponent if a.decimalExponent>0
      s = "#{s0}#{a.decimalfraction}"
      t0 = ""
      t0 = "0"*@decimalExponent if @decimalExponent>0
      t = "#{t0}#{@decimalfraction}"
      if s.length > t.length then
        m = s.length-t.length
        if m < self.repetend.call.length then
          t << self.repetend.call.slice(a.repetend.call.length - m)
        elsif m >= self.repetend.call.length
          t << self.repetend.call
          t = eval("#{m-self.repetend.call.length}.times{self.patt_succ(#{t})}")
        end
      elsif s.length < t.length then
        m = t.length-s.length
        if m < a.repetend.call.length then
          t << a.repetend.call.slice(a.repetend.call.length - m)
        elsif m >= a.repetend.call.length
          t << a.repetend.call
          t = eval("#{a.repetend.call.length-m}.times{a.patt_succ(#{t})}")
        end
      else
        p "raise an interesting and impossible error" unless s.length == t.length
      end
      m = a.repetend.call
      n = self.repetend.call 
      k=m.length
      l=n.length
      (l-1).times{m = m + a.repetend.call }
      (k-1).times{n = n +  self.repetend.call}
      pat = m.to_i+n.to_i
      pat = pat.to_s
      v = "1#{t}".to_i + "1#{s}".to_i
      v = v.to_s[0].to_i - 2
      u = (self.integraldecimal + a.integraldecimal + v).to_s.length
      ret = ("#{self.integraldecimal}#{t}".to_i + "#{a.integraldecimal}#{s}".to_i).to_s
      ret = ret[0,u] + @@decimalSeparator + ret[u,ret.length]
      ret = ret + "[" + pat + "]" unless pat.nil?
    elsif self.absolute == -1 and a.absolute == -1 then
      t = self
      t.absolute = 1
      ret = (t + a)
      ret.absolute = -1
    elsif self.absolute == -1 and a.absolute == 1 then
      ret = a - self
    elsif self.absolute == 1 and a.absolute == -1 then
      ret = self - a
    end
  elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
    self
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    infinity
  end
  ret.to_Dec
end
setdelta() click to toggle source
# File lib/m500.rb, line 1907
def setdelta
  @setdelta = true
end
showRE(a,re) click to toggle source
# File lib/m500.rb, line 1919
def showRE(a,re)  
  if a =~ re  
    "#{$`}<<#{$&}>>#{$'}"  
  else  
    "no match"  
  end  
end
succ() click to toggle source
# File lib/m500.rb, line 2142
def succ
  self + @@delta
end
to_Frac() click to toggle source
# File lib/m500.rb, line 2177
def to_Frac
  @to_Frac ||= d2Frac
end
to_K() click to toggle source
# File lib/m500.rb, line 2186
def to_K
  Kettenbruch(self.to_Frac)
end
to_N() click to toggle source
# File lib/m500.rb, line 2161
def to_N
  @decimalfraction.nil? and  @repetend.call == "0" and   @decimalExponent ==0 ? Natural(@integraldecimal) : emptySet
end
to_N0() click to toggle source
# File lib/m500.rb, line 2164
def to_N0
  @decimalfraction.nil? and  @repetend.call == "0" and   @decimalExponent ==0 ? Counting(@integraldecimal) : emptySet
end
to_Q() click to toggle source
# File lib/m500.rb, line 2180
def to_Q
  self.to_Frac.to_Q
end
to_R() click to toggle source
# File lib/m500.rb, line 2189
def to_R
  self.to_Frac
end
to_Sig() click to toggle source
# File lib/m500.rb, line 2183
def to_Sig
  Sigma(self.to_Q)
end
to_Z() click to toggle source
# File lib/m500.rb, line 2167
def to_Z
  @decimalfraction.nil? and  @repetend.call == "0" and   @decimalExponent ==0 ? Zahlen(@integraldecimal) : emptySet
end
to_digits() click to toggle source
# File lib/m500.rb, line 1910
def to_digits
   if self.nan? || self.infinite? || self.zero?
     self.to_s
   else
     i       = self.to_i.to_s
     s,f,y,z = self.frac.split
     i + "." + ("0"*(-z)) + f
   end
end
to_f() click to toggle source
# File lib/m500.rb, line 2158
def to_f
  self.to_s.to_f
end
to_s() click to toggle source
# File lib/m500.rb, line 2197
def to_s
  e = ""
  e = "-" if @absolute == -1
  t0 = ""
  t0 = "0"*(@decimalExponent - 1) if @decimalExponent>0
  t = "#{e}#{@integraldecimal}#{@@decimalSeparator}#{t0}#{@decimalfraction}#{@repetend.call}"
  instance_eval("#{@@precision}.times{patt_succ(t)}")
  return t
end
to_s!() click to toggle source
# File lib/m500.rb, line 2192
def to_s!
  #    t0 = ""
  #    t0 = "0"*(@decimalExponent - 1) if @decimalExponent>0
  #    print "Decimal(\"-%s%s%s[%s]\")", @integraldecimal.to_s, @@decimalSeparator,t0 + @decimalfraction.to_s , @repetend.call.to_s
end
to_sgml() click to toggle source
# File lib/m500.rb, line 1860
def to_sgml
  "<mn #{sgml_id}class='decimal'>#{self.to_s}</mn>"
end