Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Address Mark's review
  • Loading branch information
vstinner committed Mar 10, 2022
commit 9371cde337f22333eb31b9a7f9eca255a3b95ab0
54 changes: 28 additions & 26 deletions Doc/c-api/float.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,18 @@ Floating Point Objects
Pack and Unpack functions
=========================

The :mod:`struct` and :mod:`pickle` (at least) modules need an efficient
platform- independent way to store floating-point values as byte strings. The
Pack routines produce a bytes string from a C :c:type:`double`, and the Unpack
routines produce a C :c:type:`double` from such a bytes string. The suffix
(2, 4 or 8) specifies the number of bytes in the bytes string.

On platforms that appear to use IEEE-754 formats these functions work by
copying bits. On other platforms, the formats the 4- byte format (32-bit) is
identical to the IEEE-754 single precision format, and the 8-byte format
(64-bit) to the IEEE-754 double precision format, although the packing of INFs
and NaNs (if such things exist on the platform) isn't handled correctly, and
The pack and unpack functions provide an efficient platform-independent way to
store floating-point values as byte strings. The Pack routines produce a bytes
string from a C :c:type:`double`, and the Unpack routines produce a C
:c:type:`double` from such a bytes string. The suffix (2, 4 or 8) specifies the
number of bytes in the bytes string.

On platforms that appear to use IEEE 754 formats these functions work by
copying bits. On other platforms, the 2-byte format is identical to the IEEE
754 binary16 half-precision format, the 4-byte format (32-bit) is identical to
the IEEE 754 binary32 single precision format, and the 8-byte format to the
IEEE 754 binary64 double precision format, although the packing of INFs and
NaNs (if such things exist on the platform) isn't handled correctly, and
attempting to unpack a bytes string containing an IEEE INF or NaN will raise an
exception.

Expand All @@ -105,40 +106,41 @@ happens in such cases is partly accidental (alas).
Pack functions
--------------

The pack routines write 2, 4 or 8 bytes, starting at *p*. *le* is a bool
argument, true if you want the bytes string in little-endian format (exponent
last, at ``p+1``, ``p+3`` or ``p+7``), false if you want big-endian format
(exponent first, at *p*).
The pack routines write 2, 4 or 8 bytes, starting at *p*. *le* is an
:c:type:`int` argument, non-zero if you want the bytes string in little-endian
format (exponent last, at ``p+1``, ``p+3``, or ``p+6`` ``p+7``), zero if you
want big-endian format (exponent first, at *p*).

Return value: ``0`` if all is OK, ``-1`` if error (and an exception is set,
most likely :exc:`OverflowError`).

There are two problems on non-IEEE platforms:

* What this does is undefined if *x* is a NaN or infinity.
* -0.0 and +0.0 produce the same bytes string.
* ``-0.0`` and ``+0.0`` produce the same bytes string.

.. c:function:: int PyFloat_Pack2(double x, unsigned char *p, int le)

Pack a C double as 16-bit.
Pack a C double as the IEEE 754 binary16 half-precision format.

.. c:function:: int PyFloat_Pack4(double x, unsigned char *p, int le)

Pack a C double as 32-bit.
Pack a C double as the IEEE 754 binary32 single precision format.

.. c:function:: int PyFloat_Pack8(double x, unsigned char *p, int le)

Pack a C double as 64-bit.
Pack a C double as the IEEE 754 binary64 double precision format.


Unpack functions
----------------

The unpack routines read 2, 4 or 8 bytes, starting at *p*. *le* is a bool
argument, true if the bytes string is in little-endian format (exponent last,
at ``p+1``, ``p+3`` or ``p+7``), false if big-endian (exponent first, at *p*).
The unpack routines read 2, 4 or 8 bytes, starting at *p*. *le* is an
:c:type:`int` argument, non-zero if the bytes string is in little-endian format
(exponent last, at ``p+1``, ``p+3`` or ``p+6`` and ``p+7``), zero if big-endian
(exponent first, at *p*).

Return value: The unpacked double. On error, this is ``-1.0`` and
Return value: The unpacked double. On error, this is ``-1.0`` and
:c:func:`PyErr_Occurred` is true (and an exception is set, most likely
:exc:`OverflowError`).

Expand All @@ -147,12 +149,12 @@ represents a NaN or infinity.

.. c:function:: double PyFloat_Unpack2(const unsigned char *p, int le)

Unpack 16-bit as a C double.
Unpack the IEEE 754 binary16 half-precision format as a C double.

.. c:function:: double PyFloat_Unpack4(const unsigned char *p, int le)

Unpack 32-bit as a C double.
Unpack the IEEE 754 binary32 single precision format as a C double.

.. c:function:: double PyFloat_Unpack8(const unsigned char *p, int le)

Unpack 64-bit as a C double.
Unpack the IEEE 754 binary64 double precision format as a C double.
40 changes: 0 additions & 40 deletions Include/cpython/floatobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,10 @@ typedef struct {
#define PyFloat_AS_DOUBLE(op) (((PyFloatObject *)(op))->ob_fval)


/* PyFloat_{Pack,Unpack}{4,8}
*
* The struct and pickle (at least) modules need an efficient platform-
* independent way to store floating-point values as byte strings.
* The Pack routines produce a string from a C double, and the Unpack
* routines produce a C double from such a string. The suffix (4 or 8)
* specifies the number of bytes in the string.
*
* On platforms that appear to use (see _PyFloat_Init()) IEEE-754 formats
* these functions work by copying bits. On other platforms, the formats the
* 4- byte format is identical to the IEEE-754 single precision format, and
* the 8-byte format to the IEEE-754 double precision format, although the
* packing of INFs and NaNs (if such things exist on the platform) isn't
* handled correctly, and attempting to unpack a string containing an IEEE
* INF or NaN will raise an exception.
*
* On non-IEEE platforms with more precision, or larger dynamic range, than
* 754 supports, not all values can be packed; on non-IEEE platforms with less
* precision, or smaller dynamic range, not all values can be unpacked. What
* happens in such cases is partly accidental (alas).
*/

/* The pack routines write 2, 4 or 8 bytes, starting at p. le is a bool
* argument, true if you want the string in little-endian format (exponent
* last, at p+1, p+3 or p+7), false if you want big-endian format (exponent
* first, at p).
* Return value: 0 if all is OK, -1 if error (and an exception is
* set, most likely OverflowError).
* There are two problems on non-IEEE platforms:
* 1): What this does is undefined if x is a NaN or infinity.
* 2): -0.0 and +0.0 produce the same string.
*/
PyAPI_FUNC(int) PyFloat_Pack2(double x, char *p, int le);
PyAPI_FUNC(int) PyFloat_Pack4(double x, char *p, int le);
PyAPI_FUNC(int) PyFloat_Pack8(double x, char *p, int le);

/* The unpack routines read 2, 4 or 8 bytes, starting at p. le is a bool
* argument, true if the string is in little-endian format (exponent
* last, at p+1, p+3 or p+7), false if big-endian (exponent first, at p).
* Return value: The unpacked double. On error, this is -1.0 and
* PyErr_Occurred() is true (and an exception is set, most likely
* OverflowError). Note that on a non-IEEE platform this will refuse
* to unpack a string that represents a NaN or infinity.
*/
PyAPI_FUNC(double) PyFloat_Unpack2(const char *p, int le);
PyAPI_FUNC(double) PyFloat_Unpack4(const char *p, int le);
PyAPI_FUNC(double) PyFloat_Unpack8(const char *p, int le);