From 2e6b5ece958d6bfb389d2b0cb9fedce0e733214f Mon Sep 17 00:00:00 2001 From: mrkn Date: Thu, 1 Nov 2012 13:03:53 +0000 Subject: [PATCH] * ext/bigdecimal/bigdecimal.c (BigDecimal_add), test/bigdecimal/test_bigdecimal.rb: need to specify precision for converting Rational and Float. [ruby-core:48045] [Bug #7176] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37406 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++ ext/bigdecimal/bigdecimal.c | 44 ++++++++++++++++++++---------- test/bigdecimal/test_bigdecimal.rb | 4 +++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index ef753a43b9..0bbf7a2956 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Thu Nov 1 21:52:20 2012 Kenta Murata + + * ext/bigdecimal/bigdecimal.c (BigDecimal_add), + test/bigdecimal/test_bigdecimal.rb: + need to specify precision for converting Rational and Float. + [ruby-core:48045] [Bug #7176] + Thu Nov 1 21:42:20 2012 Yusuke Endoh * test/ruby/test_process.rb: Revert r37404. My ubuntu box has diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index e798781b67..8b3fef095a 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -821,23 +821,37 @@ BigDecimal_add(VALUE self, VALUE r) ENTER(5); Real *c, *a, *b; size_t mx; - GUARD_OBJ(a,GetVpValue(self,1)); - b = GetVpValue(r,0); - if(!b) return DoSomeOne(self,r,'+'); + + GUARD_OBJ(a, GetVpValue(self, 1)); + if (TYPE(r) == T_FLOAT) { + b = GetVpValueWithPrec(r, DBL_DIG+1, 1); + } + else if (TYPE(r) == T_RATIONAL) { + b = GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 1); + } + else { + b = GetVpValue(r,0); + } + + if (!b) return DoSomeOne(self,r,'+'); SAVE(b); - if(VpIsNaN(b)) return b->obj; - if(VpIsNaN(a)) return a->obj; - mx = GetAddSubPrec(a,b); + + if (VpIsNaN(b)) return b->obj; + if (VpIsNaN(a)) return a->obj; + + mx = GetAddSubPrec(a, b); if (mx == (size_t)-1L) { - GUARD_OBJ(c,VpCreateRbObject(VpBaseFig() + 1, "0")); - VpAddSub(c, a, b, 1); - } else { - GUARD_OBJ(c,VpCreateRbObject(mx *(VpBaseFig() + 1), "0")); - if(!mx) { - VpSetInf(c,VpGetSign(a)); - } else { - VpAddSub(c, a, b, 1); - } + GUARD_OBJ(c,VpCreateRbObject(VpBaseFig() + 1, "0")); + VpAddSub(c, a, b, 1); + } + else { + GUARD_OBJ(c, VpCreateRbObject(mx * (VpBaseFig() + 1), "0")); + if(!mx) { + VpSetInf(c, VpGetSign(a)); + } + else { + VpAddSub(c, a, b, 1); + } } return ToValue(c); } diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 856281d046..e1f98a28ca 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -587,6 +587,10 @@ class TestBigDecimal < Test::Unit::TestCase a, b = BigDecimal("0.11111").coerce(1.quo(3)) assert_equal(BigDecimal("0." + "3"*a.precs[0]), a) + + assert_nothing_raised(TypeError, '#7176') do + BigDecimal.new('1') + Rational(1) + end end def test_uplus