summaryrefslogtreecommitdiffstats
path: root/numeric.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-06-01 16:27:05 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-06-01 16:27:05 +0000
commit650ebdbc141555838cc48a937d1dcdb4e52894e7 (patch)
tree18c1a2673e7507a440a516fdd8887939a22774b0 /numeric.c
parent5bb4e5a9d14a97736cd1c909e4b26bc65b2682bd (diff)
downloadruby-650ebdbc141555838cc48a937d1dcdb4e52894e7.tar.gz
ruby-650ebdbc141555838cc48a937d1dcdb4e52894e7.tar.xz
ruby-650ebdbc141555838cc48a937d1dcdb4e52894e7.zip
* numeric.c (int_round): small optimization to handle bignums.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@12423 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/numeric.c b/numeric.c
index 8cf7a1e48..6153b9454 100644
--- a/numeric.c
+++ b/numeric.c
@@ -2308,6 +2308,22 @@ 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
@@ -3013,8 +3029,8 @@ Init_Numeric(void)
rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
rb_define_method(rb_cInteger, "floor", int_to_i, 0);
rb_define_method(rb_cInteger, "ceil", int_to_i, 0);
- rb_define_method(rb_cInteger, "round", int_to_i, 0);
rb_define_method(rb_cInteger, "truncate", int_to_i, 0);
+ rb_define_method(rb_cInteger, "round", int_round, -1);
rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
rb_include_module(rb_cFixnum, rb_mPrecision);