You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
struct (un)packing of half-precision may turn NaN into ±inf #141042
Repeating this with FFF0000000080001 you'll see nan turn into -inf.
This only happens in Python 3.14+ (not 3.13). I'm pretty sure that this fix is causing it: #130317
Preserving the signaling/quieting bit is a great idea, I find. But since the mantissa is being truncated, one must ensure that the narrower width mantissa doesn't end up being all 0 bits as that represents inf instead of nan.
Is this guaranteed across compilers and platforms? I know for instance, that LLVM doesn't make many guarantees about how NaNs behave in this regard. https://llvm.org/docs/LangRef.html#floatnan
Note that it's not even guaranteed by IEEE 754 that for a NaN, the first bit of the significand field is 0 for signaling and 1 for quiet - it could be the other way around (0 for quiet, 1 for signaling). IEEE 754 recommends the first way but doesn't require it. (The verb used is "should" rather than "shall".)
The relevant section of IEEE 754-2019 is §6.2.1 ("NaN encodings in binary interchange formats").
Fortunately, this was silently standardized by CPython long time ago. Maybe it should be documented, as we have at least one buildbot with reversed meaning of the first bit.
this was silently standardized by CPython long time ago
Can you say more about this? How can CPython standardise something that's determined by the hardware, and that differs from machine to machine? That doesn't seem possible - CPython can't dictate how the hardware behaves.
How can CPython standardise something that's determined by the hardware, and that differs from machine to machine?
Ah, sorry for a bad joke. This was in the context of (un)packing of various floating-point formats. While for floats/doubles we rely on hardware (on IEEE boxes), for the half-precision format its not. Here how it happens in v3.13:
Activity
skirpichev commentedon Nov 5, 2025
It certainly does make sense for same floating-point format on round-trip.
I see following alternatives:
(2) always happens in C for type-cast:
Details
pythongh-141042: fix sNaN's (un)packing for mixed floating-point formats
robsdedude commentedon Nov 6, 2025
Is this guaranteed across compilers and platforms? I know for instance, that LLVM doesn't make many guarantees about how NaNs behave in this regard. https://llvm.org/docs/LangRef.html#floatnan
skirpichev commentedon Nov 6, 2025
No, of course. See e.g. #133328.
mdickinson commentedon Nov 6, 2025
Note that it's not even guaranteed by IEEE 754 that for a NaN, the first bit of the significand field is 0 for signaling and 1 for quiet - it could be the other way around (0 for quiet, 1 for signaling). IEEE 754 recommends the first way but doesn't require it. (The verb used is "should" rather than "shall".)
The relevant section of IEEE 754-2019 is §6.2.1 ("NaN encodings in binary interchange formats").
skirpichev commentedon Nov 6, 2025
Fortunately, this was silently standardized by CPython long time ago. Maybe it should be documented, as we have at least one buildbot with reversed meaning of the first bit.
mdickinson commentedon Nov 6, 2025
Can you say more about this? How can CPython standardise something that's determined by the hardware, and that differs from machine to machine? That doesn't seem possible - CPython can't dictate how the hardware behaves.
skirpichev commentedon Nov 6, 2025
Ah, sorry for a bad joke. This was in the context of (un)packing of various floating-point formats. While for floats/doubles we rely on hardware (on IEEE boxes), for the half-precision format its not. Here how it happens in v3.13:
cpython/Objects/floatobject.c
Lines 2079 to 2088 in 7a3b035
No doubts, here clearly stated that some very specific qNaN's (two) have 0b1000000000 mantissa.
PS: Recommended encoding was proposed by the IEEE 754-2008.
gh-141042: fix sNaN's packing for mixed floating-point formats (#141107)
pythongh-141042: fix sNaN's packing for mixed floating-point formats (p…
vstinner commentedon Nov 12, 2025
Thanks for your bug report.
Fixed in the main branch by the change 23d85a2. The backport to 3.14 will be merged after the CI tests pass.
[3.14] gh-141042: fix sNaN's packing for mixed floating-point formats (…
pythongh-141042: fix sNaN's packing for mixed floating-point formats (p…