diff --git a/ChangeLog b/ChangeLog index 30599dac4c..ece9230a9a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Sat Apr 30 16:58:18 2016 Tanaka Akira + + * numeric.c (int_remainder): Define Integer#remainder. + + * bignum.c (rb_big_remainder): Don't define Bignum#remainder. + + * internal.h (rb_big_remainder): Declared. + Sat Apr 30 15:29:24 2016 Tanaka Akira * numeric.c (rb_int_uminus): {Fixnum,Bignum}#-@ is unified into diff --git a/bignum.c b/bignum.c index 70aad22c1d..c23f439087 100644 --- a/bignum.c +++ b/bignum.c @@ -6128,16 +6128,7 @@ rb_big_modulo(VALUE x, VALUE y) return bignorm(z); } -/* - * call-seq: - * big.remainder(numeric) -> number - * - * Returns the remainder after dividing big by numeric. - * - * -1234567890987654321.remainder(13731) #=> -6966 - * -1234567890987654321.remainder(13731.24) #=> -9906.22531493148 - */ -static VALUE +VALUE rb_big_remainder(VALUE x, VALUE y) { VALUE z; @@ -6888,7 +6879,6 @@ Init_Bignum(void) rb_define_method(rb_cBignum, "*", rb_big_mul, 1); rb_define_method(rb_cBignum, "/", rb_big_div, 1); rb_define_method(rb_cBignum, "%", rb_big_modulo, 1); - rb_define_method(rb_cBignum, "remainder", rb_big_remainder, 1); rb_define_method(rb_cBignum, "==", rb_big_eq, 1); rb_define_method(rb_cBignum, ">", big_gt, 1); diff --git a/internal.h b/internal.h index 584742e596..08379cb7b7 100644 --- a/internal.h +++ b/internal.h @@ -786,6 +786,7 @@ VALUE rb_big_aref(VALUE x, VALUE y); VALUE rb_big_abs(VALUE x); VALUE rb_big_size_m(VALUE big); VALUE rb_big_bit_length(VALUE big); +VALUE rb_big_remainder(VALUE x, VALUE y); /* class.c */ VALUE rb_class_boot(VALUE); diff --git a/numeric.c b/numeric.c index e772b01a34..6664143c48 100644 --- a/numeric.c +++ b/numeric.c @@ -3536,6 +3536,40 @@ rb_int_modulo(VALUE x, VALUE y) return num_modulo(x, y); } +/* + * call-seq: + * int.remainder(numeric) -> real + * + * + * Returns the remainder after dividing big by numeric 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: @@ -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);