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 <1547138752.64.0.136082277409.issue35707@roundup.psfhosted.org>
In-reply-to
Content
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/test_time.py 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.

Example:

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

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