Message175558
> I believe that definining x//y as math.floor(x/y) is also confusing
> in other cases (without being able to construct such cases right away).
In addition, defining x//y as math.floor(x / y) would break its connection with %: a key invariant is that
(x // y) * y + x % y should be (approximately in the case of floats) equal to y.
The connection between // and % is more fundamental than the connection between // and /, so in cases where the two disagree, the %-related one wins.
For applications: it's true that they're not common, but they do exist. One such is argument reduction: e.g., for a toy case, suppose that you're implementing a function that computes and returns sin and cos. The computation can be reduced to computing for angles between 0 and pi / 4:
def sincos(x):
""" Compute and return sin(x) and cos(x). """
q, r = divmod(x, pi / 4)
<compute sincos(r)>
<use symmetries and the last 3 bits of q to compute sincos(x)>
This is an example where if the relationship between % and // were broken, we'd get wrong results---not simply inaccurate, but completely wrong.
It's also worth noting that // and % are special in that they're the only basic arithmetic operations that can be computed *exactly*, with no numeric error, for a wide range of inputs: e.g., if x and y are positive and x / y < 2**53, then both x // y and x % y return exact results. Modifying them to return inexact results instead would be ... surprising. |
|
Date |
User |
Action |
Args |
2012-11-14 10:11:41 | mark.dickinson | set | recipients:
+ mark.dickinson, loewis, rhettinger, skrah, serhiy.storchaka, tom.pohl |
2012-11-14 10:11:41 | mark.dickinson | set | messageid: <1352887901.1.0.28801402202.issue16460@psf.upfronthosting.co.za> |
2012-11-14 10:11:41 | mark.dickinson | link | issue16460 messages |
2012-11-14 10:11:40 | mark.dickinson | create | |
|