Module: Nio::BurgerDybvig

Defined in:
lib/nio/fmt.rb

Overview

:nodoc: all

Class Method Summary collapse

Class Method Details

.exptt(_B, k) ⇒ Object



1459
1460
1461
1462
1463
1464
1465
# File 'lib/nio/fmt.rb', line 1459

def exptt(_B, k)
  if _B==10 && k>=0 && k<326 
    $exptt_table[k]
  else
    _B**k
  end
end

.fixup(r, s, m_p, m_m, k, _B, low_ok, high_ok) ⇒ Object



1397
1398
1399
1400
1401
1402
1403
# File 'lib/nio/fmt.rb', line 1397

def fixup(r,s,m_p,m_m,k,_B,low_ok,high_ok)
  if (high_ok ? (r+m_p >= s) : (r+m_p > s)) # too low?
    [r,s*_B,m_p,m_m,k+1]
  else
    [r,s,m_p,m_m,k]
  end
end

.float_to_digits(v, f, e, round_mode, min_e, p, b, _B) ⇒ Object



1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
# File 'lib/nio/fmt.rb', line 1348

def float_to_digits(v,f,e,round_mode,min_e,p,b,_B)
 
 case round_mode
   when :even
     roundl = roundh = f.even?
   when :inf
     roundl = true
     roundh = false
   when :zero
     roundl = false
     roundh = true
   else
     # here we don't assume any rounding in the floating point numbers
     # the result is valid for any rounding but may produce more digits
     # than stricly necessary for specifica rounding modes.
     roundl = false
     roundh = false
 end
  
    if e >= 0
      if f != exptt(b,p-1)
        be = exptt(b,e)
        r,s,m_p,m_m,k = scale(f*be*2,2,be,be,0,_B,roundl ,roundh,v)
      else
        be = exptt(b,e)
        be1 = be*b
        r,s,m_p,m_m,k = scale(f*be1*2,b*2,be1,be,0,_B,roundl ,roundh,v)
      end
    else
      if e==min_e or f != exptt(b,p-1)
        r,s,m_p,m_m,k = scale(f*2,exptt(b,-e)*2,1,1,0,_B,roundl ,roundh,v)
      else
        r,s,m_p,m_m,k = scale(f*b*2,exptt(b,1-e)*2,b,1,0,_B,roundl ,roundh,v)
      end
    end
    [k]+generate(r,s,m_p,m_m,_B,roundl ,roundh)
end

.float_to_digits_max(v, f, e, round_mode, min_e, p, b, _B) ⇒ Object



1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
# File 'lib/nio/fmt.rb', line 1477

def float_to_digits_max(v,f,e,round_mode,min_e,p,b,_B)
 
 case round_mode
   when :even
     roundl = roundh = f.even?
   when :inf
     roundl = true
     roundh = false
   when :zero
     roundl = false
     roundh = true
   else
     # here we don't assume any rounding in the floating point numbers
     # the result is valid for any rounding but may produce more digits
     # than stricly necessary for specifica rounding modes.
     roundl = false
     roundh = false
 end
  
    if e >= 0
      if f != exptt(b,p-1)
        be = exptt(b,e)
        r,s,m_p,m_m,k = scale(f*be*2,2,be,be,0,_B,roundl ,roundh,v)
      else
        be = exptt(b,e)
        be1 = be*b
        r,s,m_p,m_m,k = scale(f*be1*2,b*2,be1,be,0,_B,roundl ,roundh,v)
      end
    else
      if e==min_e or f != exptt(b,p-1)
        r,s,m_p,m_m,k = scale(f*2,exptt(b,-e)*2,1,1,0,_B,roundl ,roundh,v)
      else
        r,s,m_p,m_m,k = scale(f*b*2,exptt(b,1-e)*2,b,1,0,_B,roundl ,roundh,v)
      end
    end
    [k]+generate_max(r,s,m_p,m_m,_B,roundl ,roundh)
end

.generate(r, s, m_p, m_m, _B, low_ok, high_ok) ⇒ Object



1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
# File 'lib/nio/fmt.rb', line 1422

def generate(r,s,m_p,m_m,_B,low_ok ,high_ok)
  list = []
  loop do
    d,r = (r*_B).divmod(s)
    m_p *= _B
    m_m *= _B
    tc1 = low_ok ? (r<=m_m) : (r<m_m)
    tc2 = high_ok ? (r+m_p >= s) : (r+m_p > s)

     if not tc1
      if not tc2
        list << d
      else
        list << d+1
        break
      end
    else
      if not tc2
        list << d
        break
      else
        if r*2 < s
          list << d
          break
        else
          list << d+1
          break
        end
      end
    end
    
  end
  list
end

.generate_max(r, s, m_p, m_m, _B, low_ok, high_ok) ⇒ Object



1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
# File 'lib/nio/fmt.rb', line 1515

def generate_max(r,s,m_p,m_m,_B,low_ok ,high_ok)
  list = [false]
  loop do
    d,r = (r*_B).divmod(s)
    m_p *= _B
    m_m *= _B
    
    list << d
        
    tc1 = low_ok ? (r<=m_m) : (r<m_m)
    tc2 = high_ok ? (r+m_p >= s) : (r+m_p > s)
        
    if tc1 && tc2
      list[0] = true if r*2 >= s
      break
    end    
  end
  list
end

.logB(_B, x) ⇒ Object



1469
1470
1471
1472
1473
1474
1475
# File 'lib/nio/fmt.rb', line 1469

def logB(_B, x)
  if _B>=2 && _B<37
    Math.log(x)*$logB_table[_B]
  else
    Math.log(x)/Math.log(_B)
  end
end

.scale(r, s, m_p, m_m, k, _B, low_ok, high_ok, v) ⇒ Object



1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
# File 'lib/nio/fmt.rb', line 1386

def scale(r,s,m_p,m_m,k,_B,low_ok ,high_ok,v)
  return scale2(r,s,m_p,m_m,k,_B,low_ok ,high_ok) if v==0
  est = (logB(_B,v)-1E-10).ceil.to_i
  if est>=0 
    fixup(r,s*exptt(_B,est),m_p,m_m,est,_B,low_ok,high_ok)
  else
    sc = exptt(_B,-est)
    fixup(r*sc,s,m_p*sc,m_m*sc,est,_B,low_ok,high_ok)
  end
end

.scale2(r, s, m_p, m_m, k, _B, low_ok, high_ok) ⇒ Object



1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
# File 'lib/nio/fmt.rb', line 1405

def scale2(r,s,m_p,m_m,k,_B,low_ok ,high_ok)
  loop do
    if (high_ok ? (r+m_p >= s) : (r+m_p > s)) # k is too low
      s *= _B
      k += 1
    elsif (high_ok ? ((r+m_p)*_B<s) : ((r+m_p)*_B<=s)) # k is too high
      r *= _B
      m_p *= _B
      m_m *= _B
      k -= 1
    else
      break
    end
  end
  [r,s,m_p,m_m,k]
end