Class: Nio::NeutralNum

Inherits:
Object
  • Object
show all
Includes:
StateEquivalent
Defined in:
lib/nio/fmt.rb,
lib/nio/fmt.rb

Overview

positional notation, unformatted numeric literal: used as intermediate form

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StateEquivalent

#==, #===, #eql?, #hash

Constructor Details

#initialize(s = '', d = '', p = nil, r = nil, dgs = DigitsDef.base(10), inexact = false, round = :inf) ⇒ NeutralNum

Returns a new instance of NeutralNum.



26
27
28
# File 'lib/nio/fmt.rb', line 26

def initialize(s='',d='',p=nil,r=nil,dgs=DigitsDef.base(10), inexact=false, round=:inf)
  set s,d,p,r,dgs,dgs, inexact, round
end

Instance Attribute Details

#dec_posObject

Returns the value of attribute dec_pos.



29
30
31
# File 'lib/nio/fmt.rb', line 29

def dec_pos
  @dec_pos
end

#digitsObject

Returns the value of attribute digits.



29
30
31
# File 'lib/nio/fmt.rb', line 29

def digits
  @digits
end

#inexactObject

Returns the value of attribute inexact.



29
30
31
# File 'lib/nio/fmt.rb', line 29

def inexact
  @inexact
end

#rep_posObject

Returns the value of attribute rep_pos.



29
30
31
# File 'lib/nio/fmt.rb', line 29

def rep_pos
  @rep_pos
end

#roundingObject

Returns the value of attribute rounding.



29
30
31
# File 'lib/nio/fmt.rb', line 29

def rounding
  @rounding
end

#signObject

Returns the value of attribute sign.



29
30
31
# File 'lib/nio/fmt.rb', line 29

def sign
  @sign
end

#specialObject

Returns the value of attribute special.



29
30
31
# File 'lib/nio/fmt.rb', line 29

def special
  @special
end

Instance Method Details

#baseObject



53
54
55
# File 'lib/nio/fmt.rb', line 53

def base
  @base
end

#base=(b) ⇒ Object



63
64
65
66
# File 'lib/nio/fmt.rb', line 63

def base=(b)
  @dgs = DigitsDef.base(b)
  @base=@dgs.radix
end

#base_digitsObject



56
57
58
# File 'lib/nio/fmt.rb', line 56

def base_digits
  @dgs
end

#base_digits=(dd) ⇒ Object



59
60
61
62
# File 'lib/nio/fmt.rb', line 59

def base_digits=(dd)
  @dgs = dd
  @base = @dgs.radix
end

#dupObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/nio/fmt.rb', line 78

def dup
  n = NeutralNum.new
  if special?
    n.set_special @special.dup, @sign.dup
  else
    #n.set @sign.dup, @digits.dup, @dec_pos.dup, @rep_pos.dup, @dgs.dup
    # in Ruby 1.6.8 Float,BigNum,Fixnum doesn't respond to dup
    n.set @sign.dup, @digits.dup, @dec_pos, @rep_pos, @dgs.dup, @inexact, @rounding
  end
  return n 
end

#inexact?Boolean

check for special numbers (which have only special and sign attributes)

Returns:

  • (Boolean)


74
75
76
# File 'lib/nio/fmt.rb', line 74

def inexact?
  @inexact
end

#round(n, mode = :fix, dir = nil) ⇒ Object



201
202
203
204
205
206
# File 'lib/nio/fmt.rb', line 201

def round(n, mode=:fix, dir=nil)
  dir ||= rounding
  nn = dup
  nn.round!(n,mode,dir)
  return nn
end

#round!(n, mode = :fix, dir = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/nio/fmt.rb', line 108

def round!(n, mode=:fix, dir=nil)
  dir ||= rounding
  trimLeadZeros
  if n==:exact
    return unless @inexact
    n = @digits.size
  end
  
  n += @dec_pos if mode==:fix
  n = [n,@digits.size].min if @inexact

  adj = 0
  dv = :tie
  if @inexact && n==@digits.size
    dv = @inexact==:roundup ? :hi : :lo
  else
    v = dig_value(n)
    v2 = 2*v
    if v2 < @base # v<((@base+1)/2)
      dv = :lo
    elsif v2 > @base # v>(@base/2)
      dv = :hi
    else
     
     (n+1...@digits.length).each do |i|
       if dig_value(i)>0
         dv = :hi
         break
       end        
     end
      
      dv = :hi if dv==:tie && @rep_pos<=n                  
    end
  end
  
  if dv==:hi
    adj = +1
  elsif dv==:tie
    if dir==:inf # towards nearest +/-infinity
      adj = +1
    elsif dir==:even # to nearest even digit (IEEE unbiased rounding)
      adj = +1 if (dig_value(n-1)%2)!=0
    elsif dir==:zero # towards zero
      adj=0
  #  elsif dir==:odd 
  #    adj = +1 unless (dig_value(n-1)%2)!=0
    end
  end    
  
  if n>@digits.length
    (@digits.length...n).each do |i|
      @digits << dig_char(dig_value(i))
      @rep_pos += 1
    end
  end
  
  prefix = ''
  i = n-1
  while adj!=0
    v = dig_value(i)
    v += adj
    adj = 0
    if v<0
      v += @base
      adj = -1
    elsif v>=@base
      v -= @base
      adj = +1
    end
    if i<0
      prefix = dig_char(v)+prefix 
    elsif i<@digits.length
      @digits[i] = dig_char(v)
    end
    i += -1
  end 
  
  if n<0
    @digits = ""  
  else
    @digits = @digits[0...n]  
  end
  @rep_pos = @digits.length

  if prefix!=''
    @digits = prefix + @digits
    @dec_pos += prefix.length
    @rep_pos += prefix.length
  end
  
  
end

#set(s, d, p = nil, r = nil, dgs = DigitsDef.base(10), inexact = false, rounding = :inf, normalize = true) ⇒ Object

set number



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nio/fmt.rb', line 33

def set(s,d,p=nil,r=nil,dgs=DigitsDef.base(10),inexact=false,rounding=:inf,normalize=true)
  @sign = s # sign: '+','-',''
  @digits = d # digits string
  @dec_pos = p==nil ? d.length : p # position of decimal point: 0=before first digit...
  @rep_pos = r==nil ? d.length : r # first repeated digit (0=first digit...)
  @dgs = dgs
  @base = @dgs.radix
  @inexact = inexact
  @special = nil
  @rounding = rounding
  trimZeros unless inexact  
  self
end

#set_special(s, sgn = '') ⇒ Object

set infinite (:inf) and invalid (:nan) numbers



47
48
49
50
51
# File 'lib/nio/fmt.rb', line 47

def set_special(s,sgn='') # :inf, :nan
  @special = s
  @sign = sgn
  self
end

#special?Boolean

check for special numbers (which have only special and sign attributes)

Returns:

  • (Boolean)


69
70
71
# File 'lib/nio/fmt.rb', line 69

def special?
  special != nil
end

#to_RepDecObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/nio/fmt.rb', line 283

def to_RepDec
  n = RepDec.new(@base)
  if special?
    
    case special
      when :nan
        n.ip = :indeterminate
      when :inf
        if sign=='-'
          n.ip = :posinfinity
        else
          n.ip  :neginfinity
        end
      else
        n = nil
    end
    
  else
    if dec_pos<=0
      n.ip = 0
      n.d =  text_to_digits(dig_char(0)*(-dec_pos) + digits)
    elsif dec_pos >= digits.length
      n.ip = digits.to_i(@base)        
      if rep_pos<dec_pos          
        i=0
        (dec_pos-digits.length).times do
          n.ip *= @base
          n.ip += @dgs.digit_value(digits[rep_pos+i]) if rep_pos+i<digits.length
          i += 1
          i=0 if i>=digits.length-rep_pos
        end
        n.d = []
        while i<digits.length-rep_pos
          n.d << @dgs.digit_value(digits[rep_pos+i])
          i += 1
        end
        new_rep_pos = n.d.size + dec_pos
        n.d += text_to_digits(digits[rep_pos..-1])
        self.rep_pos = new_rep_pos
      else
        n.ip *= @base**(dec_pos-digits.length)
        n.d = []
      end
    else
      n.ip = digits[0...dec_pos].to_i(@base)
      n.d = text_to_digits(digits[dec_pos..-1])
      if rep_pos<dec_pos 
        new_rep_pos = n.d.size + dec_pos
        n.d += text_to_digits(digits[rep_pos..-1])
        self.rep_pos = new_rep_pos
        puts "--rep_pos=#{rep_pos}"
      end        
    end
    n.sign = -1 if sign=='-'
    n.rep_i = rep_pos - dec_pos     
  end
  n.normalize!(!inexact) # keep trailing zeros for inexact numbers
  return n
end

#trimLeadZerosObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/nio/fmt.rb', line 226

def trimLeadZeros()
  i = 0
  while i<@digits.length && dig_value(i)==0
    i += 1
  end
  @digits = @digits[i...@digits.length]
  @dec_pos -= i
  @rep_pos -= i
  
  if @digits==''
    @digits = dig_char(0) # '0'
    @rep_pos = 1
    @dec_pos = 1
  end
  
end

#trimTrailZerosObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/nio/fmt.rb', line 208

def trimTrailZeros()    
  i = @digits.length
  while i>0 && dig_value(i-1)==0
    i -= 1    
  end
  if @rep_pos>=i
    @digits = @digits[0...i]
    @rep_pos = i
  end
  
  if @digits==''
    @digits = dig_char(0) # '0'
    @rep_pos = 1
    @dec_pos = 1
  end
  
end

#trimZerosObject



243
244
245
246
# File 'lib/nio/fmt.rb', line 243

def trimZeros()    
  trimLeadZeros
  trimTrailZeros
end

#zero?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/nio/fmt.rb', line 90

def zero?
  z = false
  if !special
    if digits==''
      z = true
    else
      z = true
      for i in (0...@digits.length)
        if dig_value(i)!=0
          z = false
          break
        end
      end
    end
  end
  z
end