Repository navigation
gh-67795: Rewrite pytime_from_pyfloat() - #162940
Conversation
Convert the value to a number of nanoseconds using integers (PyTime_t), instead of doing the computation using doubles to avoid loss of precision.
StanFromIreland
left a comment
There was a problem hiding this comment.
Maybe add the fixups from the other one here?
|
Example on the current main branch: >>> ROUND_FLOOR = 0
>>> ROUND_CEILING = 1
>>> import _testinternalcapi
>>> _testinternalcapi._PyTime_FromSecondsObject(-8388607.999999999, ROUND_FLOOR)
-8388607999999999
>>> _testinternalcapi._PyTime_FromSecondsObject(-8388607.999999999, ROUND_CEILING)
-8388607999999999
>>> tmax=(2**63-1) / 10**9 - 1e9
>>> _testinternalcapi._PyTime_FromSecondsObject(tmax, ROUND_CEILING)
8223372036854776832
>>> _testinternalcapi._PyTime_FromSecondsObject(tmax, ROUND_FLOOR)
8223372036854776832Example with the change: >>> ROUND_FLOOR = 0
>>> ROUND_CEILING = 1
>>> import _testinternalcapi
>>> _testinternalcapi._PyTime_FromSecondsObject(-8388607.999999999, ROUND_FLOOR)
-8388608000000000
>>> _testinternalcapi._PyTime_FromSecondsObject(-8388607.999999999, ROUND_CEILING)
-8388607999999999
>>> tmax=(2**63-1) / 10**9 - 1e9
>>> _testinternalcapi._PyTime_FromSecondsObject(tmax, ROUND_FLOOR)
8223372036854776382
>>> _testinternalcapi._PyTime_FromSecondsObject(tmax, ROUND_CEILING)
8223372036854776383
For tmax, the difference is 450 ns. With the change: >>> int(tmax*1e9) - _testinternalcapi._PyTime_FromSecondsObject(tmax, ROUND_FLOOR)
450 |
I'm missing context. What do you mean by "the other one"? |
The argument cannot be NaN.
|
@StanFromIreland: I addressed your review in this PR. Do you want to double check this change?
Ah, I suppose that you're referring to your review on my other pytime.c change: #162904 (review). |
test_shutil failed. Strange, my PR is unrelated to shutil: |
|
I can do a post merge review this weekend, I'm a little swamped this week. |
| d = pytime_round(d, round); | ||
| // Multiply by unit_to_ns using integers (PyTime_t), instead of double | ||
| // to avoid loss of precision. | ||
| floatpart = modf(value, &intpart); |
There was a problem hiding this comment.
This now reveals floating point errors, which were hidden before...
>>> from _testinternalcapi import _PyTime_FromSecondsObject
>>> _PyTime_FromSecondsObject(1.1, 3)
1100000001
This is also exposed by the socket timeouts which call PyTime_AsSecondsDouble:
>>> import socket
>>> s = socket.socket()
>>> s.settimeout(1.1)
>>> s.gettimeout()
1.100000001
|
When you're done making the requested changes, leave the comment: |
Convert the value to a number of nanoseconds using integers (PyTime_t), instead of doing the computation using doubles to avoid loss of precision.