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

ruby-changes:68487

From: Burdette <ko1@a...>
Date: Sat, 16 Oct 2021 02:52:09 +0900 (JST)
Subject: [ruby-changes:68487] 37ea909f42 (master): Enhanced RDoc for divmod (#4973)

https://git.ruby-lang.org/ruby.git/commit/?id=37ea909f42

From 37ea909f426db2c548f7d79139c0eb924b68ef77 Mon Sep 17 00:00:00 2001
From: Burdette Lamar <BurdetteLamar@Y...>
Date: Fri, 15 Oct 2021 12:51:37 -0500
Subject: Enhanced RDoc for divmod (#4973)

Treats:

    Integer#divmod
    Float#divmod
    Numeric#divmod
---
 numeric.c | 119 ++++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 69 insertions(+), 50 deletions(-)

diff --git a/numeric.c b/numeric.c
index f6de533e1b..dfe6a1f849 100644
--- a/numeric.c
+++ b/numeric.c
@@ -693,45 +693,30 @@ num_remainder(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L693
 
 /*
  *  call-seq:
- *     num.divmod(numeric)  ->  array
- *
- *  Returns an array containing the quotient and modulus obtained by dividing
- *  +num+ by +numeric+.
- *
- *  If <code>q, r = x.divmod(y)</code>, then
- *
- *      q = floor(x/y)
- *      x = q*y + r
- *
- *  The quotient is rounded toward negative infinity, as shown in the
- *  following table:
- *
- *     a    |  b  |  a.divmod(b)  |   a/b   | a.modulo(b) | a.remainder(b)
- *    ------+-----+---------------+---------+-------------+---------------
- *     13   |  4  |   3,    1     |   3     |    1        |     1
- *    ------+-----+---------------+---------+-------------+---------------
- *     13   | -4  |  -4,   -3     |  -4     |   -3        |     1
- *    ------+-----+---------------+---------+-------------+---------------
- *    -13   |  4  |  -4,    3     |  -4     |    3        |    -1
- *    ------+-----+---------------+---------+-------------+---------------
- *    -13   | -4  |   3,   -1     |   3     |   -1        |    -1
- *    ------+-----+---------------+---------+-------------+---------------
- *     11.5 |  4  |   2,    3.5   |   2.875 |    3.5      |     3.5
- *    ------+-----+---------------+---------+-------------+---------------
- *     11.5 | -4  |  -3,   -0.5   |  -2.875 |   -0.5      |     3.5
- *    ------+-----+---------------+---------+-------------+---------------
- *    -11.5 |  4  |  -3,    0.5   |  -2.875 |    0.5      |    -3.5
- *    ------+-----+---------------+---------+-------------+---------------
- *    -11.5 | -4  |   2,   -3.5   |   2.875 |   -3.5      |    -3.5
- *
- *
- *  Examples
- *
- *     11.divmod(3)        #=> [3, 2]
- *     11.divmod(-3)       #=> [-4, -1]
- *     11.divmod(3.5)      #=> [3, 0.5]
- *     (-11).divmod(3.5)   #=> [-4, 3.0]
- *     11.5.divmod(3.5)    #=> [3, 1.0]
+ *    divmod(other) -> array
+ *
+ *  Returns a 2-element array <tt>[q, r]</tt>, where
+ *
+ *    q = (self/other).floor                  # Quotient
+ *    r = self % other                        # Remainder
+ *
+ *  Of the Core and Standard Library classes,
+ *  only Rational uses this implementation.
+ *
+ *  Examples:
+ *
+ *    Rational(11, 1).divmod(4)               # => [2, (3/1)]
+ *    Rational(11, 1).divmod(-4)              # => [-3, (-1/1)]
+ *    Rational(-11, 1).divmod(4)              # => [-3, (1/1)]
+ *    Rational(-11, 1).divmod(-4)             # => [2, (-3/1)]
+ *
+ *    Rational(12, 1).divmod(4)               # => [3, (0/1)]
+ *    Rational(12, 1).divmod(-4)              # => [-3, (0/1)]
+ *    Rational(-12, 1).divmod(4)              # => [-3, (0/1)]
+ *    Rational(-12, 1).divmod(-4)             # => [3, (0/1)]
+ *
+ *    Rational(13, 1).divmod(4.0)             # => [3, 1.0]
+ *    Rational(13, 1).divmod(Rational(4, 11)) # => [35, (3/11)]
  */
 
 static VALUE
@@ -1255,12 +1240,28 @@ dbl2ival(double d) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1240
 
 /*
  *  call-seq:
- *     float.divmod(numeric)  ->  array
+ *    divmod(other) -> array
  *
- *  See Numeric#divmod.
+ *  Returns a 2-element array <tt>[q, r]</tt>, where
+ *
+ *    q = (self/other).floor      # Quotient
+ *    r = self % other            # Remainder
+ *
+ *  Examples:
+ *
+ *    11.0.divmod(4)              # => [2, 3.0]
+ *    11.0.divmod(-4)             # => [-3, -1.0]
+ *    -11.0.divmod(4)             # => [-3, 1.0]
+ *    -11.0.divmod(-4)            # => [2, -3.0]
+ *
+ *    12.0.divmod(4)              # => [3, 0.0]
+ *    12.0.divmod(-4)             # => [-3, 0.0]
+ *    -12.0.divmod(4)             # => [-3, -0.0]
+ *    -12.0.divmod(-4)            # => [3, -0.0]
+ *
+ *    13.0.divmod(4.0)            # => [3, 1.0]
+ *    13.0.divmod(Rational(4, 1)) # => [3, 1.0]
  *
- *     42.0.divmod(6)   #=> [7, 0.0]
- *     42.0.divmod(5)   #=> [8, 2.0]
  */
 
 static VALUE
@@ -3842,13 +3843,6 @@ int_remainder(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3843
     return Qnil;
 }
 
-/*
- *  Document-method: Integer#divmod
- *  call-seq:
- *     int.divmod(numeric)  ->  array
- *
- *  See Numeric#divmod.
- */
 static VALUE
 fix_divmod(VALUE x, VALUE y)
 {
@@ -3878,6 +3872,31 @@ fix_divmod(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3872
     }
 }
 
+/*
+ *  call-seq:
+ *    divmod(other) -> array
+ *
+ *  Returns a 2-element array <tt>[q, r]</tt>, where
+ *
+ *    q = (self/other).floor    # Quotient
+ *    r = self % other          # Remainder
+ *
+ *  Examples:
+ *
+ *    11.divmod(4)              # => [2, 3]
+ *    11.divmod(-4)             # => [-3, -1]
+ *    -11.divmod(4)             # => [-3, 1]
+ *    -11.divmod(-4)            # => [2, -3]
+ *
+ *    12.divmod(4)              # => [3, 0]
+ *    12.divmod(-4)             # => [-3, 0]
+ *    -12.divmod(4)             # => [-3, 0]
+ *    -12.divmod(-4)            # => [3, 0]
+ *
+ *    13.divmod(4.0)            # => [3, 1.0]
+ *    13.divmod(Rational(4, 1)) # => [3, (1/1)]
+ *
+ */
 VALUE
 rb_int_divmod(VALUE x, VALUE y)
 {
-- 
cgit v1.2.1


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

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