Author vstinner
Recipients AVINASH MISHRA, jdemeyer, ncoghlan, remi.lapeyre, vstinner
Date 2019-01-10.16:45:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The problem comes from the private C function _PyTime_FromObject() of Python/pytime.c. This function must use the proper conversion to minimize the precision loss. Lib/test/ contains a lot of tests on conversions from different types and ensure that values are rounded correctly. See also my PEP 564 "Add new time functions with nanosecond resolution".

The correct code works for float and int (and maybe decimal.Decimal, I don't recall!), but it seems like it doesn't support types with __float__(). You have to explicitly cast such objects using float(value).

PyNumber_Float() can be used to convert arbitrary object to a float, but I'm not sure in which order the conversion should be tried to avoid/reduce precision loss during the conversion.


>>> x=2**53+1; x - int(float(x))

If we convert 'x' (int) to float, we introduce an error of 1.
Date User Action Args
2019-01-10 16:45:53vstinnersetrecipients: + vstinner, ncoghlan, jdemeyer, remi.lapeyre, AVINASH MISHRA
2019-01-10 16:45:52vstinnersetmessageid: <>
2019-01-10 16:45:52vstinnerlinkissue35707 messages
2019-01-10 16:45:52vstinnercreate