nio 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,17 @@
1
- == 0.2.0 2007-09-22
2
-
3
- * Initial release
1
+ == 0.2.1 2007-12-15
2
+
3
+ * Minor enhancements
4
+ - The RepDec maximum number of digits can now be changed, and the default
5
+ value has been incremented to 5000 digits (was 2048). With the new value
6
+ the minimum value of a IEEE 754 extended number can now be shown as
7
+ different from zero.
8
+ - Performance optimizations in RepDec#setQ
9
+ * Bug corrections
10
+ - Fixed a problem in the formatted output of BigDecimals which affected large numbers.
11
+ - Fix minor problem with nextfloat
12
+
13
+ == 0.2.0 2007-09-22
14
+
15
+ * Initial release
4
16
  - The Nio library has been extracted from and old project
5
- and packages as a Gem.
17
+ and packaged as a Gem.
@@ -26,10 +26,13 @@ class Float
26
26
 
27
27
  x = 1.0
28
28
  _bits_ = 0
29
- begin
29
+ while 1!=x+1
30
30
  _bits_ += 1
31
31
  x /= 2
32
- end while 1!=x+1
32
+ end
33
+ if ((1.0+2*x)-1.0)>2*x
34
+ _bits_ -= 1
35
+ end
33
36
 
34
37
  # Number of RADIX-base digits of precision in a Float
35
38
  MANT_DIG = _bits_
@@ -522,11 +522,14 @@ module Nio
522
522
  # - <tt>:round</tt> rounding mode applied to conversions
523
523
  # (this is relevant for both input and output). It must be one of:
524
524
  # [<tt>:inf</tt>]
525
- # rounds towards infinite; 1.5 is rounded to 2, -1.5 to -2
525
+ # rounds to nearest with ties toward infinite;
526
+ # 1.5 is rounded to 2, -1.5 to -2
526
527
  # [<tt>:zero</tt>]
527
- # rounds towards zero; 1.5 is rounded to 1, -1.5 to 2
528
+ # rounds to nearest with ties toward zero;
529
+ # 1.5 is rounded to 1, -1.5 to 2
528
530
  # [<tt>:even</tt>]
529
- # rounds to the nearest even digit 1.5 rounds to 2, 2.5 to 2
531
+ # rounds to the nearest with ties toward an even digit;
532
+ # 1.5 rounds to 2, 2.5 to 2
530
533
  # - <tt>:approx</tt> approximate mode
531
534
  # [<tt>:only_sig</tt>]
532
535
  # (the default) treats the value as an approximation and only
@@ -1263,8 +1266,7 @@ module Nio
1263
1266
  end
1264
1267
 
1265
1268
  def ratio_float(u,v,k,round_mode,beta=Float::RADIX,n=Float::MANT_DIG)
1266
- q = u.div v
1267
- r = u-q*v
1269
+ q,r = u.divmod(v)
1268
1270
  v_r = v-r
1269
1271
  z = Math.ldexp(q,k)
1270
1272
  if r<v_r
@@ -1278,14 +1280,20 @@ module Nio
1278
1280
  end
1279
1281
  end
1280
1282
 
1283
+ # valid only for non-negative x
1281
1284
  def nextfloat(x)
1282
1285
  f,e = Math.frexp(x)
1283
1286
  e = Float::MIN_EXP if f==0
1284
1287
  e = [Float::MIN_EXP,e].max
1285
1288
  dx = Math.ldexp(1,e-Float::MANT_DIG) #Math.ldexp(Math.ldexp(1.0,-Float::MANT_DIG),e)
1286
- x + dx
1289
+ if f==(1.0 - Math.ldexp(1,-Float::MANT_DIG))
1290
+ x + dx*2
1291
+ else
1292
+ x + dx
1293
+ end
1287
1294
  end
1288
1295
 
1296
+ # valid only for non-negative x
1289
1297
  def prevfloat(x)
1290
1298
  f,e = Math.frexp(x)
1291
1299
  e = Float::MIN_EXP if f==0
@@ -1316,7 +1324,8 @@ module Nio
1316
1324
  roundh = true
1317
1325
  else
1318
1326
  # here we don't assume any rounding in the floating point numbers
1319
- # the result is valid for any rounding
1327
+ # the result is valid for any rounding but may produce more digits
1328
+ # than stricly necessary for specifica rounding modes.
1320
1329
  roundl = false
1321
1330
  roundh = false
1322
1331
  end
@@ -1444,7 +1453,8 @@ module Nio
1444
1453
  roundh = true
1445
1454
  else
1446
1455
  # here we don't assume any rounding in the floating point numbers
1447
- # the result is valid for any rounding
1456
+ # the result is valid for any rounding but may produce more digits
1457
+ # than stricly necessary for specifica rounding modes.
1448
1458
  roundl = false
1449
1459
  roundh = false
1450
1460
  end
@@ -1831,6 +1841,7 @@ class BigDecimal
1831
1841
  dec_pos += exp
1832
1842
  neutral.set sign, txt, dec_pos, nil, fmt.get_base_digits(10), true, fmt.get_round
1833
1843
 
1844
+ converted = true
1834
1845
  end
1835
1846
  end
1836
1847
  if !converted
@@ -74,7 +74,7 @@ module Nio
74
74
  @digits = DigitsDef.new
75
75
  @digits_defined = false
76
76
 
77
- @max_d = 2048
77
+ @max_d = 5000
78
78
 
79
79
  end
80
80
  attr_accessor :begin_rep, :end_rep, :auto_rep, :dec_sep, :grp_sep, :grp, :max_d
@@ -379,6 +379,17 @@ module Nio
379
379
  # return !(self==c);
380
380
  #end
381
381
 
382
+ # Change the maximum number of digits that RepDec objects
383
+ # can handle.
384
+ def RepDec.maximum_number_of_digits=(n)
385
+ @max_d = [n,2048].max
386
+ end
387
+ # Return the maximum number of digits that RepDec objects
388
+ # can handle.
389
+ def RepDec.maximum_number_of_digits
390
+ @max_d
391
+ end
392
+
382
393
  def setQ(x,y, opt=DEF_OPT)
383
394
  @radix = opt.digits.radix if opt.digits_defined?
384
395
  xy_sign = x==0 ? 0 : x<0 ? -1 : +1;
@@ -399,7 +410,7 @@ module Nio
399
410
  return self
400
411
  end
401
412
 
402
- k = [];
413
+ k = {};
403
414
  @ip = x.div(y) #x/y;
404
415
  x -= @ip*y;
405
416
  i = 0;
@@ -407,12 +418,12 @@ module Nio
407
418
 
408
419
  max_d = opt.max_d
409
420
  while x>0 && @rep_i==nil && (max_d<=0 || i<max_d)
410
- @rep_i = k.index(x)
421
+ @rep_i = k[x]
411
422
  if @rep_i.nil? then
412
- k.push x;
423
+ k[x] = i;
413
424
  x *= @radix
414
- @d.push x.div(y) # x/y;
415
- x-= @d[i]*y;
425
+ d,x = x.divmod(y)
426
+ @d.push d
416
427
  i += 1;
417
428
  end
418
429
  end
@@ -2,7 +2,7 @@ module Nio #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -152,7 +152,10 @@ class TestFmt < Test::Unit::TestCase
152
152
  x = BigDecimal(x.to_s)
153
153
  assert_equal x,BigDecimal.nio_read(x.nio_write(fmt),fmt)
154
154
  end
155
-
155
+ assert_equal "1E500",BigDec('1E500').nio_write
156
+ assert_equal "1E-500",BigDec('1E-500').nio_write
157
+ assert_equal "-1E500",BigDec('-1E500').nio_write
158
+ assert_equal "-1E-500",BigDec('-1E-500').nio_write
156
159
  end
157
160
 
158
161
  def test_Rational
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: nio
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2007-09-23 00:00:00 +02:00
6
+ version: 0.2.1
7
+ date: 2007-12-15 00:00:00 +01:00
8
8
  summary: Numeric input/output
9
9
  require_paths:
10
10
  - lib