зеркало из https://github.com/github/ruby.git
* numeric.c (int_round): should not just truncate.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12437 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
3c17590061
Коммит
931614a341
|
@ -18,7 +18,7 @@ Tue Jun 5 13:17:11 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
|||
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 <naruse@ruby-lang.org>
|
||||
Mon Jun 4 21:15:45 2007 NARUSE, Yui <naruse@ruby-lang.org>
|
||||
|
||||
* 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 <naruse@ruby-lang.org>
|
|||
* ext/nkf: import nkf.c rev:1.124
|
||||
Support CP10001.
|
||||
|
||||
Mon Jun 4 20:52:58 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* numeric.c (int_round): should not just truncate.
|
||||
|
||||
Sat Jun 2 16:48:55 2007 Koichi Sasada <ko1@atdot.net>
|
||||
|
||||
* cont.c (Fiber#pass): rename to Fiber#yield. Block parameter
|
||||
|
|
41
numeric.c
41
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
|
||||
|
|
Загрузка…
Ссылка в новой задаче