Class: Nio::NeutralNum
- Inherits:
-
Object
- Object
- Nio::NeutralNum
- 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
-
#dec_pos ⇒ Object
Returns the value of attribute dec_pos.
-
#digits ⇒ Object
Returns the value of attribute digits.
-
#inexact ⇒ Object
Returns the value of attribute inexact.
-
#rep_pos ⇒ Object
Returns the value of attribute rep_pos.
-
#rounding ⇒ Object
Returns the value of attribute rounding.
-
#sign ⇒ Object
Returns the value of attribute sign.
-
#special ⇒ Object
Returns the value of attribute special.
Instance Method Summary collapse
- #base ⇒ Object
- #base=(b) ⇒ Object
- #base_digits ⇒ Object
- #base_digits=(dd) ⇒ Object
- #dup ⇒ Object
-
#inexact? ⇒ Boolean
check for special numbers (which have only special and sign attributes).
-
#initialize(s = '', d = '', p = nil, r = nil, dgs = DigitsDef.base(10), inexact = false, round = :inf) ⇒ NeutralNum
constructor
A new instance of NeutralNum.
- #round(n, mode = :fix, dir = nil) ⇒ Object
- #round!(n, mode = :fix, dir = nil) ⇒ Object
-
#set(s, d, p = nil, r = nil, dgs = DigitsDef.base(10), inexact = false, rounding = :inf, normalize = true) ⇒ Object
set number.
-
#set_special(s, sgn = '') ⇒ Object
set infinite (:inf) and invalid (:nan) numbers.
-
#special? ⇒ Boolean
check for special numbers (which have only special and sign attributes).
- #to_RepDec ⇒ Object
- #trimLeadZeros ⇒ Object
- #trimTrailZeros ⇒ Object
- #trimZeros ⇒ Object
- #zero? ⇒ Boolean
Methods included from StateEquivalent
Constructor Details
#initialize(s = '', d = '', p = nil, r = nil, dgs = DigitsDef.base(10), inexact = false, round = :inf) ⇒ NeutralNum
Returns a new instance of NeutralNum.
28 29 30 |
# File 'lib/nio/fmt.rb', line 28 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_pos ⇒ Object
Returns the value of attribute dec_pos.
31 32 33 |
# File 'lib/nio/fmt.rb', line 31 def dec_pos @dec_pos end |
#digits ⇒ Object
Returns the value of attribute digits.
31 32 33 |
# File 'lib/nio/fmt.rb', line 31 def digits @digits end |
#inexact ⇒ Object
Returns the value of attribute inexact.
31 32 33 |
# File 'lib/nio/fmt.rb', line 31 def inexact @inexact end |
#rep_pos ⇒ Object
Returns the value of attribute rep_pos.
31 32 33 |
# File 'lib/nio/fmt.rb', line 31 def rep_pos @rep_pos end |
#rounding ⇒ Object
Returns the value of attribute rounding.
31 32 33 |
# File 'lib/nio/fmt.rb', line 31 def rounding @rounding end |
#sign ⇒ Object
Returns the value of attribute sign.
31 32 33 |
# File 'lib/nio/fmt.rb', line 31 def sign @sign end |
#special ⇒ Object
Returns the value of attribute special.
31 32 33 |
# File 'lib/nio/fmt.rb', line 31 def special @special end |
Instance Method Details
#base ⇒ Object
55 56 57 |
# File 'lib/nio/fmt.rb', line 55 def base @base end |
#base=(b) ⇒ Object
65 66 67 68 |
# File 'lib/nio/fmt.rb', line 65 def base=(b) @dgs = DigitsDef.base(b) @base=@dgs.radix end |
#base_digits ⇒ Object
58 59 60 |
# File 'lib/nio/fmt.rb', line 58 def base_digits @dgs end |
#base_digits=(dd) ⇒ Object
61 62 63 64 |
# File 'lib/nio/fmt.rb', line 61 def base_digits=(dd) @dgs = dd @base = @dgs.radix end |
#dup ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/nio/fmt.rb', line 80 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)
76 77 78 |
# File 'lib/nio/fmt.rb', line 76 def inexact? @inexact end |
#round(n, mode = :fix, dir = nil) ⇒ Object
208 209 210 211 212 213 |
# File 'lib/nio/fmt.rb', line 208 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
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 200 201 202 203 204 205 206 |
# File 'lib/nio/fmt.rb', line 110 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 # TODO: the combination of the value true with the values of Formatter#round_up makes this ugly dv = @inexact.is_a?(Symbol) ? @inexact : :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 if @inexact dv = :hi else (n+1...@digits.length).each do |i| if dig_value(i)>0 dv = :hi break end 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
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nio/fmt.rb', line 35 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
49 50 51 52 53 |
# File 'lib/nio/fmt.rb', line 49 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)
71 72 73 |
# File 'lib/nio/fmt.rb', line 71 def special? special != nil end |
#to_RepDec ⇒ Object
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 342 343 344 345 346 347 348 |
# File 'lib/nio/fmt.rb', line 290 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 |
#trimLeadZeros ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/nio/fmt.rb', line 233 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 |
#trimTrailZeros ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/nio/fmt.rb', line 215 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 |
#trimZeros ⇒ Object
250 251 252 253 |
# File 'lib/nio/fmt.rb', line 250 def trimZeros() trimLeadZeros trimTrailZeros end |
#zero? ⇒ Boolean
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/nio/fmt.rb', line 92 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 |