diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-31 15:09:38 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-31 15:09:38 +0000 |
commit | d7058e10dc730b101558d21c343d784aaaf98d3f (patch) | |
tree | a037ef0dbf4cad2b48d4239581cb0a5b310c1a71 | |
parent | 8b709310284d8801b88faa74af1284ec367f56fb (diff) | |
download | ruby-d7058e10dc730b101558d21c343d784aaaf98d3f.tar.gz |
* ext/bigdecimal/bigdecimal.c (BigDecimal_coerce): support coerce with a
Rational. The precision used for instantiate a BigDecimal from the
given Rational is obtained from the receiver BigDecimal.
* test/bigdecimal/test_bigdecimal.rb (test_coerce): add a test for the
above change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31868 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 13 | ||||
-rw-r--r-- | test/bigdecimal/test_bigdecimal.rb | 6 |
3 files changed, 26 insertions, 2 deletions
@@ -1,3 +1,12 @@ +Tue Jun 1 00:07:00 2011 Kenta Murata <mrkn@mrkn.jp> + + * ext/bigdecimal/bigdecimal.c (BigDecimal_coerce): support coerce with a + Rational. The precision used for instantiate a BigDecimal from the + given Rational is obtained from the receiver BigDecimal. + + * test/bigdecimal/test_bigdecimal.rb (test_coerce): add a test for the + above change. + Tue May 31 23:49:08 2011 Tadayoshi Funaba <tadf@dotrb.org> * ext/date/date_core.c (offset_to_sec): fixed invalid validation. diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index cd960f4b87..571bf41461 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -675,12 +675,21 @@ BigDecimal_coerce(VALUE self, VALUE other) ENTER(2); VALUE obj; Real *b; + if (TYPE(other) == T_FLOAT) { obj = rb_assoc_new(other, BigDecimal_to_f(self)); - } else { - GUARD_OBJ(b,GetVpValue(other,1)); + } + else { + if (TYPE(other) == T_RATIONAL) { + Real* pv = DATA_PTR(self); + GUARD_OBJ(b, GetVpValueWithPrec(other, pv->Prec*VpBaseFig(), 1)); + } + else { + GUARD_OBJ(b, GetVpValue(other, 1)); + } obj = rb_assoc_new(b->obj, self); } + return obj; } diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index dd75dbfd64..63873d2b7c 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -485,6 +485,12 @@ class TestBigDecimal < Test::Unit::TestCase assert_instance_of(Float, a) assert_instance_of(Float, b) assert_equal(2, 1 + BigDecimal.new("1"), '[ruby-core:25697]') + + a, b = BigDecimal("1").coerce(1.quo(10)) + assert_equal(BigDecimal("0.1"), a, '[ruby-core:34318]') + + a, b = BigDecimal("0.11111").coerce(1.quo(3)) + assert_equal(BigDecimal("0." + "3"*a.precs[0]), a) end def test_uplus |