summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-30 08:27:30 (GMT)
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-30 08:27:30 (GMT)
commit635cff451ff4405ee8ad5c97d9b5b6a04435e0da (patch)
tree426d5a90062c874ee8b90bbf4832e42ffa41c6db /numeric.c
parentae225a2ca84ea84da0c7c15ad07b9e89fbbb9392 (diff)
Define Integer#remainder instead of Bignum#remainder.
* numeric.c (int_remainder): Define Integer#remainder. * bignum.c (rb_big_remainder): Don't define Bignum#remainder. * internal.h (rb_big_remainder): Declared. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54838 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/numeric.c b/numeric.c
index e772b01..6664143 100644
--- a/numeric.c
+++ b/numeric.c
@@ -3537,6 +3537,40 @@ rb_int_modulo(VALUE x, VALUE y)
}
/*
+ * call-seq:
+ * int.remainder(numeric) -> real
+ *
+ *
+ * Returns the remainder after dividing <i>big</i> by <i>numeric</i> as:
+ *
+ * x.remainder(y) means x-y*(x/y).truncate
+ *
+ * Examples
+ *
+ * 5.remainder(3) #=> 2
+ * -5.remainder(3) #=> -2
+ * 5.remainder(-3) #=> 2
+ * -5.remainder(-3) #=> -2
+ *
+ * -1234567890987654321.remainder(13731) #=> -6966
+ * -1234567890987654321.remainder(13731.24) #=> -9906.22531493148
+ *
+ * See Numeric#divmod.
+ */
+
+VALUE
+int_remainder(VALUE x, VALUE y)
+{
+ if (FIXNUM_P(x)) {
+ return num_remainder(x, y);
+ }
+ else if (RB_TYPE_P(x, T_BIGNUM)) {
+ return rb_big_remainder(x, y);
+ }
+ return Qnil;
+}
+
+/*
* Document-method: Integer#divmod
* call-seq:
* integer.divmod(numeric) -> array
@@ -4843,6 +4877,7 @@ Init_Numeric(void)
rb_define_method(rb_cInteger, "div", rb_int_idiv, 1);
rb_define_method(rb_cFixnum, "%", fix_mod, 1);
rb_define_method(rb_cInteger, "modulo", rb_int_modulo, 1);
+ rb_define_method(rb_cInteger, "remainder", int_remainder, 1);
rb_define_method(rb_cInteger, "divmod", int_divmod, 1);
rb_define_method(rb_cInteger, "fdiv", int_fdiv, 1);
rb_define_method(rb_cInteger, "**", rb_int_pow, 1);