Repository navigation
gh-67795: Improve precision of non-float timestamp and timeout arguments - #139273
serhiy-storchaka merged 12 commits into
Conversation
…arguments Use the as_integer_ratio() or the numerator and denominator attributes to represent the number as an integer ratio and perform scaling and rounding exactly. This allows to avoid the precision loss and double rounding error due to conversion to float.
5672976 to
90f8bf2
Compare
Co-authored-by: Victor Stinner <vstinner@python.org>
|
This PR is stale because it has been open for 30 days with no activity. |
Documentation build overview
475 files changed ·
|
|
This PR is stale because it has been open for 90 days with no activity. |
|
I'd love to see this in 3.16 so it'd be a shame for it to go stale |
|
This PR is stale because it has been open for 90 days with no activity. |
vstinner
left a comment
There was a problem hiding this comment.
Would it be possible to add some C API tests to test_time?
- test_object_to_time_t(): _PyTime_ObjectToTime_t()
- test_object_to_timeval(): _PyTime_ObjectToTimeval()
- test_object_to_timespec(): _PyTime_ObjectToTimespec()
Or I can try to add some tests once this change will be merged.
…s-integer-ratio # Conflicts: # Include/internal/pycore_global_objects_fini_generated.h # Include/internal/pycore_unicodeobject_generated.h
Also simplify checked_divmod() and clarify the comment of maybe_as_integer_ratio(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…yTime_ObjectToTimespec() The rounding mode was ignored: the fractional part was always rounded half to even. Add tests for conversion of Fraction, Decimal and other numbers with the numerator and denominator attributes in the C API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only the remainder of the division by the denominator is now scaled, as in _PyTime_ObjectToTimeval() and _PyTime_ObjectToTimespec(). Too large values are rejected before the multiplication. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Added tests. They found a bug: the rounding mode was ignored in _PyTime_ObjectToTimeval() and _PyTime_ObjectToTimespec(). Fixed.
vstinner
left a comment
There was a problem hiding this comment.
LGTM. I'm feeling safer with new tests in test_time :-)
| *ratio = NULL; | ||
| if (PyType_Check(number)) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "required a number, not type"); |
There was a problem hiding this comment.
| "required a number, not type"); | |
| "expected a number, not type"); |
"required" reads a little oddly.
There was a problem hiding this comment.
Changed to must be real number, not %T, the same as the error for other non-numbers.
| Py_DECREF(ratio); | ||
| return -1; | ||
| } | ||
| PyTime_t ns; |
There was a problem hiding this comment.
Nit: ns holds whole seconds or milliseconds until the pytime_mul() below, which is confusing next to unit_to_ns. Maybe units?
|
Performance impact: |
|
IMO the performance slowdown is accepted and worth it. Passing Fraction and Decimal as timestamp sounds like an unusual use case. I suppose that if someone does that, it would be to get the best accuracy, not for the best performance. There is already a workaround for performance: use float or int. If later, performance becomes too critical, we may add new functions. For example, add time.sleep_ns(int) which sleeps a number of nanoseconds (integer). |
StanFromIreland
left a comment
There was a problem hiding this comment.
LGTM
I wonder how long till the PSRT will get a report about this performance change. ;-)
|
Looks like this is failing for 32-bit:
|
Use the as_integer_ratio() or the numerator and denominator attributes to represent the number as an integer ratio and perform scaling and rounding exactly.
This allows to avoid the precision loss and double rounding error due to conversion to float.
📚 Documentation preview 📚: https://cpython-previews--139273.org.readthedocs.build/