You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why does floor division not do floor division on Decimal? The
documentation says
The integer division operator // behaves analogously, returning the
integer part of the true quotient (truncating towards zero) rather
than its floor, so as to preserve the usual identity
x == (x // y) * y + x % y
but it's not clear why that identity is more important than floor
division returning the floor.
I guess we could just document the difference and maybe add a
Decimal ceildiv method, although that makes me sad :-(
The decimal module intends to be a faithful implementation of external standards. The identity
x == (x // y) * y + x % y
isn't a minor detail, it's the dog on which all else is but a tail ;-)
It's why Guido picked -7 // 4 = -2 in Python[1]. That's really not all that useful on its own, and does surprise people. But it's necessary so that the identity implies -7 % 4 = 1, and _that's_ what drove it all, the desire that doing mod wrt a positive int always give a non-negative result.
The identity also implies that, under truncating division, mod returns a result with the sign of the dividend rather than of the divisor.
decimal implements what the external standards demand integer division do, and also integer modulus. The decimal context divmod() method returns both at once.
The behavior of int div and mod are linked by all standards I'm aware of, including by C89 (which didn't define what integer division did in all cases, but did demand that - whatever it did - % had to work in a way consistent with the named identity).
I'm going to close this here. Like every other design decision it's a trade-off. Both the benefit and the cost are small, but to my eyes the cost outweighs the benefits here.
Reacted by endolith, anshulpanjabimil and Akshit Kumar
@serhiy-storchaka Is it worth re-opening/re-considering this issue now that there's a more obvious place to put a ceildiv function?
One wrinkle - in the place I most recently found myself wanting a ceildiv operation, I was actually working with datetime.timedelta objects rather than integers (or floats). That is, I was doing something along the lines of:
and I wanted to be able to spell that fourth line as analysis_count = ceildiv(video_length, max_analysis_length).
So if ceildiv got added to math.integer, would it break the math.integer module policy to have it accept timedelta objects as well as ints? What about accepting floats?
So if ceildiv got added to math.integer, would it break the math.integer module policy to have it accept timedelta objects as well as ints? What about accepting floats?
Maybe worth noting that other similarly type-generic functions (math.prod, math.sumprod) are still in math, not math.integer.
It is easier to implement ceildiv and friends only for integers (_PyLong_DivmodNear only supports integers), but it would be more convenient if they are general, applicable to floats, decimals, fractions. Should we add two variants -- integer-only and general?
And if add ceildiv, we need to add also variants with rounding to the nearest integer, to zero, to infinity, and corresponding divmod variants.
We have also to keep in mind that in future we may add the rounding option for isqrt() and for future ilog() or ilog2().
See also #139273. We may need more such complicated code if we want support high-precision timedelta.
The only thing that's actually been requested (& more than once) I've seen is ceildiv(int, int). Would it be possible to just start with that, to make some users happy, without making any progress for years on end due to hypothetical "purity" arguments? 😉.
Python just doesn't need GMP's 18 variations, but GMP is there if people want them. Real Python programmers do want what the OP of this report asked for. It's not at all uncommon in "number theory" work too - int ceiling division is a fundamental primitive.
Other types can follow later if there's real need demonstrated repeatedly. But it's a pit. For example, timedelta // timedelta returns an int now, but timedelta // int returns a timedelta, and timedelta // float raises TypeError.
If args aren't int, I'd be fine with falling back to the generic -(x // -y), but special-casing Decimal to raise TypeError instead (since decimal's implementation of __floordiv__ doesn't work for this, and better to refuse to pretend that whatever it does is fine).
Lets focus just on ceildiv and friends here. And reopen issue, indeed.
would it break the math.integer module policy to have it accept timedelta objects as well as ints?
Certainly yes.
What about accepting floats?
For floats, like Decimal's, maybe we have a better option? I.e. support contexts, that will expose FPE status flags and control modes.
And if add ceildiv, we need to add also variants with rounding to the nearest integer, to zero, to infinity, and corresponding divmod variants.
We have also to keep in mind that in future we may add the rounding option for isqrt()
If we are going to support different rounding modes for isqrt(), maybe we should do same for division, instead of bunch of gmpy2-like functions? (Though, for isqrt() I would rather prefer isqrt_rem() as a building block.)
It seems to me that for division - all three cases from the GMP do make sense (we have feature requests and/or code in the stdlib, which reinvent the wheel). But I'm not sure if we need something beyond the quotient, i.e. c_divmod(), t_mod() and so on. Note also, that MPFR/libmpdec have more rounding modes.
Activity
stevendaprano commentedon Feb 8, 2022
Decimal is a good question.
Why does floor division not do floor division on Decimal? The
documentation says
but it's not clear why that identity is more important than floor
division returning the floor.
I guess we could just document the difference and maybe add a
Decimal ceildiv method, although that makes me sad :-(
Could we "fix" Decimal?
tim-one commentedon Feb 8, 2022
The
decimalmodule intends to be a faithful implementation of external standards. The identityx == (x // y) * y + x % y
isn't a minor detail, it's the dog on which all else is but a tail ;-)
It's why Guido picked -7 // 4 = -2 in Python[1]. That's really not all that useful on its own, and does surprise people. But it's necessary so that the identity implies -7 % 4 = 1, and _that's_ what drove it all, the desire that doing mod wrt a positive int always give a non-negative result.
The identity also implies that, under truncating division, mod returns a result with the sign of the dividend rather than of the divisor.
decimalimplements what the external standards demand integer division do, and also integer modulus. The decimal contextdivmod()method returns both at once.The behavior of int div and mod are linked by all standards I'm aware of, including by C89 (which didn't define what integer division did in all cases, but did demand that - whatever it did - % had to work in a way consistent with the named identity).
[1] http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html
serhiy-storchaka commentedon Feb 8, 2022
Round division would be useful not less than ceil division, if not more. In the stdlib it would be used in:
mdickinson commentedon Apr 11, 2022
I'm going to close this here. Like every other design decision it's a trade-off. Both the benefit and the cost are small, but to my eyes the cost outweighs the benefits here.
mdickinson commentedon Nov 5, 2025
@serhiy-storchaka Is it worth re-opening/re-considering this issue now that there's a more obvious place to put a
ceildivfunction?One wrinkle - in the place I most recently found myself wanting a
ceildivoperation, I was actually working withdatetime.timedeltaobjects rather than integers (or floats). That is, I was doing something along the lines of:and I wanted to be able to spell that fourth line as
analysis_count = ceildiv(video_length, max_analysis_length).So if
ceildivgot added tomath.integer, would it break themath.integermodule policy to have it accepttimedeltaobjects as well asints? What about acceptingfloats?mdickinson commentedon Nov 5, 2025
Maybe worth noting that other similarly type-generic functions (
math.prod,math.sumprod) are still inmath, notmath.integer.serhiy-storchaka commentedon Nov 5, 2025
It is easier to implement
ceildivand friends only for integers (_PyLong_DivmodNearonly supports integers), but it would be more convenient if they are general, applicable to floats, decimals, fractions. Should we add two variants -- integer-only and general?And if add
ceildiv, we need to add also variants with rounding to the nearest integer, to zero, to infinity, and correspondingdivmodvariants.We have also to keep in mind that in future we may add the rounding option for
isqrt()and for futureilog()orilog2().See also #139273. We may need more such complicated code if we want support high-precision
timedelta.tim-one commentedon Nov 5, 2025
The only thing that's actually been requested (& more than once) I've seen is
ceildiv(int, int). Would it be possible to just start with that, to make some users happy, without making any progress for years on end due to hypothetical "purity" arguments? 😉.Python just doesn't need GMP's 18 variations, but GMP is there if people want them. Real Python programmers do want what the OP of this report asked for. It's not at all uncommon in "number theory" work too - int ceiling division is a fundamental primitive.
Other types can follow later if there's real need demonstrated repeatedly. But it's a pit. For example,
timedelta // timedeltareturns anintnow, buttimedelta // intreturns atimedelta, andtimedelta // floatraisesTypeError.If args aren't int, I'd be fine with falling back to the generic
-(x // -y), but special-casingDecimalto raiseTypeErrorinstead (since decimal's implementation of__floordiv__doesn't work for this, and better to refuse to pretend that whatever it does is fine).serhiy-storchaka commentedon Nov 5, 2025
I do not remember I even needed
ceildiv(perhaps because this was easy to write in other way), but_divide_and_roundis not so trivial.If you only need integer operations -- I think that the bar to add new functions to
math.integeris now lower.skirpichev commentedon Nov 5, 2025
Lets focus just on ceildiv and friends here. And reopen issue, indeed.
Certainly yes.
For floats, like Decimal's, maybe we have a better option? I.e. support contexts, that will expose FPE status flags and control modes.
If we are going to support different rounding modes for isqrt(), maybe we should do same for division, instead of bunch of gmpy2-like functions? (Though, for isqrt() I would rather prefer isqrt_rem() as a building block.)
It seems to me that for division - all three cases from the GMP do make sense (we have feature requests and/or code in the stdlib, which reinvent the wheel). But I'm not sure if we need something beyond the quotient, i.e. c_divmod(), t_mod() and so on. Note also, that MPFR/libmpdec have more rounding modes.
skirpichev commentedon May 8, 2026
FYI: https://discuss.python.org/t/integer-ceiling-divide/91269
skirpichev commentedon May 31, 2026
I'll close this issue in favor of #76159 (more old). Both asks for support of differently rounded division.