diff --git a/ChangeLog b/ChangeLog index aa9714ba0e..9b47ac4145 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,7 +18,7 @@ Tue Jun 5 13:17:11 2007 Nobuyoshi Nakada pack.c, re.c, thread.c, thread_win32.ci, vm.c, vm_dump.c: fixed indentation. -Mon Jun 04 21:15:45 2007 NARUSE, Yui +Mon Jun 4 21:15:45 2007 NARUSE, Yui * lib/json.rb, lib/json, ext/json, test/json: import JSON library. @@ -26,6 +26,10 @@ Mon Jun 04 21:15:45 2007 NARUSE, Yui * ext/nkf: import nkf.c rev:1.124 Support CP10001. +Mon Jun 4 20:52:58 2007 Yukihiro Matsumoto + + * numeric.c (int_round): should not just truncate. + Sat Jun 2 16:48:55 2007 Koichi Sasada * cont.c (Fiber#pass): rename to Fiber#yield. Block parameter diff --git a/numeric.c b/numeric.c index 5f9b98e413..ceaba65fee 100644 --- a/numeric.c +++ b/numeric.c @@ -2308,22 +2308,6 @@ int_pow(long x, unsigned long y) return LONG2NUM(z); } -static VALUE -int_round(int argc, VALUE* argv, VALUE num) -{ - VALUE nd; - int ndigits; - - if (argc == 0) return num; - rb_scan_args(argc, argv, "1", &nd); - ndigits = NUM2INT(nd); - if (ndigits > 0) { - return rb_Float(num); - } - ndigits = -ndigits; - return rb_funcall(num, '-', 1, rb_funcall(num, '%', 1, int_pow(10, ndigits))); -} - /* * call-seq: * fix ** other => Numeric @@ -2911,6 +2895,31 @@ int_dotimes(VALUE num) return num; } +static VALUE +int_round(int argc, VALUE* argv, VALUE num) +{ + VALUE n, f, h, r; + int ndigits; + + if (argc == 0) return num; + if (FIXNUM_P(num)) return num_round(argc, argv, num); + + rb_scan_args(argc, argv, "1", &n); + ndigits = NUM2INT(n); + if (ndigits > 0) { + return rb_Float(num); + } + ndigits = -ndigits; + f = int_pow(10, ndigits); + 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))) { + n = rb_funcall(n, '+', 1, f); + } + return n; +} + /* * call-seq: * fix.zero? => true or false