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

math.c: fix for Bignum argument

* math.c (math_log, math_log2, math_log10): fix for Bignum argument.
  numbits should be add only when right shifted.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43080 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-09-28 14:25:59 +00:00
parent f450dede0e
commit 202cc8e615
3 changed files with 20 additions and 3 deletions

View file

@ -1,3 +1,8 @@
Sat Sep 28 23:25:56 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* math.c (math_log, math_log2, math_log10): fix for Bignum argument.
numbits should be add only when right shifted.
Sat Sep 28 14:30:29 2013 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* test/dl/test_base.rb: {libc, libm} detection now handle GNU/Hurd

15
math.c
View file

@ -442,7 +442,7 @@ math_log(int argc, VALUE *argv)
{
VALUE x, base;
double d0, d;
size_t numbits = 0;
size_t numbits;
rb_scan_args(argc, argv, "11", &x, &base);
@ -451,6 +451,9 @@ math_log(int argc, VALUE *argv)
numbits -= DBL_MANT_DIG;
x = rb_big_rshift(x, SIZET2NUM(numbits));
}
else {
numbits = 0;
}
Need_Float(x);
d0 = RFLOAT_VALUE(x);
@ -501,13 +504,16 @@ static VALUE
math_log2(VALUE obj, VALUE x)
{
double d0, d;
size_t numbits = 0;
size_t numbits;
if (RB_BIGNUM_TYPE_P(x) && RBIGNUM_POSITIVE_P(x) &&
DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
numbits -= DBL_MANT_DIG;
x = rb_big_rshift(x, SIZET2NUM(numbits));
}
else {
numbits = 0;
}
Need_Float(x);
d0 = RFLOAT_VALUE(x);
@ -540,13 +546,16 @@ static VALUE
math_log10(VALUE obj, VALUE x)
{
double d0, d;
size_t numbits = 0;
size_t numbits;
if (RB_BIGNUM_TYPE_P(x) && RBIGNUM_POSITIVE_P(x) &&
DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
numbits -= DBL_MANT_DIG;
x = rb_big_rshift(x, SIZET2NUM(numbits));
}
else {
numbits = 0;
}
Need_Float(x);
d0 = RFLOAT_VALUE(x);

View file

@ -136,6 +136,7 @@ class TestMath < Test::Unit::TestCase
check(0, Math.log(1, 10))
check(1, Math.log(10, 10))
check(2, Math.log(100, 10))
check(Math.log(2.0 ** 64), Math.log(1 << 64))
assert_equal(1.0/0, Math.log(1.0/0))
assert_nothing_raised { assert_infinity(-Math.log(+0.0)) }
assert_nothing_raised { assert_infinity(-Math.log(-0.0)) }
@ -147,6 +148,7 @@ class TestMath < Test::Unit::TestCase
check(0, Math.log2(1))
check(1, Math.log2(2))
check(2, Math.log2(4))
check(Math.log2(2.0 ** 64), Math.log2(1 << 64))
assert_equal(1.0/0, Math.log2(1.0/0))
assert_nothing_raised { assert_infinity(-Math.log2(+0.0)) }
assert_nothing_raised { assert_infinity(-Math.log2(-0.0)) }
@ -157,6 +159,7 @@ class TestMath < Test::Unit::TestCase
check(0, Math.log10(1))
check(1, Math.log10(10))
check(2, Math.log10(100))
check(Math.log10(2.0 ** 64), Math.log10(1 << 64))
assert_equal(1.0/0, Math.log10(1.0/0))
assert_nothing_raised { assert_infinity(-Math.log10(+0.0)) }
assert_nothing_raised { assert_infinity(-Math.log10(-0.0)) }