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.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def absolute
  @absolute
end

#decimalExponentObject

Returns the value of attribute decimalExponent.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def decimalExponent
  @decimalExponent
end

#decimalfractionObject

Returns the value of attribute decimalfraction.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def decimalfraction
  @decimalfraction
end

#decimalfraction_1Object

Returns the value of attribute decimalfraction_1.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def decimalfraction_1
  @decimalfraction_1
end

#integraldecimalObject

Returns the value of attribute integraldecimal.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def integraldecimal
  @integraldecimal
end

#integraldecimal_1Object

Returns the value of attribute integraldecimal_1.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def integraldecimal_1
  @integraldecimal_1
end

#patt_succ(t) ⇒ Object

Returns the value of attribute patt_succ.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def patt_succ
  @patt_succ
end

#precisionObject

Returns the value of attribute precision.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def precision
  @precision
end

#repetendObject

Returns the value of attribute repetend.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def repetend
  @repetend
end

#repetend_1Object

Returns the value of attribute repetend_1.



1885
1886
1887
# File 'lib/m500.rb', line 1885

def repetend_1
  @repetend_1
end

Class Method Details

.decimalSeparatorObject



1844
1845
1846
# File 'lib/m500.rb', line 1844

def Decimal::decimalSeparator
  @@decimalSeparator
end

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



1835
1836
1837
# File 'lib/m500.rb', line 1835

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

.precisionObject



1847
1848
1849
# File 'lib/m500.rb', line 1847

def Decimal::precision
  @@precision
end

.precision=(a) ⇒ Object



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

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

Instance Method Details

#*(a) ⇒ Object



2061
2062
2063
2064
2065
2066
2067
2068
2069
# File 'lib/m500.rb', line 2061

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



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

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

#+(a) ⇒ Object



1914
1915
1916
1917
1918
1919
1920
1921
1922
# File 'lib/m500.rb', line 1914

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



1923
1924
1925
1926
1927
1928
1929
1930
1931
# File 'lib/m500.rb', line 1923

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



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

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

#<=>(other) ⇒ Object



2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
# File 'lib/m500.rb', line 2076

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



2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
# File 'lib/m500.rb', line 2106

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



2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
# File 'lib/m500.rb', line 2196

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



2151
2152
2153
2154
2155
2156
2157
# File 'lib/m500.rb', line 2151

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

#inspectObject



2187
2188
2189
2190
2191
2192
2193
2194
2195
# File 'lib/m500.rb', line 2187

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)


2136
2137
2138
# File 'lib/m500.rb', line 2136

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

#isRE?(a, re) ⇒ Boolean

Returns:

  • (Boolean)


1907
1908
1909
1910
1911
1912
1913
# File 'lib/m500.rb', line 1907

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

#minus(a) ⇒ Object



1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
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
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
# File 'lib/m500.rb', line 1993

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

forces Decimal class addition working



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
1989
1990
1991
1992
# File 'lib/m500.rb', line 1932

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



1888
1889
1890
# File 'lib/m500.rb', line 1888

def setdelta
  @setdelta = true
end

#showRE(a, re) ⇒ Object



1900
1901
1902
1903
1904
1905
1906
# File 'lib/m500.rb', line 1900

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

#succObject



2123
2124
2125
# File 'lib/m500.rb', line 2123

def succ
  self + @@delta
end

#to_digitsObject



1891
1892
1893
1894
1895
1896
1897
1898
1899
# File 'lib/m500.rb', line 1891

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



2139
2140
2141
# File 'lib/m500.rb', line 2139

def to_f
  self.to_s.to_f
end

#to_FracObject



2158
2159
2160
# File 'lib/m500.rb', line 2158

def to_Frac
  @to_Frac ||= d2Frac
end

#to_KObject



2167
2168
2169
# File 'lib/m500.rb', line 2167

def to_K
  Kettenbruch(self.to_Frac)
end

#to_NObject



2142
2143
2144
# File 'lib/m500.rb', line 2142

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

#to_N0Object



2145
2146
2147
# File 'lib/m500.rb', line 2145

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

#to_QObject



2161
2162
2163
# File 'lib/m500.rb', line 2161

def to_Q
  self.to_Frac.to_Q
end

#to_RObject



2170
2171
2172
# File 'lib/m500.rb', line 2170

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


2178
2179
2180
2181
2182
2183
2184
2185
2186
# File 'lib/m500.rb', line 2178

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



2173
2174
2175
2176
2177
# File 'lib/m500.rb', line 2173

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



1841
1842
1843
# File 'lib/m500.rb', line 1841

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

#to_SigObject



2164
2165
2166
# File 'lib/m500.rb', line 2164

def to_Sig
  Sigma(self.to_Q)
end

#to_ZObject



2148
2149
2150
# File 'lib/m500.rb', line 2148

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