[issue46639] Ceil division with math.ceildiv

2022-02-07 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: Round division would be useful not less than ceil division, if not more. In the stdlib it would be used in: 1. The datetime module. 2. Fraction.__round__. -- ___ Python tracker

[issue46639] Ceil division with math.ceildiv

2022-02-07 Thread Tim Peters
Tim Peters added the comment: 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

[issue46639] Ceil division with math.ceildiv

2022-02-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: Decimal is a good question. 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

[issue46639] Ceil division with math.ceildiv

2022-02-07 Thread Mark Dickinson
Mark Dickinson added the comment: > Couldn't math.ceildiv(x, y) be implemented as -(-x//y) in a type-agnostic > fashion? Ah, good point. Yes, that could work. We'd have to decide what to do about Decimal if we took this approach, since the -(-x//y) trick doesn't work there. (Document the is

[issue46639] Ceil division with math.ceildiv

2022-02-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: I don't understand why math.ceildiv couldn't ducktype. There's no rule that says it *must* convert arguments to float first, that's just a convention, and we've broken it before. >>> math.prod([Fraction(1, 3), 7]) Fraction(7, 3) Couldn't math.ceildiv(x, y)

[issue46639] Ceil division with math.ceildiv

2022-02-05 Thread Tim Peters
Tim Peters added the comment: I expect "obviousness" is mostly driven by background here. You know, e.g., that ceil(x) = -floor(-x) for any real x, and the application to integer division is just a special case of that. I expect programmers mostly don't know that, though. And Python having f

[issue46639] Ceil division with math.ceildiv

2022-02-05 Thread Mark Dickinson
Mark Dickinson added the comment: [Tim] > Because it's a bit obscure, and in real life y is always known to be > positive, so the nearly obvious (x + y - 1) // y works fine. Whereas I find (x + y - 1) // y less obvious at first sight than -(-x // y). :-) I don't care about negative y - that'

[issue46639] Ceil division with math.ceildiv

2022-02-05 Thread Tim Peters
Tim Peters added the comment: GMP's mpz has 18 functions of this form. These are the 6 "ceiling" flavors: c_divmod c_div c_mod c_divmod_2exp c_div_2exp c_mod_2exp The suggestion here is for c_div. There are 6 more for floor rounding (with prefix "f_" instead of "c_"), and another 6 for tru

[issue46639] Ceil division with math.ceildiv

2022-02-05 Thread Vladimir Feinberg
Vladimir Feinberg added the comment: Mark, I will say I'm pretty sympathetic to the feature-bloat avoidance perspective here, and if the outcome here is to improve docs, that's still a win, I think. That said, since this thread will become precedent, and I think `math.ceildiv` is the exactly-a

[issue46639] Ceil division with math.ceildiv

2022-02-05 Thread Mark Dickinson
Mark Dickinson added the comment: I'm not convinced that this deserves to be a math module function. I agree that `-(-x // y)`, while simple to write, isn't necessarily obvious. But it does have some advantages, like not needing an import, and being naturally duck-typed, so that it automatic

[issue46639] Ceil division with math.ceildiv

2022-02-04 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: See also issue31978. -- nosy: +serhiy.storchaka ___ Python tracker ___ ___ Python-bugs-list mai

[issue46639] Ceil division with math.ceildiv

2022-02-04 Thread Gregory P. Smith
Change by Gregory P. Smith : -- nosy: +mark.dickinson ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https:/

[issue46639] Ceil division with math.ceildiv

2022-02-04 Thread Nathaniel Manista
Change by Nathaniel Manista : -- nosy: +Nathaniel Manista ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: htt

[issue46639] Ceil division with math.ceildiv

2022-02-04 Thread Vladimir Feinberg
New submission from Vladimir Feinberg : I have a request related to the rejected proposal (https://bugs.python.org/issue43255) to introduce a ceildiv operator. I frequently find myself wishing for a ceildiv function which computes `ceil(x/y)` for integers `x,y`. This comes up all the time whe