Message227156
> Is this change compelling enough to break compatibility,
> or is it just a matter of purity?
According to the PEP 3141, Integer is a subtype of Real, so one should be able to substitute an Integer whenever Real is expected. The reverse is not true, for example
>>> [1,2][1.0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not float
This is one of the common uses of floor division - to find an index of a cell in a regular grid: (x - start) // step. In this situation, it is convenient to have the result ready to be used as an index without a cast.
The substitution principle also suggests that compatibility issues are likely to be small: in most contexts integers behave the same as floats or "better".
Here is an example of a "better" behavior:
>>> x = 1 + 10**50
>>> x * 1 == x
True
>>> x * 1.0 == x
False
The only case I can think of where float result may be preferable is inf // 1 because integers cannot represent infinity. However, this case is arguably already broken.
What are the use-cases for float // float where integer result is not acceptable? |
|
Date |
User |
Action |
Args |
2014-09-20 16:01:29 | belopolsky | set | recipients:
+ belopolsky, rhettinger, pitrou, casevh, skrah |
2014-09-20 16:01:29 | belopolsky | set | messageid: <1411228889.28.0.752055468204.issue22444@psf.upfronthosting.co.za> |
2014-09-20 16:01:29 | belopolsky | link | issue22444 messages |
2014-09-20 16:01:28 | belopolsky | create | |
|