[issue38929] Float // Integer doesn't give best result.
Steven D'Aprano
report at bugs.python.org
Wed Nov 27 07:50:33 EST 2019
Steven D'Aprano <steve+python at pearwood.info> added the comment:
Although it often gets called "integer division", that's not actually what // does, it is actually *floor* division, as documented here:
https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
So the behaviour as given is correct: it returns the floor of the quotient. Arguments are coerced to a common type in the normal fashion, so float//int performs the floor division in floating point, which may not give the same result as integer floor division due to floating point rounding:
py> 10**17 // 7
14285714285714285
py> 1e17 // 7
1.4285714285714284e+16
If you don't want floating point rounding, don't use floats.
If you want "true division", use / not the floor division operator.
This isn't a bug, it is working as designed.
Even if the behaviour you are asking for was possible, practical and desirable, it would break backwards compatibility. There's no way to do a "floor division that returns an int" for all float arguments, since there are no int NANs.
py> float('inf')//7
nan
----------
nosy: +steven.daprano
resolution: -> not a bug
stage: -> resolved
status: open -> closed
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38929>
_______________________________________
More information about the Python-bugs-list
mailing list