diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 44 | ||||
-rw-r--r-- | test/bigdecimal/test_bigdecimal.rb | 4 |
3 files changed, 40 insertions, 15 deletions
@@ -1,3 +1,10 @@ +Thu Nov 1 21:52:20 2012 Kenta Murata <mrkn@mrkn.jp> + + * 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 <mame@tsg.ne.jp> * 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 |