summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-26 15:49:47 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-26 15:49:47 +0000
commitdca0b4177706c2cd9cda0d38af1b82752982e158 (patch)
tree127949dd094e102c5fde0dd73066c2e397134f34
parent079fb8d4c369cfc7d450d6e4d02b112596dcb5ee (diff)
* ext/bigdecimal/lib/bigdecimal/util.rb (Rational#to_d):
zero or negative precision is error. fixes #5098. [ruby-dev:44210] * test/bigdecimal/test_bigdecimal_util.rb: add test for the above change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32680 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog9
-rw-r--r--ext/bigdecimal/lib/bigdecimal/util.rb10
-rw-r--r--test/bigdecimal/test_bigdecimal_util.rb15
3 files changed, 29 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 93ab012..d4dc297 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Wed Jul 27 00:50: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]
+
+ * test/bigdecimal/test_bigdecimal_util.rb: add test for the above
+ change.
+
Wed Jul 27 00:48:00 2011 Kenta Murata <mrkn@mrkn.jp>
* ext/bigdecimal/lib/bigdecimal/util.rb (Float#to_d): modified for
diff --git a/ext/bigdecimal/lib/bigdecimal/util.rb b/ext/bigdecimal/lib/bigdecimal/util.rb
index 1e4bbbb..b27e64c 100644
--- a/ext/bigdecimal/lib/bigdecimal/util.rb
+++ b/ext/bigdecimal/lib/bigdecimal/util.rb
@@ -95,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
index 87b4f20..72342b9 100644
--- a/test/bigdecimal/test_bigdecimal_util.rb
+++ b/test/bigdecimal/test_bigdecimal_util.rb
@@ -25,4 +25,19 @@ class TestBigDecimalUtil < Test::Unit::TestCase
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