diff --git a/ChangeLog b/ChangeLog index 7d919d23a7..dc3abec798 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Sun Jan 13 15:00:00 2013 Kenta Murata + + * 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. + Sun Jan 13 14:48:55 2013 Marc-Andre Lafortune * lib/matrix/eigenvalue_decomposition: Fix eigensystem with complex diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 7cd0044532..dca54b44db 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(mxPrec + 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); diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 51d2e8f309..07c79a8874 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -652,6 +652,14 @@ class TestBigDecimal < Test::Unit::TestCase assert_equal(BigDecimal.new((2**100-1).to_s), x - 1) end + def test_sub_with_float + assert_kind_of(BigDecimal, BigDecimal.new("3") - 1.0) + end + + def test_sub_with_rational + assert_kind_of(BigDecimal, BigDecimal.new("3") - 1.quo(3)) + end + def test_mult x = BigDecimal.new((2**100).to_s) assert_equal(BigDecimal.new((2**100 * 3).to_s), (x * 3).to_i) @@ -660,6 +668,14 @@ class TestBigDecimal < Test::Unit::TestCase assert_equal(BigDecimal.new((2**200).to_s), (x * x).to_i) end + def test_mult_with_float + assert_kind_of(BigDecimal, BigDecimal.new("3") * 1.5) + end + + def test_mult_with_rational + assert_kind_of(BigDecimal, BigDecimal.new("3") * 1.quo(3)) + end + def test_div x = BigDecimal.new((2**100).to_s) assert_equal(BigDecimal.new((2**100 / 3).to_s), (x / 3).to_i) @@ -669,6 +685,14 @@ class TestBigDecimal < Test::Unit::TestCase assert_equal(-2, BigDecimal.new("2") / -1) end + def test_div_with_float + assert_kind_of(BigDecimal, BigDecimal.new("3") / 1.5) + end + + def test_div_with_rational + assert_kind_of(BigDecimal, BigDecimal.new("3") / 1.quo(3)) + end + def test_mod x = BigDecimal.new((2**100).to_s) assert_equal(1, x % 3) @@ -677,6 +701,14 @@ class TestBigDecimal < Test::Unit::TestCase assert_equal(-1, (-x) % -3) end + def test_mod_with_float + assert_kind_of(BigDecimal, BigDecimal.new("3") % 1.5) + end + + def test_mod_with_rational + assert_kind_of(BigDecimal, BigDecimal.new("3") % 1.quo(3)) + end + def test_remainder x = BigDecimal.new((2**100).to_s) assert_equal(1, x.remainder(3)) @@ -685,6 +717,14 @@ class TestBigDecimal < Test::Unit::TestCase assert_equal(-1, (-x).remainder(-3)) end + def test_remainder_with_float + assert_kind_of(BigDecimal, BigDecimal.new("3").remainder(1.5)) + end + + def test_remainder_with_rational + assert_kind_of(BigDecimal, BigDecimal.new("3").remainder(1.quo(3))) + end + def test_divmod x = BigDecimal.new((2**100).to_s) assert_equal([(x / 3).floor, 1], x.divmod(3))