summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/bigdecimal.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-13 06:15:37 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-13 06:15:37 +0000
commitdc2182aab8c319697b0f4c7f22cb99e5266c43c6 (patch)
tree0a974fce876adc0ddcf4d798f9cecfac2667924e /ext/bigdecimal/bigdecimal.c
parentcc64c2144abbbaf1b0b5fef3bcc4a8ab162cc397 (diff)
* ext/bigdecimal/bigdecimal.c (BigDecimal_sub):
need to specify precision for converting Rational and Float. [ruby-dev:46544] [Bug #7404] * ext/bigdecimal/bigdecimal.c (BigDecimal_mult): ditto. * ext/bigdecimal/bigdecimal.c (BigDecimal_divide): ditto. * ext/bigdecimal/bigdecimal.c (BigDecimal_DoDivmod): ditto. * ext/bigdecimal/bigdecimal.c (BigDecimal_divremain): ditto. * test/bigdecimal/test_bigdecimal.rb: add tests for the above fixes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38801 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal/bigdecimal.c')
-rw-r--r--ext/bigdecimal/bigdecimal.c56
1 files changed, 51 insertions, 5 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 7cd0044..dca54b44 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -894,7 +894,16 @@ BigDecimal_sub(VALUE self, VALUE r)
size_t mx;
GUARD_OBJ(a,GetVpValue(self,1));
- b = GetVpValue(r,0);
+ 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);
@@ -1146,7 +1155,16 @@ BigDecimal_mult(VALUE self, VALUE r)
size_t mx;
GUARD_OBJ(a,GetVpValue(self,1));
- b = GetVpValue(r,0);
+ 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);
@@ -1165,9 +1183,19 @@ BigDecimal_divide(Real **c, Real **res, Real **div, VALUE self, VALUE r)
size_t mx;
GUARD_OBJ(a,GetVpValue(self,1));
- b = GetVpValue(r,0);
+ 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);
+
*div = b;
mx = a->Prec + vabs(a->exponent);
if(mx<b->Prec + vabs(b->exponent)) mx = b->Prec + vabs(b->exponent);
@@ -1230,7 +1258,16 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod)
size_t mx;
GUARD_OBJ(a,GetVpValue(self,1));
- b = GetVpValue(r,0);
+ 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 Qfalse;
SAVE(b);
@@ -1322,7 +1359,16 @@ BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)
Real *f=NULL;
GUARD_OBJ(a,GetVpValue(self,1));
- b = GetVpValue(r,0);
+ 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,rb_intern("remainder"));
SAVE(b);