mercredi 8 février 2017

Slight deviation between datetime.datetime calculations in Python 2 and 3

Vote count: 0

While porting some code from Python 2 to Python 3, the tests highlighted a numerical regression when computing dates with datetime.datetime that I'm having a hard time to explain.

How to reproduce

date_max = datetime.datetime(2016, 9, 28, 4, 21, 5, 228000)
date_min = datetime.datetime(2016, 9, 28, 4, 21, 4, 460315)
date_futur = date_min + datetime.timedelta(seconds=((date_max - date_min).total_seconds() / 2))

Outputs

Output from print date_futur in Python 2.7.12:

2016-09-28 04:21:04.844158

Output from print(date_futur) in Python 3.5.2:

2016-09-28 04:21:04.844157

Issue

This is just one microsecond difference, but it bugs me because I cannot explain it, so I do not know if I can update my test results with the new Python 3 behavior or if something more complicated is at hand.

Possible lead

Maybe it is due to how Python 3 rounds 0.5 to the closest even number, and not up like Python 2 ?

asked 8 secs ago

Let's block ads! (Why?)



Slight deviation between datetime.datetime calculations in Python 2 and 3

Aucun commentaire:

Enregistrer un commentaire