In Python there is a operation for floor division: a // b. Ceil division easy can be expressed via floor division: -((-a) // b). But round division is more complicated. This operation is needed in Fraction.__round__, in a number of methods in the datetime module (see _divide_and_round). Due to the complexity of the correct Python implementation, it is slower then just division. I propose to add special function in the math module. This not only will speed up Python implementation of the datetime module and the fractions module, but will encourage users to use correct algorithm instead of obvious but incorrect round(a/b).
Pardon my ignorance, but what is the definition of round division? (if it isn't "round(a/b)")
On 10 September 2015 at 22:13, Mark Young
Pardon my ignorance, but what is the definition of round division? (if it isn't "round(a/b)")
I assumed it would be "what round(a/b) would give if it weren't subject to weird floating point rounding issues". To put it another way, if a / b is d remainder r, then I'd assume "round division" would be d if r < b/2, d+1 if r > b/2, and (which of d, d+1?) if r == b/2. (a, b, d and r are all integers). If not, then I also would like to know what it means... Either way, if it is introduced then it should be documented (particularly as regards what happens when one or both of a, b are negative) clearly, as it's not 100% obvious. Also, is the math module the right place? All of the operations in the math module (apart from factorial, for some reason...) are floating point. Paul
On 11.09.15 00:48, Paul Moore wrote:
On 10 September 2015 at 22:13, Mark Young
wrote: Pardon my ignorance, but what is the definition of round division? (if it isn't "round(a/b)")
I assumed it would be "what round(a/b) would give if it weren't subject to weird floating point rounding issues". To put it another way, if a / b is d remainder r, then I'd assume "round division" would be d if r < b/2, d+1 if r > b/2, and (which of d, d+1?) if r == b/2. (a, b, d and r are all integers).
If not, then I also would like to know what it means...
Yes, it is what you have described. If r == b/2, the result is even (i.e. (d+1)//2*2).
Either way, if it is introduced then it should be documented (particularly as regards what happens when one or both of a, b are negative) clearly, as it's not 100% obvious.
Also, is the math module the right place? All of the operations in the math module (apart from factorial, for some reason...) are floating point.
It is the best place in the stdlib. Apart from floating point functions, the math module contains integer functions (factorial and gcd) and general number functions (floor, ceil, trunc and isclose). gcd and isclose are new in 3.5.
On Thu, Sep 10, 2015 at 3:39 PM, Serhiy Storchaka
On 11.09.15 00:48, Paul Moore wrote:
On 10 September 2015 at 22:13, Mark Young
wrote: Pardon my ignorance, but what is the definition of round division? (if it isn't "round(a/b)")
I assumed it would be "what round(a/b) would give if it weren't subject to weird floating point rounding issues". To put it another way, if a / b is d remainder r, then I'd assume "round division" would be d if r < b/2, d+1 if r > b/2, and (which of d, d+1?) if r == b/2. (a, b, d and r are all integers).
If not, then I also would like to know what it means...
Yes, it is what you have described. If r == b/2, the result is even (i.e. (d+1)//2*2).
Either way, if it is introduced then it should be documented
(particularly as regards what happens when one or both of a, b are negative) clearly, as it's not 100% obvious.
Also, is the math module the right place? All of the operations in the math module (apart from factorial, for some reason...) are floating point.
It is the best place in the stdlib. Apart from floating point functions, the math module contains integer functions (factorial and gcd) and general number functions (floor, ceil, trunc and isclose). gcd and isclose are new in 3.5.
well, floor, ceil, and isclose are all about floats... Nevertheless, yes the math module is the place for it. -CHB
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
On Fri, Sep 11, 2015 at 01:39:59AM +0300, Serhiy Storchaka wrote:
On 11.09.15 00:48, Paul Moore wrote:
On 10 September 2015 at 22:13, Mark Young
wrote: Pardon my ignorance, but what is the definition of round division? (if it isn't "round(a/b)")
I assumed it would be "what round(a/b) would give if it weren't subject to weird floating point rounding issues". To put it another way, if a / b is d remainder r, then I'd assume "round division" would be d if r < b/2, d+1 if r > b/2, and (which of d, d+1?) if r == b/2. (a, b, d and r are all integers).
If not, then I also would like to know what it means...
Yes, it is what you have described. If r == b/2, the result is even (i.e. (d+1)//2*2).
How does this differ from round(a/b)? round() also rounds to even. Perhaps a more general solution would be a round-to-direction, or divide-and-round-to-direction. Now that we have Enums, we could define enums for round-to-zero, round-to-nearest, round-to-infinity, round-to-even, and have a function divide(a, b, dir=ROUNDEVEN), say. -- Steve
On Sep 10, 2015, at 20:13, Steven D'Aprano
On Fri, Sep 11, 2015 at 01:39:59AM +0300, Serhiy Storchaka wrote:
On 11.09.15 00:48, Paul Moore wrote:
On 10 September 2015 at 22:13, Mark Young
wrote: Pardon my ignorance, but what is the definition of round division? (if it isn't "round(a/b)") I assumed it would be "what round(a/b) would give if it weren't subject to weird floating point rounding issues". To put it another way, if a / b is d remainder r, then I'd assume "round division" would be d if r < b/2, d+1 if r > b/2, and (which of d, d+1?) if r == b/2. (a, b, d and r are all integers).
If not, then I also would like to know what it means...
Yes, it is what you have described. If r == b/2, the result is even (i.e. (d+1)//2*2).
How does this differ from round(a/b)? round() also rounds to even.
His rounds based on the exact integer remainder; yours rounds based on the inexact float fractional part. So, if b is large enough, using round division is guaranteed to do the right thing,[1] but rounding float division may have rounding, overflow, or underflow errors. [1] Except I'm pretty sure he wants to compare r*2 to b, not r to b/2. Otherwise he's reintroduced the problem he's trying to solve.
On 11.09.15 06:13, Steven D'Aprano wrote:
How does this differ from round(a/b)? round() also rounds to even.
round(5000000000000000/9999999999999999) 0 round(14999999999999999/10000000000000000) 2
But fractions 5000000000000000/9999999999999999 > 1/2 and 14999999999999999/10000000000000000 < 3/2.
On 9/10/2015 11:27 PM, Serhiy Storchaka wrote:
On 11.09.15 06:13, Steven D'Aprano wrote:
How does this differ from round(a/b)? round() also rounds to even.
round(5000000000000000/9999999999999999) 0 round(14999999999999999/10000000000000000) 2
But fractions 5000000000000000/9999999999999999 > 1/2 and 14999999999999999/10000000000000000 < 3/2.
Wow -- I'm glad I work predominately in business environments and keep amounts in pennies. The only time I need to round anything is to the nearest cent. Emile
On Fri, Sep 11, 2015 at 9:18 AM, Emile van Sebille
On 9/10/2015 11:27 PM, Serhiy Storchaka wrote:
On 11.09.15 06:13, Steven D'Aprano wrote:
How does this differ from round(a/b)? round() also rounds to even.
round(5000000000000000/9999999999999999) 0 round(14999999999999999/10000000000000000) 2
But fractions 5000000000000000/9999999999999999 > 1/2 and 14999999999999999/10000000000000000 < 3/2.
Wow -- I'm glad I work predominately in business environments and keep amounts in pennies. The only time I need to round anything is to the nearest cent.
I thought any programmer worth their salt would round down (i.e. trunc()) and transfer the fractional penny to their own account? :-) -- --Guido van Rossum (python.org/~guido)
Guido van Rossum writes:
On Fri, Sep 11, 2015 at 9:18 AM, Emile van Sebille
wrote:
Wow -- I'm glad I work predominately in business environments and keep amounts in pennies. The only time I need to round anything is to the nearest cent.
I thought any programmer worth their salt would round down (i.e. trunc()) and transfer the fractional penny to their own account? :-)
Hate to tell you, but the accountants and even the SEC caught on to that one four decades ago.
participants (9)
-
Andrew Barnert
-
Chris Barker
-
Emile van Sebille
-
Guido van Rossum
-
Mark Young
-
Paul Moore
-
Serhiy Storchaka
-
Stephen J. Turnbull
-
Steven D'Aprano