Method: BigDecimal#divmod

Defined in:
ext/bigdecimal/bigdecimal.c

#divmod(value) ⇒ Object

Divides by the specified value, and returns the quotient and modulus as BigDecimal numbers. The quotient is rounded towards negative infinity.

For example:

require 'bigdecimal'

a = BigDecimal("42")
b = BigDecimal("9")

q, m = a.divmod(b)

c = q * b + m

a == c  #=> true

The quotient q is (a/b).floor, and the modulus is the amount that must be added to q * b to get a.



1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
# File 'ext/bigdecimal/bigdecimal.c', line 1833

static VALUE
BigDecimal_divmod(VALUE self, VALUE r)
{
    NULLABLE_BDVALUE div, mod;

    if (BigDecimal_DoDivmod(self, r, &div, &mod, false)) {
        return rb_assoc_new(BigDecimal_to_i(CheckGetValue(bdvalue_nonnullable(div))), CheckGetValue(bdvalue_nonnullable(mod)));
    }
    return DoSomeOne(self,r,rb_intern("divmod"));
}