public Method

Numeric.divmod(p1)

num.divmod( aNumeric )  anArray

Returns an array containing the quotient and modulus obtained by dividing num by aNumeric. If q, r = x.divmod(y), then

q = floor(float(x)/float(y))
x = q*y + r

The quotient is rounded toward -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     |  -3     |   -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]

Source Code

/*
*  call-seq:
*     num.divmod( aNumeric ) -> anArray
*  
*  Returns an array containing the quotient and modulus obtained by
*  dividing <i>num</i> by <i>aNumeric</i>. If <code>q, r =
*  x.divmod(y)</code>, then
*
*      q = floor(float(x)/float(y))
*      x = q*y + r
*     
*  The quotient is rounded toward -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     |  -3     |   -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]
*/

static VALUE
num_divmod(x, y)
   VALUE x, y;
{
   return rb_assoc_new(num_div(x, y), rb_funcall(x, '%', 1, y));
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.