Module: Nio::BurgerDybvig

Defined in:
lib/nio/fmt.rb

Overview

:nodoc: all

Class Method Summary collapse

Class Method Details

.exptt(_B, k) ⇒ Object



1425
1426
1427
1428
1429
1430
1431
# File 'lib/nio/fmt.rb', line 1425

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



1363
1364
1365
1366
1367
1368
1369
# File 'lib/nio/fmt.rb', line 1363

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



1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
# File 'lib/nio/fmt.rb', line 1314

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



1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
# File 'lib/nio/fmt.rb', line 1443

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



1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
# File 'lib/nio/fmt.rb', line 1388

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



1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'lib/nio/fmt.rb', line 1481

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



1435
1436
1437
1438
1439
1440
1441
# File 'lib/nio/fmt.rb', line 1435

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



1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
# File 'lib/nio/fmt.rb', line 1352

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



1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'lib/nio/fmt.rb', line 1371

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