From 7a3f54edb1656a7ad068c54e4084123beef2aafa Mon Sep 17 00:00:00 2001 From: marcandre Date: Wed, 24 Aug 2011 23:04:50 +0000 Subject: * backport r33060, 33061 from trunk * numeric.c (int_round): Fix Integer#round [ruby-core:39096] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@33062 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- numeric.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index 46b2e8cf17..ab890c6153 100644 --- a/numeric.c +++ b/numeric.c @@ -1602,7 +1602,7 @@ num_ceil(VALUE num) * num.round([ndigits]) -> integer or float * * Rounds num to a given precision in decimal digits (default 0 digits). - * Precision may be negative. Returns a floating point number when ndigits + * Precision may be negative. Returns a floating point number when ndigits * is more than zero. Numeric implements this by converting itself * to a Float and invoking Float#round. */ @@ -2031,7 +2031,6 @@ rb_num2ull(VALUE val) * int.to_int -> integer * int.floor -> integer * int.ceil -> integer - * int.round -> integer * int.truncate -> integer * * As int is already an Integer, all these @@ -3283,7 +3282,7 @@ int_dotimes(VALUE num) /* * call-seq: - * num.round([ndigits]) -> integer or float + * int.round([ndigits]) -> integer or float * * Rounds flt to a given precision in decimal digits (default 0 digits). * Precision may be negative. Returns a floating point number when +ndigits+ @@ -3299,6 +3298,7 @@ int_round(int argc, VALUE* argv, VALUE num) { VALUE n, f, h, r; int ndigits; + ID op; if (argc == 0) return num; rb_scan_args(argc, argv, "1", &n); @@ -3325,7 +3325,8 @@ int_round(int argc, VALUE* argv, VALUE num) h = rb_funcall(f, '/', 1, INT2FIX(2)); r = rb_funcall(num, '%', 1, f); n = rb_funcall(num, '-', 1, r); - if (!RTEST(rb_funcall(r, '<', 1, h))) { + op = RTEST(rb_funcall(num, '<', 1, INT2FIX(0))) ? rb_intern("<=") : '<'; + if (!RTEST(rb_funcall(r, op, 1, h))) { n = rb_funcall(n, '+', 1, f); } return n; -- cgit v1.2.3