diff options
-rw-r--r-- | ChangeLog | 28 | ||||
-rw-r--r-- | ext/bigdecimal/lib/bigdecimal/util.rb | 39 | ||||
-rw-r--r-- | test/bigdecimal/test_bigdecimal_util.rb | 43 |
3 files changed, 103 insertions, 7 deletions
@@ -1,3 +1,31 @@ +Wed Jul 27 01:13:00 2011 Kenta Murata <mrkn@mrkn.jp> + + * ext/bigdecimal/lib/bigdecimal/util.rb (Rational#to_d): + zero or negative precision is error. fixes #5098. + [ruby-dev:44210] + + * ext/bigdecimal/lib/bigdecimal/util.rb (Float#to_d): modified for + specifying precision. fixes #5098. [ruby-dev:44210] + + * ext/bigdecimal/lib/bigdecimal/util.rb (Integer#to_d): added + for symmetry to BigDecimal() function with an Integer. + fixes #5098. [ruby-dev:44210] + + * ext/bigdecimal/lib/bigdecimal/util.rb (BigDecimal#to_d): added + for adapting other Numeric subclasses. [ruby-dev:44245] + + * test/bigdecimal/test_bigdecimal_util.rb: add tests for the above + changes. + +Wed Jul 27 00:54:38 2011 Kenta Murata <mrkn@mrkn.jp> + + * bigdecimal/bigdecimal.c (VpDup) a new function for duplicating + a BigDecimal. + + * bigdecimal/bigdecimal.c (BigDecimal_new): support generating a new + BigDecimal from another BigDecimal using BigDecimal global function + or constructor. [ruby-dev:44245] + Mon Jul 25 22:24:09 2011 Hiroshi Nakamura <nahi@ruby-lang.org> * backport r32666 from trunk. diff --git a/ext/bigdecimal/lib/bigdecimal/util.rb b/ext/bigdecimal/lib/bigdecimal/util.rb index 6e1697be88..b27e64c511 100644 --- a/ext/bigdecimal/lib/bigdecimal/util.rb +++ b/ext/bigdecimal/lib/bigdecimal/util.rb @@ -1,3 +1,20 @@ +class Integer < Numeric + # call-seq: + # int.to_d -> bigdecimal + # + # Convert +int+ to a BigDecimal and return it. + # + # require 'bigdecimal' + # require 'bigdecimal/util' + # + # 42.to_d + # # => #<BigDecimal:1008ef070,'0.42E2',9(36)> + # + def to_d + BigDecimal(self) + end +end + class Float < Numeric # call-seq: # flt.to_d -> bigdecimal @@ -10,8 +27,8 @@ class Float < Numeric # 0.5.to_d # # => #<BigDecimal:1dc69e0,'0.5E0',9(18)> # - def to_d - BigDecimal(self.to_s) + def to_d(precision=nil) + BigDecimal(self, precision || Float::DIG+1) end end @@ -54,6 +71,14 @@ class BigDecimal < Numeric i + "." + ("0"*(-z)) + f end end + + # call-seq: + # a.to_d -> bigdecimal + # + # Returns self. + def to_d + self + end end class Rational < Numeric @@ -70,11 +95,11 @@ class Rational < Numeric # # => #<BigDecimal:1a52bd8,'0.3142857142 8571427937 0154144999 105E1',45(63)> # r.to_d(3) # # => #<BigDecimal:1a44d08,'0.314E1',18(36)> - def to_d(nFig=0) - num = self.numerator.to_s - if nFig<=0 - nFig = BigDecimal.double_fig*2+1 + def to_d(precision) + if precision <= 0 + raise ArgumentError, "negative precision" end - BigDecimal.new(num).div(self.denominator,nFig) + num = self.numerator + BigDecimal(num).div(self.denominator, precision) end end diff --git a/test/bigdecimal/test_bigdecimal_util.rb b/test/bigdecimal/test_bigdecimal_util.rb new file mode 100644 index 0000000000..72342b922f --- /dev/null +++ b/test/bigdecimal/test_bigdecimal_util.rb @@ -0,0 +1,43 @@ +require_relative "testbase" + +require 'bigdecimal/util' + +class TestBigDecimalUtil < Test::Unit::TestCase + def test_BigDecimal_to_d + x = BigDecimal(1) + assert_same(x, x.to_d) + end + + def test_Integer_to_d + assert_equal(BigDecimal(1), 1.to_d) + assert_equal(BigDecimal(2<<100), (2<<100).to_d) + end + + def test_Float_to_d_without_precision + delta = 1.0/10**(Float::DIG + 1) + assert_in_delta(BigDecimal(0.5, Float::DIG+1), 0.5.to_d, delta) + assert_in_delta(BigDecimal(355.0/113.0, Float::DIG+1), (355.0/113.0).to_d, delta) + end + + def test_Float_to_d_with_precision + digits = 5 + delta = 1.0/10**(digits) + assert_in_delta(BigDecimal(0.5, 5), 0.5.to_d(digits), delta) + assert_in_delta(BigDecimal(355.0/113.0, 5), (355.0/113.0).to_d(digits), delta) + end + + def test_Rational_to_d + digits = 100 + delta = 1.0/10**(digits) + assert_in_delta(BigDecimal(1.quo(2), digits), 1.quo(2).to_d(digits), delta) + assert_in_delta(BigDecimal(355.quo(113), digits), 355.quo(113).to_d(digits), delta) + end + + def test_Rational_to_d_with_zero_precision + assert_raise(ArgumentError) { 355.quo(113).to_d(0) } + end + + def test_Rational_to_d_with_negative_precision + assert_raise(ArgumentError) { 355.quo(113).to_d(-42) } + end +end |