Skip to content

Commit 70f72fb

Browse files
committed
gh-145633: amend dae85c4 (refine notes on non-IEEE platforms)
* we don't specify what happens on non-IEEE platforms * use rather PY_LITTLE_ENDIAN to get native endianness * mention that unpack functions don't fail in CPython * mention that PyFloat_Pack8 doesn't fail in CPython
1 parent d19de37 commit 70f72fb

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

Doc/c-api/float.rst

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,21 @@ string from a C :c:expr:`double`, and the Unpack routines produce a C
192192
:c:expr:`double` from such a bytes string. The suffix (2, 4 or 8) specifies the
193193
number of bytes in the bytes string.
194194
195-
On platforms that appear to use IEEE 754 formats these functions work by
196-
copying bits. On other platforms, the 2-byte format is identical to the IEEE
197-
754 binary16 half-precision format, the 4-byte format (32-bit) is identical to
198-
the IEEE 754 binary32 single precision format, and the 8-byte format to the
199-
IEEE 754 binary64 double precision format, although the packing of INFs and
200-
NaNs (if such things exist on the platform) isn't handled correctly, and
201-
attempting to unpack a bytes string containing an IEEE INF or NaN will raise an
202-
exception.
203-
204-
Note that NaNs type may not be preserved on IEEE platforms (signaling NaN become
205-
quiet NaN), for example on x86 systems in 32-bit mode.
206-
195+
The 2-byte format is the IEEE 754 binary16 half-precision format, the 4-byte
196+
format is the IEEE 754 binary32 single precision format, and the 8-byte format
197+
is the IEEE 754 binary64 double precision format, although the NaNs type may
198+
not be preserved on some platforms while unpacking (signaling NaN become quiet
199+
NaN), for example on x86 systems in 32-bit mode.
200+
201+
It's assumed, that the :c:expr:`double` type has the IEEE 754 binary64 double
202+
precision format. What happens if it's not true is partly accidental (alas).
207203
On non-IEEE platforms with more precision, or larger dynamic range, than IEEE
208204
754 supports, not all values can be packed; on non-IEEE platforms with less
209-
precision, or smaller dynamic range, not all values can be unpacked. What
210-
happens in such cases is partly accidental (alas).
205+
precision, or smaller dynamic range, not all values can be unpacked. The
206+
packing of special numbers like INFs and NaNs (if such things exist on the
207+
platform) may be not handled correctly, and attempting to unpack a bytes string
208+
containing an IEEE INF or NaN may raise an exception.
209+
211210
212211
.. versionadded:: 3.11
213212
@@ -217,13 +216,16 @@ Pack functions
217216
The pack routines write 2, 4 or 8 bytes, starting at *p*. *le* is an
218217
:c:expr:`int` argument, non-zero if you want the bytes string in little-endian
219218
format (exponent last, at ``p+1``, ``p+3``, or ``p+6`` ``p+7``), zero if you
220-
want big-endian format (exponent first, at *p*). The :c:macro:`PY_BIG_ENDIAN`
221-
constant can be used to use the native endian: it is equal to ``1`` on big
222-
endian processor, or ``0`` on little endian processor.
219+
want big-endian format (exponent first, at *p*). The :c:macro:`PY_LITTLE_ENDIAN`
220+
constant can be used to use the native endian: it is equal to ``0`` on big
221+
endian processor, or ``1`` on little endian processor.
223222
224223
Return value: ``0`` if all is OK, ``-1`` if error (and an exception is set,
225224
most likely :exc:`OverflowError`).
226225
226+
.. impl-detail::
227+
The :c:func:`PyFloat_Pack8` function always succeeds in the CPython.
228+
227229
.. c:function:: int PyFloat_Pack2(double x, char *p, int le)
228230
229231
Pack a C double as the IEEE 754 binary16 half-precision format.
@@ -243,14 +245,17 @@ Unpack functions
243245
The unpack routines read 2, 4 or 8 bytes, starting at *p*. *le* is an
244246
:c:expr:`int` argument, non-zero if the bytes string is in little-endian format
245247
(exponent last, at ``p+1``, ``p+3`` or ``p+6`` and ``p+7``), zero if big-endian
246-
(exponent first, at *p*). The :c:macro:`PY_BIG_ENDIAN` constant can be used to
247-
use the native endian: it is equal to ``1`` on big endian processor, or ``0``
248+
(exponent first, at *p*). The :c:macro:`PY_LITTLE_ENDIAN` constant can be used to
249+
use the native endian: it is equal to ``0`` on big endian processor, or ``1``
248250
on little endian processor.
249251
250252
Return value: The unpacked double. On error, this is ``-1.0`` and
251253
:c:func:`PyErr_Occurred` is true (and an exception is set, most likely
252254
:exc:`OverflowError`).
253255
256+
.. impl-detail::
257+
These functions always succeed in the CPython.
258+
254259
.. c:function:: double PyFloat_Unpack2(const char *p, int le)
255260
256261
Unpack the IEEE 754 binary16 half-precision format as a C double.

0 commit comments

Comments
 (0)