Class: Decimal

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

Constant Summary collapse

@@decimalSeparator =

,

"."
@@precision =
100
@@delta =
0

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_Dec

Instance Attribute Details

#absoluteObject

Returns the value of attribute absolute.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def absolute
  @absolute
end

#decimalExponentObject

Returns the value of attribute decimalExponent.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def decimalExponent
  @decimalExponent
end

#decimalfractionObject

Returns the value of attribute decimalfraction.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def decimalfraction
  @decimalfraction
end

#decimalfraction_1Object

Returns the value of attribute decimalfraction_1.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def decimalfraction_1
  @decimalfraction_1
end

#integraldecimalObject

Returns the value of attribute integraldecimal.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def integraldecimal
  @integraldecimal
end

#integraldecimal_1Object

Returns the value of attribute integraldecimal_1.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def integraldecimal_1
  @integraldecimal_1
end

#patt_succ(t) ⇒ Object

Returns the value of attribute patt_succ.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def patt_succ
  @patt_succ
end

#precisionObject

Returns the value of attribute precision.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def precision
  @precision
end

#repetendObject

Returns the value of attribute repetend.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def repetend
  @repetend
end

#repetend_1Object

Returns the value of attribute repetend_1.



1813
1814
1815
# File 'lib/m500.rb', line 1813

def repetend_1
  @repetend_1
end

Class Method Details

.decimalSeparatorObject



1772
1773
1774
# File 'lib/m500.rb', line 1772

def Decimal::decimalSeparator
  @@decimalSeparator
end

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



1763
1764
1765
# File 'lib/m500.rb', line 1763

def Decimal.new!(a,b,c=0,d=0,e=1)
  new(a,b,c,d,e)
end

.precisionObject



1775
1776
1777
# File 'lib/m500.rb', line 1775

def Decimal::precision
  @@precision
end

.precision=(a) ⇒ Object



1778
1779
1780
1781
1782
1783
1784
# File 'lib/m500.rb', line 1778

def Decimal::precision=(a)
  if a.class == Integer then
    @@precision = a 
  else
    @@precision = a.to_i
  end
end

Instance Method Details

#*(a) ⇒ Object



1989
1990
1991
1992
1993
1994
1995
1996
1997
# File 'lib/m500.rb', line 1989

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) ⇒ Object



2001
2002
2003
# File 'lib/m500.rb', line 2001

def ** (a)
  self.to_f ** a.to_f
end

#+(a) ⇒ Object



1842
1843
1844
1845
1846
1847
1848
1849
1850
# File 'lib/m500.rb', line 1842

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) ⇒ Object



1851
1852
1853
1854
1855
1856
1857
1858
1859
# File 'lib/m500.rb', line 1851

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) ⇒ Object



1998
1999
2000
# File 'lib/m500.rb', line 1998

def / (a)
  ((self.to_Q) /(a.to_Q)).to_Dec
end

#<=>(other) ⇒ Object



2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
# File 'lib/m500.rb', line 2004

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) ⇒ Object



2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
# File 'lib/m500.rb', line 2034

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)  # =8= cr
    self == other.to_Dec
  else
    other.to_i == self.to_i
  end
end

#coerce(other) ⇒ Object



2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
# File 'lib/m500.rb', line 2127

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

#d2FracObject



2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
# File 'lib/m500.rb', line 2079

def d2Frac
  t0 = Quotient(0,1)
  s = @decimalfraction.to_s.length > 0 ? @decimalfraction.to_s.length - 1 : 0
  #p "decimal fraction part"  
  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)
  #p "repetend part"
  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)
  #Fraction(Zahlen(@integraldecimal),(t0 + Quotient(Zahlen(0),t1)),@absolute)
end

#inspectObject



2118
2119
2120
2121
2122
2123
2124
2125
2126
# File 'lib/m500.rb', line 2118

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

#is_0?Boolean

Returns:

  • (Boolean)


2064
2065
2066
# File 'lib/m500.rb', line 2064

def is_0?
  @integraldecimal === 0 and @decimalfraction.nil? and  @repetend.call == "0" and   @decimalExponent ==0 ? true : false
end

#isRE?(a, re) ⇒ Boolean

Returns:

  • (Boolean)


1835
1836
1837
1838
1839
1840
1841
# File 'lib/m500.rb', line 1835

def isRE?(a,re)
  if a =~ re  
    true
  else  
    false
  end  
end

#minus(a) ⇒ Object

forces Decimal class subtraction NOT working



1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
# File 'lib/m500.rb', line 1921

def minus(a) #forces Decimal class subtraction NOT working
  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) ⇒ Object

forces Decimal class addition working



1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
# File 'lib/m500.rb', line 1860

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

#setdeltaObject



1816
1817
1818
# File 'lib/m500.rb', line 1816

def setdelta
  @setdelta = true
end

#showRE(a, re) ⇒ Object



1828
1829
1830
1831
1832
1833
1834
# File 'lib/m500.rb', line 1828

def showRE(a,re)  
  if a =~ re  
    "#{$`}<<#{$&}>>#{$'}"  
  else  
    "no match"  
  end  
end

#succObject



2051
2052
2053
# File 'lib/m500.rb', line 2051

def succ
  self + @@delta
end

#to_digitsObject



1819
1820
1821
1822
1823
1824
1825
1826
1827
# File 'lib/m500.rb', line 1819

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_fObject



2067
2068
2069
# File 'lib/m500.rb', line 2067

def to_f
  self.to_s.to_f
end

#to_FracObject

Fraction(Zahlen(@integraldecimal),(t0 + Quotient(Zahlen(0),t1)),@absolute)



2089
2090
2091
# File 'lib/m500.rb', line 2089

def to_Frac
  @to_Frac ||= d2Frac
end

#to_KObject



2098
2099
2100
# File 'lib/m500.rb', line 2098

def to_K
  Kettenbruch(self.to_Frac)
end

#to_NObject



2070
2071
2072
# File 'lib/m500.rb', line 2070

def to_N
  @decimalfraction.nil? and  @repetend.call == "0" and   @decimalExponent ==0 ? Natural(@integraldecimal) : emptySet
end

#to_N0Object



2073
2074
2075
# File 'lib/m500.rb', line 2073

def to_N0
  @decimalfraction.nil? and  @repetend.call == "0" and   @decimalExponent ==0 ? Counting(@integraldecimal) : emptySet
end

#to_QObject



2092
2093
2094
# File 'lib/m500.rb', line 2092

def to_Q
  self.to_Frac.to_Q
end

#to_RObject



2101
2102
2103
# File 'lib/m500.rb', line 2101

def to_R
  self.to_Frac
end

#to_sObject

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


2109
2110
2111
2112
2113
2114
2115
2116
2117
# File 'lib/m500.rb', line 2109

def to_s
  e = ""
  e = "-" if @absolute == -1
  t0 = ""
  t0 = "0"*(@decimalExponent - 1) if @decimalExponent>0
  t = "#{e}#{@integraldecimal}#{@@decimalSeparator}#{t0}#{@decimalfraction}#{@repetend.call}"
  eval("#{@@precision}.times{patt_succ(t)}")
  return t
end

#to_s!Object



2104
2105
2106
2107
2108
# File 'lib/m500.rb', line 2104

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_sgmlObject



1769
1770
1771
# File 'lib/m500.rb', line 1769

def to_sgml
  "<mn #{sgml_id}class='decimal'>#{self.to_s}</mn>"
end

#to_SigObject



2095
2096
2097
# File 'lib/m500.rb', line 2095

def to_Sig
  Sigma(self.to_Q)
end

#to_ZObject



2076
2077
2078
# File 'lib/m500.rb', line 2076

def to_Z
  @decimalfraction.nil? and  @repetend.call == "0" and   @decimalExponent ==0 ? Zahlen(@integraldecimal) : emptySet
end