Class Index [+]

Quicksearch

Flt::Support::AuxiliarFunctions

Constants

NBITS_BLOCK
NBITS_LIMIT
NDIGITS_BLOCK
NDIGITS_LIMIT

Public Instance Methods

_nbits(x) click to toggle source

Number of bits in binary representation of the positive integer n, or 0 if n == 0.

      # File lib/flt/support.rb, line 1245
1245:       def _nbits(x)
1246:         raise  TypeError, "The argument to _nbits should be nonnegative." if x < 0
1247:         if x.is_a?(Fixnum)
1248:           return 0 if x==0
1249:           x.to_s(2).length
1250:         elsif x <= NBITS_LIMIT
1251:           Math.frexp(x).last
1252:         else
1253:           n = 0
1254:           while x!=0
1255:             y = x
1256:             x >>= NBITS_BLOCK
1257:             n += NBITS_BLOCK
1258:           end
1259:           n += y.to_s(2).length - NBITS_BLOCK if y!=0
1260:           n
1261:         end
1262:       end
_ndigits(x, b) click to toggle source

Number of base b digits in an integer

      # File lib/flt/support.rb, line 1267
1267:       def _ndigits(x, b)
1268:         raise  TypeError, "The argument to _ndigits should be nonnegative." if x < 0
1269:         return 0 unless x.is_a?(Integer)
1270:         return _nbits(x) if b==2
1271:         if x.is_a?(Fixnum)
1272:           return 0 if x==0
1273:           x.to_s(b).length
1274:         elsif x <= NDIGITS_LIMIT
1275:           (Math.log(x)/Math.log(b)).floor + 1
1276:         else
1277:           n = 0
1278:           block = b**NDIGITS_BLOCK
1279:           while x!=0
1280:             y = x
1281:             x /= block
1282:             n += NDIGITS_BLOCK
1283:           end
1284:           n += y.to_s(b).length - NDIGITS_BLOCK if y!=0
1285:           n
1286:         end
1287:       end
detect_float_rounding() click to toggle source
      # File lib/flt/support.rb, line 1291
1291:       def detect_float_rounding
1292:         x = x = Math::ldexp(1, Float::MANT_DIG+1) # 10000...00*Float::RADIX**2 == Float::RADIX**(Float::MANT_DIG+1)
1293:         y = x + Math::ldexp(1, 2)                 # 00000...01*Float::RADIX**2 == Float::RADIX**2
1294:         h = Float::RADIX/2
1295:         b = h*Float::RADIX
1296:         z = Float::RADIX**2 - 1
1297:         if x + 1 == y
1298:           if (y + 1 == y) && Float::RADIX==10
1299:             :up05
1300:           elsif -x - 1 == -y
1301:             :up
1302:           else
1303:             :ceiling
1304:           end
1305:         else # x + 1 == x
1306:           if x + z == x
1307:             if -x - z == -x
1308:               :down
1309:             else
1310:               :floor
1311:             end
1312:           else # x + z == y
1313:             # round to nearest
1314:             if x + b == x
1315:               if y + b == y
1316:                 :half_down
1317:               else
1318:                 :half_even
1319:               end
1320:             else # x + b == y
1321:               :half_up
1322:             end
1323:           end
1324:         end
1325: 
1326:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.