summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-26 15:48:25 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-26 15:48:25 +0000
commit079fb8d4c369cfc7d450d6e4d02b112596dcb5ee (patch)
tree96248be4d9cb5747abd8dde11d35c1cde1b5f40b
parent2fd6b26620b1f9e25f204f05524b8086f7c8d23e (diff)
* ext/bigdecimal/lib/bigdecimal/util.rb (Float#to_d): modified for
specifying precision. 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@32679 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--ext/bigdecimal/lib/bigdecimal/util.rb4
-rw-r--r--test/bigdecimal/test_bigdecimal_util.rb13
3 files changed, 23 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 0e285f8..93ab012 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Wed Jul 27 00:48:00 2011 Kenta Murata <mrkn@mrkn.jp>
+
+ * ext/bigdecimal/lib/bigdecimal/util.rb (Float#to_d): modified for
+ specifying precision. fixes #5098. [ruby-dev:44210]
+
+ * test/bigdecimal/test_bigdecimal_util.rb: add test for the above
+ change.
+
Wed Jul 27 00:45:00 2011 Kenta Murata <mrkn@mrkn.jp>
* ext/bigdecimal/lib/bigdecimal/util.rb (Integer#to_d): added
diff --git a/ext/bigdecimal/lib/bigdecimal/util.rb b/ext/bigdecimal/lib/bigdecimal/util.rb
index 1ec39a3..1e4bbbb 100644
--- a/ext/bigdecimal/lib/bigdecimal/util.rb
+++ b/ext/bigdecimal/lib/bigdecimal/util.rb
@@ -27,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
diff --git a/test/bigdecimal/test_bigdecimal_util.rb b/test/bigdecimal/test_bigdecimal_util.rb
index 5f03b73..87b4f20 100644
--- a/test/bigdecimal/test_bigdecimal_util.rb
+++ b/test/bigdecimal/test_bigdecimal_util.rb
@@ -12,4 +12,17 @@ class TestBigDecimalUtil < Test::Unit::TestCase
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
end