Class: Float

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Nio::Formattable

append_features, #nio_round, #nio_write

Class Method Details

.nio_read_neutral(neutral) ⇒ Object



1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
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
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
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
# File 'lib/nio/fmt.rb', line 1303

def self.nio_read_neutral(neutral)
  x = nil
  
  honor_rounding = true
  
  if neutral.special?
    case neutral.special
      when :nan
        x = 0.0/0.0
      when :inf
        x = (neutral.sign=='-' ? -1.0 : +1.0)/0.0
    end
  elsif neutral.rep_pos<neutral.digits.length
    
    x,y = neutral.to_RepDec.getQ
    x = Float(x)/y
    
  else
    nd = neutral.base==10 ? Float::DIG : ((Float::MANT_DIG-1)*Math.log(2)/Math.log(neutral.base)).floor
    k = neutral.dec_pos-neutral.digits.length
    if !honor_rounding && (neutral.digits.length<=nd && k.abs<=15)
      x = neutral.digits.to_i(neutral.base).to_f
      if k<0
        x /= Float(neutral.base**-k)
      else
        x *= Float(neutral.base**k)
      end
      x = -x if neutral.sign=='-'
    elsif !honor_rounding && (k>0 && (k+neutral.digits.length < 2*nd))
      j = k-neutral.digits.length
      x = neutral.digits.to_i(neutral.base).to_f * Float(neutral.base**(j))
      x *= Float(neutral.base**(k-j))
      x = -x if neutral.sign=='-'
    elsif neutral.base.modulo(Float::RADIX)==0
     
     f = neutral.digits.to_i(neutral.base)
     e = neutral.dec_pos-neutral.digits.length

     rounding = case neutral.rounding
     when :even
       :half_even
     when :zero
       :half_down
     when :inf
       :half_up
     when :truncate
       :down
     when :directed_up
       :up
     when :floor
       :floor
     when :ceil
       :ceil
     else
       nil
     end
     
     reader = Flt::Support::Reader.new(:mode=>:fixed)
     sign = neutral.sign == '-' ? -1 : +1
     x = reader.read(Float.context, rounding, sign, f, e, neutral.base)
     exact = reader.exact?
     
    else
     
     f = neutral.digits.to_i(neutral.base)
     e = neutral.dec_pos-neutral.digits.length

     rounding = case neutral.rounding
     when :even
       :half_even
     when :zero
       :half_down
     when :inf
       :half_up
     when :truncate
       :down
     when :directed_up
       :up
     when :floor
       :floor
     when :ceil
       :ceil
     else
       nil
     end
     
     reader = Flt::Support::Reader.new(:mode=>:fixed)
     sign = neutral.sign == '-' ? -1 : +1
     x = reader.read(Float.context, rounding, sign, f, e, neutral.base)
     exact = reader.exact?
     
    end
  end
  
  return x
end

Instance Method Details

#nio_r(tol = Flt.Tolerance(:big_epsilon)) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/nio/rtnlzr.rb', line 107

def nio_r(tol = Flt.Tolerance(:big_epsilon))
  case tol
    when Integer
      Rational(*Nio::Rtnlzr.max_denominator(self,tol,Float))
    else
      Rational(*Nio::Rtnlzr.new(tol).rationalize(self))
  end
end

#nio_write_neutral(fmt) ⇒ Object



1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
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
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
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
# File 'lib/nio/fmt.rb', line 1399

def nio_write_neutral(fmt)
  neutral = Nio::NeutralNum.new
  x = self
  
  if x.nan?
    neutral.set_special(:nan)
  elsif x.infinite?
    neutral.set_special(:inf, x<0 ? '-' : '+')
  else
    converted = false
    if fmt.get_ndig==:exact && fmt.get_approx==:simplify
      
      if x!=0
        q = x.nio_r(Flt.Tolerance(Float::DIG, :sig_decimals))
        if q!=0
          neutral = q.nio_write_neutral(fmt)
          converted = true if neutral.digits.length<=Float::DIG
        end
      end
      
    elsif fmt.get_approx==:exact
      neutral = x.nio_xr.nio_write_neutral(fmt)
      converted = true
    end
    if !converted
      if fmt.get_base==10 && false
        txt = format "%.*e",Float::DECIMAL_DIG-1,x # note that spec. e output precision+1 significant digits
        
        sign = '+'
        if txt[0,1]=='-'
          sign = '-'
          txt = txt[1...txt.length]
        end
        exp = 0
        x_char = fmt.get_exp_char(fmt.get_base)

        exp_i = txt.index(x_char)
        exp_i = txt.index(x_char.downcase) if exp_i===nil
        if exp_i!=nil
          exp = txt[exp_i+1...txt.length].to_i
          txt = txt[0...exp_i]
        end
        
        dec_pos = txt.index '.'
        if dec_pos==nil
          dec_pos = txt.length
        else
          txt[dec_pos]=''
        end
        dec_pos += exp
        neutral.set sign, txt, dec_pos, nil, fmt.get_base_digits(10), true, fmt.get_round
        
        converted = true
      end
    end
    if !converted
      
      sign = x<0 ? '-' : '+'
      f,e = Math.frexp(x)
      if e < Float::MIN_EXP
        # denormalized number
        f = Math.ldexp(f,e-Float::MIN_EXP+Float::MANT_DIG)
        e = Float::MIN_EXP-Float::MANT_DIG
      else
        # normalized number
        f = Math.ldexp(f,Float::MANT_DIG)
        e -= Float::MANT_DIG
      end
      f = f.to_i
      inexact = true

      rounding = case fmt.get_round
      when :even
        :half_even
      when :zero
        :half_down
      when :inf
        :half_up
      when :truncate
        :down
      when :directed_up
        :up
      when :floor
        :floor
      when :ceil
        :ceil
      else
        nil
      end
      

      # Note: it is assumed that fmt will be used for for input too, otherwise
      # rounding should be Float.context.rounding (input rounding for Float) rather than fmt.get_round (output)
      formatter = Flt::Support::Formatter.new(Float::RADIX,  Float::MIN_EXP-Float::MANT_DIG, fmt.get_base)
      formatter.format(x, f, e, rounding, Float::MANT_DIG, fmt.get_all_digits?)
      inexact = formatter.round_up if formatter.round_up.is_a?(Symbol)
      dec_pos, digits = formatter.digits
      txt = ''
      digits.each{|d| txt << fmt.get_base_digits.digit_char(d)}
      neutral.set sign, txt, dec_pos, nil, fmt.get_base_digits, inexact, fmt.get_round
      
    end
  end
  
  return neutral
end

#nio_xrObject

Conversion to Rational preserving the exact value of the number.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nio/rtnlzr.rb', line 36

def nio_xr
  return Rational(self.to_i,1) if self.modulo(1)==0
  if !self.finite?
    return Rational(0,0) if self.nan?
    return self<0 ? Rational(-1,0) : Rational(1,0)
  end

  f,e = Math.frexp(self)

  if e < Float::MIN_EXP
     bits = e+Float::MANT_DIG-Float::MIN_EXP
  else
     bits = [Float::MANT_DIG,e].max
     #return Rational(self.to_i,1) if bits<e
  end
    p = Math.ldexp(f,bits)
    e = bits - e
    if e<Float::MAX_EXP
      q = Math.ldexp(1,e)
    else
      q = Float::RADIX**e
    end
  return Rational(p.to_i,q.to_i)
end