[前][次][番号順一覧][スレッド一覧]

ruby-changes:42764

From: akr <ko1@a...>
Date: Sat, 30 Apr 2016 16:30:53 +0900 (JST)
Subject: [ruby-changes:42764] akr:r54838 (trunk): Define Integer#remainder instead of Bignum#remainder.

akr	2016-04-30 17:27:30 +0900 (Sat, 30 Apr 2016)

  New Revision: 54838

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54838

  Log:
    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.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/internal.h
    trunk/numeric.c
Index: numeric.c
===================================================================
--- numeric.c	(revision 54837)
+++ numeric.c	(revision 54838)
@@ -3537,6 +3537,40 @@ rb_int_modulo(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3537
 }
 
 /*
+ *  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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4877
     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);
Index: bignum.c
===================================================================
--- bignum.c	(revision 54837)
+++ bignum.c	(revision 54838)
@@ -6128,16 +6128,7 @@ rb_big_modulo(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6128
     return bignorm(z);
 }
 
-/*
- *  call-seq:
- *     big.remainder(numeric)    -> number
- *
- *  Returns the remainder after dividing <i>big</i> by <i>numeric</i>.
- *
- *     -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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6879
     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);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54837)
+++ ChangeLog	(revision 54838)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr 30 16:58:18 2016  Tanaka Akira  <akr@f...>
+
+	* 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  <akr@f...>
 
 	* numeric.c (rb_int_uminus): {Fixnum,Bignum}#-@ is unified into
Index: internal.h
===================================================================
--- internal.h	(revision 54837)
+++ internal.h	(revision 54838)
@@ -786,6 +786,7 @@ VALUE rb_big_aref(VALUE x, VALUE y); https://github.com/ruby/ruby/blob/trunk/internal.h#L786
 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);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]