1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
mrkn 2013-01-13 06:15:37 +00:00
parent cc64c2144a
commit dc2182aab8
3 changed files with 107 additions and 5 deletions

View file

@ -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

View file

@ -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);

View file

@ -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))