Permalink
Browse files

* 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
  • Loading branch information...
1 parent cc64c21 commit dc2182aab8c319697b0f4c7f22cb99e5266c43c6 @mrkn mrkn committed Jan 13, 2013
Showing with 107 additions and 5 deletions.
  1. +16 −0 ChangeLog
  2. +51 −5 ext/bigdecimal/bigdecimal.c
  3. +40 −0 test/bigdecimal/test_bigdecimal.rb
View
@@ -1,3 +1,19 @@
+Sun Jan 13 15:00:00 2013 Kenta Murata <mrkn@mrkn.jp>
+
+ * 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 <ruby-core@marc-andre.ca>
* lib/matrix/eigenvalue_decomposition: Fix eigensystem with complex
@@ -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);
@@ -652,6 +652,14 @@ def test_sub
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 @@ def test_mult
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 @@ def test_div
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 @@ def test_mod
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 @@ def test_remainder
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))

0 comments on commit dc2182a

Please sign in to comment.