Skip to content

gh-67795: Rewrite pytime_from_pyfloat() - #162940

Merged
vstinner merged 5 commits into
python:mainfrom
vstinner:pytime_from_pytfloat
Dec 3, 2026
Merged

vstinner merged 5 commits into
python:mainfrom
vstinner:pytime_from_pytfloat

Conversation

@vstinner

@vstinner vstinner commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Convert the value to a number of nanoseconds using integers (PyTime_t), instead of doing the computation using doubles to avoid loss of precision.

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 StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add the fixups from the other one here?

@vstinner

vstinner commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

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)
8223372036854776832

Example 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

-8388607.999999999 is rounded differently. There is a difference of 1 nanosecond compared to before.

For tmax, the difference is 450 ns. With the change:

>>> int(tmax*1e9) - _testinternalcapi._PyTime_FromSecondsObject(tmax, ROUND_FLOOR)
450

@vstinner

vstinner commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@StanFromIreland

Maybe add the fixups from the other one here?

I'm missing context. What do you mean by "the other one"?

@vstinner

vstinner commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@StanFromIreland: I addressed your review in this PR. Do you want to double check this change?

Maybe add the fixups from the other one here?

Ah, I suppose that you're referring to your review on my other pytime.c change: #162904 (review).

@vstinner

vstinner commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Tests / Windows / Build and test (x64, switch-case) (pull_request): Failing after 17m

test_shutil failed. Strange, my PR is unrelated to shutil:

test_tarfile_vs_tar (test.test_shutil.TestArchives.test_tarfile_vs_tar) ... tar: dist: Can't add archive to itself
FAIL

======================================================================
FAIL: test_tarfile_vs_tar (test.test_shutil.TestArchives.test_tarfile_vs_tar)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\cpython\cpython\Lib\test\test_shutil.py", line 1962, in test_tarfile_vs_tar
    self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Tuples differ: ('dist', 'dist/file1', 'dist/file2', 'dist/sub', 'dist/sub/file3', 'dist/sub2') != ('dist/file1', 'dist/file2', 'dist/sub', 'dist/sub/file3', 'dist/sub2')

@vstinner
vstinner enabled auto-merge (squash) December 2, 2026 23:41
@vstinner
vstinner merged commit 5f4aeba into python:main Dec 3, 2026
70 of 71 checks passed
@vstinner
vstinner deleted the pytime_from_pytfloat branch December 3, 2026 00:15
@StanFromIreland

Copy link
Copy Markdown
Member

I can do a post merge review this weekend, I'm a little swamped this week.

Comment thread Python/pytime.c
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);

@StanFromIreland StanFromIreland Dec 4, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@bedevere-app

bedevere-app Bot commented Dec 4, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants