Class: Nio::RepDec

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

Overview

RepDec handles repeating decimals (repeating numerals actually)

Defined Under Namespace

Classes: Opt

Constant Summary collapse

DEF_OPT =
Opt.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StateEquivalent

#===, #eql?, #hash

Constructor Details

#initialize(b = 10) ⇒ RepDec

Returns a new instance of RepDec.



134
135
136
# File 'lib/nio/repdec.rb', line 134

def initialize(b=10)
  setZ(b)
end

Instance Attribute Details

#dObject

protected



474
475
476
# File 'lib/nio/repdec.rb', line 474

def d
  @d
end

#ipObject

protected



474
475
476
# File 'lib/nio/repdec.rb', line 474

def ip
  @ip
end

#rep_iObject

protected



474
475
476
# File 'lib/nio/repdec.rb', line 474

def rep_i
  @rep_i
end

#signObject

protected



474
475
476
# File 'lib/nio/repdec.rb', line 474

def sign
  @sign
end

Class Method Details

.group_digits(digits, opt) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/nio/repdec.rb', line 480

def RepDec.group_digits(digits, opt)
  if opt.grp_sep!=nil && opt.grp_sep!='' && opt.grp.length>0
    grouped = ''
    i = 0
    while digits.length>0
      l = opt.grp[i]
      l = digits.length if l>digits.length
      grouped = opt.grp_sep + grouped if grouped.length>0
      grouped = digits[-l,l] + grouped
      digits = digits[0,digits.length-l]
      i += 1 if i<opt.grp.length-1
    end
    grouped
  else
   digits
  end
end

.maximum_number_of_digitsObject

Return the maximum number of digits that RepDec objects can handle.



389
390
391
# File 'lib/nio/repdec.rb', line 389

def RepDec.maximum_number_of_digits
  @max_d
end

.maximum_number_of_digits=(n) ⇒ Object

Change the maximum number of digits that RepDec objects can handle.



384
385
386
# File 'lib/nio/repdec.rb', line 384

def RepDec.maximum_number_of_digits=(n)
  @max_d = [n,2048].max
end

.parse(str, opt = DEF_OPT) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/nio/repdec.rb', line 211

def RepDec.parse(str, opt=DEF_OPT)
  sgn,i_str,f_str,ri,detect_rep = nil,nil,nil,nil,nil

  i = 0;
  l = str.length;

  detect_rep = false;

  
  i += 1 while i<str.length && str[i,1] =~/\s/
  
  
  neg = false;

  neg = true if str[i,1]=='-'
  i += 1 if str[i,1]=='-' || str[i,1]=='+'
  

  i += 1 while i<str.length && str[i,1] =~/\s/
  

  str.upcase!
  if str[i,opt.nan_txt.size]==opt.nan_txt.upcase
    i_str = :indeterminate;
  elsif str[i,opt.inf_txt.size]==opt.inf_txt.upcase
    i_str = neg ? :neginfinity : :posinfinity;
  end
  
  unless i_str
    i_str = "0";
    while i<l && str[i,1]!=opt.dec_sep
      break if str[i,opt.auto_rep.length]==opt.auto_rep && opt.auto_rep!=''
      i_str += str[i,1] if str[i,1]!=opt.grp_sep
      i += 1;
    end
    sgn = neg ? -1 : +1
    i += 1; # skip the decimal separator
  end
  
  unless i_str.kind_of?(Symbol)
    j = 0;
    f_str = ''
    while i<l
      ch = str[i,1];
      if ch==opt.begin_rep then
        ri = j;
      elsif ch==opt.end_rep then
        i = l;
      elsif ch==opt.auto_rep[0,1] then
        detect_rep = true;
        i = l;
      else
        f_str << ch
        j += 1;
      end
      i += 1;
    end
  end
  return [sgn,i_str,f_str,ri,detect_rep]
end

Instance Method Details

#==(c) ⇒ Object



370
371
372
373
374
375
376
# File 'lib/nio/repdec.rb', line 370

def ==(c)
  a = copy;
  b = c.copy;
  a.normalize!
  b.normalize!
  return a.ip==b.ip && a.d==b.d && a.rep_i==b.rep_i
end

#copyObject



364
365
366
367
368
# File 'lib/nio/repdec.rb', line 364

def copy()
  c = clone
  c.d = d.clone
  return c;
end

#getQ(opt = DEF_OPT) ⇒ Object

Raises:



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/nio/repdec.rb', line 433

def getQ(opt=DEF_OPT)
  raise RepDecError,"Base mismatch: #{opt.digits.radix} when #{@radix} was expected." if opt.digits_defined? && @radix!=opt.digits.radix
  
  if !ip.is_a?(Integer) then
    y = 0;
    x=0 if ip==:indeterminate;
    x=1 if ip==:posinfinity
    x=-1 if ip==:neginfinity
    return x,y;
  end if

  
  n = @d.length
  a = @ip
  b = a
  for i in 0...n
    a*=@radix
    a+=@d[i];
    if @rep_i!=nil && i<@rep_i
      b *= @radix
      b += @d[i];
    end
  end

  x = a
  x -= b if @rep_i!=nil

  y = @radix**n
  y -= @radix**@rep_i if @rep_i!=nil

  d = Nio.gcd(x,y)
  x /= d
  y /= d

  x = -x if @sign<0

  return x,y;
end

#getS(nrep = 0, opt = DEF_OPT) ⇒ Object

Raises:



272
273
274
275
276
277
278
279
280
281
282
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
# File 'lib/nio/repdec.rb', line 272

def getS(nrep=0, opt=DEF_OPT)
  raise RepDecError,"Base mismatch: #{opt.digits.radix} when #{@radix} was expected." if opt.digits_defined? && @radix!=opt.digits.radix
   
   if !ip.is_a?(Integer) then
     str=opt.nan_txt if ip==:indeterminate;
     str=opt.inf_txt if ip==:posinfinity
     str='-'+opt.inf_txt if ip==:neginfinity
     return str;
   end
   
   s = "";
   s += '-' if @sign<0
   s += RepDec.group_digits(@ip.to_s(@radix),opt);
   s += opt.dec_sep if @d.length>0;
   for i in 0...@d.length
     break if nrep>0 && @rep_i==i;
     s += opt.begin_rep if i==@rep_i;
     s << opt.digits.digit_char(@d[i])
   end;
   if nrep>0 then
     if @rep_i!=nil then
        nrep += 1;
        nrep.times do
          for i in @rep_i...@d.length
           s << opt.digits.digit_char(@d[i])
          end
        end
        
        check = RepDec.new;
        check.setS s+opt.auto_rep, opt;
        #print " s=",s,"\n"
        #print " self=",self.to_s,"\n"
        while check!=self
          for i in @rep_i...@d.length
            s << opt.digits.digit_char(@d[i])
          end
          check.setS s+opt.auto_rep, opt;
        end
        
        s += opt.auto_rep;
     end
   else
     s += opt.end_rep if @rep_i!=nil;
   end
   return s;
end

#normalize!(remove_trailing_zeros = true) ⇒ Object



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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/nio/repdec.rb', line 323

def normalize!(remove_trailing_zeros=true)
  if ip.is_a?(Integer)
    if @rep_i!=nil && @rep_i==@d.length-1 && @d[@rep_i]==(@radix-1) then
      @d.pop;
      @rep_i = nil;
      
      i = @d.length-1;
      carry = 1;
      while carry>0 && i>=0
        @d[i] += carry;
        carry = 0;
        if @d[i]>(@radix) then
          carry = 1;
          @d[i]=0;
          @d.pop if i==@d.length;
        end
        i -= 1;
      end
      @ip += carry;
      
    end
    
    if @rep_i!=nil && @rep_i>=@d.length
      @rep_i = nil
    end
    
    if @rep_i!=nil && @rep_i>=0
      unless @d[@rep_i..-1].find {|x| x!=0}
        @d = @d[0...@rep_i]
        @rep_i = nil
      end
    end
    if @rep_i==nil && remove_trailing_zeros
      while @d[@d.length-1]==0
        @d.pop
      end
    end
    
  end
end

#setQ(x, y, opt = DEF_OPT) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/nio/repdec.rb', line 393

def setQ(x,y, opt=DEF_OPT)
  @radix = opt.digits.radix if opt.digits_defined?
  xy_sign = x==0 ? 0 : x<0 ? -1 : +1;
  xy_sign = -xy_sign if y<0;
  @sign = xy_sign
  x = x.abs;
  y = y.abs;

  @d = [];
  @rep_i = nil;
  
  if y==0 then
    if x==0 then
      @ip = :indeterminate
    else
      @ip = xy_sign==-1 ? :neginfinity : :posinfinity
    end
    return self
  end
  
  k = {};
  @ip = x.div(y) #x/y;
  x -= @ip*y;
  i = 0;
  ended = false;

  max_d = opt.max_d
  while x>0 && @rep_i==nil && (max_d<=0 || i<max_d)
    @rep_i = k[x]
    if @rep_i.nil? then
      k[x] = i;
      x *= @radix
      d,x = x.divmod(y)
      @d.push d
      i += 1;
    end
  end
  self
end

#setS(str, opt = DEF_OPT) ⇒ Object



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
207
208
209
# File 'lib/nio/repdec.rb', line 147

def setS(str, opt=DEF_OPT)
  setZ(opt.digits_defined? ? opt.digits.radix : @radix);
  sgn,i_str,f_str,ri,detect_rep = RepDec.parse(str,opt)
  if i_str.kind_of?(Symbol)
    @ip = i_str
  else
    @ip = i_str.to_i(@radix); # this assumes conventional digits
  end
  @sign = sgn
  @rep_i = ri if ri
  f_str.each_byte{|b| @d.push opt.digits.digit_value(b)} unless f_str.nil?

  if detect_rep then
    
    for l in 1..(@d.length/2)
      l = @d.length/2 + 1 - l;
      if @d[-l..-1]==@d[-2*l...-l]
        
        for m in 1..l
          if l.modulo(m)==0 then
            reduce_l = true;
            for i in 2..l/m
              if @d[-m..-1]!=@d[-i*m...-i*m+m] then
                 reduce_l = false;
                 break;
              end
            end
            if reduce_l then
               l = m
               break
            end
          end
        end
        
        
        @rep_i = @d.length - 2*l;
        l.times { @d.pop }
        
        
        while @d.length >= 2*l && @d[-l..-1]==@d[-2*l...-l]
          
          @rep_i = @d.length - 2*l;
          l.times { @d.pop }
          
        end
        
        break
      end
    end
    
  end

  
  if @rep_i!=nil then
    if @d.length==@rep_i+1 && @d[@rep_i]==0 then
      @rep_i = nil;
      @d.pop;
    end
  end
  @d.pop while @d[@d.length-1]==0
  
  self
end

#setZ(b = 10) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/nio/repdec.rb', line 138

def setZ(b=10)
   @ip    = 0;
   @d     = [];
   @rep_i = nil;
   @sign  = 0;
   @radix = b;
   self
end

#to_NeutralNum(base_dgs = nil) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/nio/fmt.rb', line 360

def to_NeutralNum(base_dgs=nil)
  num = NeutralNum.new
  if !ip.is_a?(Integer)
    
    case ip
      when :indeterminate
        num.set_special :nan
      when :posinfinity
        num.set_special :inf,'+'
      when :neginfinity
        num.set_special :inf,'-'
      else
        num = nil
    end
    
  else
    base_dgs ||= DigitsDef.base(@radix)
    # assert base_dgs.radix == @radix
    signch = sign<0 ? '-' : '+'
    decimals = ip.to_s(@radix)
    dec_pos = decimals.length
    d.each {|dig| decimals << base_dgs.digit_char(dig) }
    rep_pos = rep_i==nil ? decimals.length : dec_pos + rep_i
    num.set signch, decimals, dec_pos, rep_pos, base_dgs
  end
  return num
end

#to_sObject



319
320
321
# File 'lib/nio/repdec.rb', line 319

def to_s()
  getS
end