divmod(p1)
public
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)
11.divmod(-3)
11.divmod(3.5)
(-11).divmod(3.5)
(11.5).divmod(3.5)
Show source
/*
* 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)
* 11.divmod(-3)
* 11.divmod(3.5)
* (-11).divmod(3.5)
* (11.5).divmod(3.5)
*/
static VALUE
num_divmod(x, y)
VALUE x, y;
{
return rb_assoc_new(num_div(x, y), rb_funcall(x, '%', 1, y));
}