diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-01 13:03:53 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-01 13:03:53 +0000 |
commit | 2e6b5ece958d6bfb389d2b0cb9fedce0e733214f (patch) | |
tree | 346b8d2e649faa167ca8ebdefb77fc02fd0b6855 /ext/bigdecimal/bigdecimal.c | |
parent | a5fc96a5c12b347c9b78949810bca245f48a1bec (diff) | |
download | ruby-2e6b5ece958d6bfb389d2b0cb9fedce0e733214f.tar.gz |
* 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
Diffstat (limited to 'ext/bigdecimal/bigdecimal.c')
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 44 |
1 files changed, 29 insertions, 15 deletions
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); } |