APIdock / Ruby
/
method

divmod

ruby latest stable - Class: Numeric
divmod(p1)
public

Returns an array containing the quotient and modulus obtained by dividing num by numeric.

If q, r = x.divmod(y), 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]
static VALUE
num_divmod(VALUE x, VALUE y)
{
 return rb_assoc_new(num_div(x, y), num_modulo(x, y));
}

AltStyle によって変換されたページ (->オリジナル) /