* numeric.c (flo_round): fix for negative value.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31778 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2011-05-29 06:09:08 +00:00
parent e15b3c83b8
commit a3ffe9698b
5 changed files with 16 additions and 3 deletions

View File

@ -1,3 +1,7 @@
Sun May 29 15:09:05 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* numeric.c (flo_round): fix for negative value.
Sun May 29 02:16:53 2011 KOSAKI Motohiro <kosaki.motohiro@gmail.com> Sun May 29 02:16:53 2011 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* test/net/http/utils.rb (TestNetHTTPUtils#teardown): add nil check. * test/net/http/utils.rb (TestNetHTTPUtils#teardown): add nil check.

View File

@ -1641,7 +1641,7 @@ rb_big_eql(VALUE x, VALUE y)
* Unary minus (returns an integer whose value is 0-big) * Unary minus (returns an integer whose value is 0-big)
*/ */
static VALUE VALUE
rb_big_uminus(VALUE x) rb_big_uminus(VALUE x)
{ {
VALUE z = rb_big_clone(x); VALUE z = rb_big_clone(x);

View File

@ -25,6 +25,8 @@ struct rb_classext_struct {
struct st_table *const_tbl; struct st_table *const_tbl;
}; };
VALUE rb_big_uminus(VALUE x);
#if defined(__cplusplus) #if defined(__cplusplus)
#if 0 #if 0
{ /* satisfy cc-mode */ { /* satisfy cc-mode */

View File

@ -97,6 +97,7 @@ round(double x)
} }
#endif #endif
static VALUE fix_uminus(VALUE num);
static VALUE fix_mul(VALUE x, VALUE y); static VALUE fix_mul(VALUE x, VALUE y);
static VALUE int_pow(long x, unsigned long y); static VALUE int_pow(long x, unsigned long y);
@ -1504,10 +1505,15 @@ flo_round(int argc, VALUE *argv, VALUE num)
} }
else { else {
if (ndigits < 0) { if (ndigits < 0) {
if (fabs(number) < f) return INT2FIX(0); double absnum = fabs(number);
if (absnum < f) return INT2FIX(0);
if (!FIXABLE(number)) { if (!FIXABLE(number)) {
VALUE f10 = int_pow(10, -ndigits); VALUE f10 = int_pow(10, -ndigits);
num = rb_big_idiv(rb_dbl2big(number), f10); VALUE n10 = f10;
if (number < 0) {
f10 = FIXNUM_P(f10) ? fix_uminus(f10) : rb_big_uminus(f10);
}
num = rb_big_idiv(rb_dbl2big(absnum), n10);
return FIXNUM_P(num) ? fix_mul(num, f10) : rb_big_mul(num, f10); return FIXNUM_P(num) ? fix_mul(num, f10) : rb_big_mul(num, f10);
} }
number /= f; number /= f;

View File

@ -322,6 +322,7 @@ class TestFloat < Test::Unit::TestCase
assert_equal(11100.0, 11111.1.round(-2)) assert_equal(11100.0, 11111.1.round(-2))
assert_equal(10**300, 1.1e300.round(-300)) assert_equal(10**300, 1.1e300.round(-300))
assert_equal(-10**300, -1.1e300.round(-300))
end end
VS = [ VS = [