diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-29 06:09:08 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-29 06:09:08 +0000 |
commit | a3ffe9698b8a8dff25d94b3e7e490f0a01684bfe (patch) | |
tree | 6521135a57fcc5508ec052f8725114af7de0b477 | |
parent | e15b3c83b880aa2c2f07378095d1eba0214ca468 (diff) |
* 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
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | bignum.c | 2 | ||||
-rw-r--r-- | internal.h | 2 | ||||
-rw-r--r-- | numeric.c | 10 | ||||
-rw-r--r-- | test/ruby/test_float.rb | 1 |
5 files changed, 16 insertions, 3 deletions
@@ -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> * test/net/http/utils.rb (TestNetHTTPUtils#teardown): add nil check. @@ -1641,7 +1641,7 @@ rb_big_eql(VALUE x, VALUE y) * Unary minus (returns an integer whose value is 0-big) */ -static VALUE +VALUE rb_big_uminus(VALUE x) { VALUE z = rb_big_clone(x); diff --git a/internal.h b/internal.h index ca4220a841..4e39e47f75 100644 --- a/internal.h +++ b/internal.h @@ -25,6 +25,8 @@ struct rb_classext_struct { struct st_table *const_tbl; }; +VALUE rb_big_uminus(VALUE x); + #if defined(__cplusplus) #if 0 { /* satisfy cc-mode */ @@ -97,6 +97,7 @@ round(double x) } #endif +static VALUE fix_uminus(VALUE num); static VALUE fix_mul(VALUE x, VALUE y); static VALUE int_pow(long x, unsigned long y); @@ -1504,10 +1505,15 @@ flo_round(int argc, VALUE *argv, VALUE num) } else { if (ndigits < 0) { - if (fabs(number) < f) return INT2FIX(0); + double absnum = fabs(number); + if (absnum < f) return INT2FIX(0); if (!FIXABLE(number)) { 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); } number /= f; diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 2e879bdd2f..fb37d73831 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -322,6 +322,7 @@ class TestFloat < Test::Unit::TestCase assert_equal(11100.0, 11111.1.round(-2)) assert_equal(10**300, 1.1e300.round(-300)) + assert_equal(-10**300, -1.1e300.round(-300)) end VS = [ |