[issue18629] future division breaks timedelta division by integer

Tim Peters report at bugs.python.org
Sat Aug 3 19:44:39 CEST 2013


Tim Peters added the comment:

Well, a timedelta is a duration.  timedelta // n is as close as possible to one n'th of that duration, but "rounding down" (if necessary) so that the result is representable as a timedelta.  In the same way, if i and j are integers, i // j is as close as possible to one j'th of i, but "rounding down" (if necessary) so that the result is representable as an integer.  Like:

>>> from datetime import timedelta
>>> timedelta(1) // 7
datetime.timedelta(0, 12342, 857142)
>>> timedelta(1) - 7 * _
datetime.timedelta(0, 0, 6)
>>>

The last line shows the part truncated away:  one seventh of a day is not representable as a timedetla. If `timedelta // int` rounded to the closest representable timedelta, it would return timedelta(0, 12342, 857143) instead.  That's a little bigger than a seventh of a day:

>>> timedelta(0, 12342, 857143) * 7
datetime.timedelta(1, 0, 1)

It has nothing directly to do with days, hours, seconds ... it so happens that timedelta has microsecond resolution, so the closest representable approximations to one n'th of a timedelta most often have non-zero microsecond components.  If timedelta had femtosecond resolution, they'd most often have non-zero femtosecond components ;-)

----------
nosy: +tim_one

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18629>
_______________________________________


More information about the Python-bugs-list mailing list