Skip to content

Commit 68c7fad

Browse files
skirpichevvstinnerStanFromIreland
authored
gh-138580: Add sys.float_info.iec_60559 boolean flag (#138811)
This value indicating support the IEC 60559 floating-point standard (the Annex F of C99). If enabled, the float type characteristics matches the IEC 60559 double format and exceptional cases for the math's functions follow to the section F.10 of the C99 standard. Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent 6d73bc2 commit 68c7fad

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

Doc/library/math.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,8 @@ Constants
848848

849849
The :mod:`!math` module consists mostly of thin wrappers around the platform C
850850
math library functions. Behavior in exceptional cases follows Annex F of
851-
the C99 standard where appropriate. The current implementation will raise
851+
the C99 standard, if :attr:`sys.float_info.iec_60559` is true.
852+
The current implementation will raise
852853
:exc:`ValueError` for invalid operations like ``sqrt(-1.0)`` or ``log(0.0)``
853854
(where C99 Annex F recommends signaling invalid operation or divide-by-zero),
854855
and :exc:`OverflowError` for results that overflow (for example,

Doc/library/sys.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,16 @@ always available. Unless explicitly noted otherwise, all variables are read-only
694694
A :term:`named tuple` holding information about the float type. It
695695
contains low level information about the precision and internal
696696
representation. The values correspond to the various floating-point
697-
constants defined in the standard header file :file:`float.h` for the 'C'
698-
programming language; see section 5.2.4.2.2 of the 1999 ISO/IEC C standard
699-
[C99]_, 'Characteristics of floating types', for details.
697+
constants defined by C implementation and in the standard header file
698+
:file:`float.h` for the 'C' programming language; see Annex F and section
699+
5.2.4.2.2 of the 1999 ISO/IEC C standard [C99]_, 'Characteristics of
700+
floating types', for details.
700701

701702
.. list-table:: Attributes of the :data:`!float_info` :term:`named tuple`
702703
:header-rows: 1
703704

704705
* - attribute
705-
- float.h macro
706+
- C macro
706707
- explanation
707708

708709
* - .. attribute:: float_info.epsilon
@@ -771,6 +772,12 @@ always available. Unless explicitly noted otherwise, all variables are read-only
771772
All other values for :c:macro:`!FLT_ROUNDS` characterize
772773
implementation-defined rounding behavior.
773774

775+
* - .. attribute:: float_info.iec_60559
776+
- :c:macro:`!__STDC_IEC_559__`
777+
- A boolean, indicating support the IEC 60559 floating-point standard.
778+
If true, the :class:`float` type characteristics and behavior matches
779+
the IEC 60559 double format.
780+
774781
The attribute :attr:`sys.float_info.dig` needs further explanation. If
775782
``s`` is any string representing a decimal number with at most
776783
:attr:`!sys.float_info.dig` significant digits, then converting ``s`` to a

Doc/whatsnew/3.15.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,11 @@ sys
10151015
* Add :data:`sys.abi_info` namespace to improve access to ABI information.
10161016
(Contributed by Klaus Zimmermann in :gh:`137476`.)
10171017

1018+
* Add :data:`sys.float_info.iec_60559 <sys.float_info>`: a boolean flag,
1019+
indicating support the IEC 60559 floating-point standard (as specified by the
1020+
Annex F of C99).
1021+
(Contributed by Sergey B Kirpichev in :gh:`138580`.)
1022+
10181023

10191024
tarfile
10201025
-------

Lib/test/test_sys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ def test_attributes(self):
641641
self.assertIsInstance(sys.exec_prefix, str)
642642
self.assertIsInstance(sys.base_exec_prefix, str)
643643
self.assertIsInstance(sys.executable, str)
644-
self.assertEqual(len(sys.float_info), 11)
644+
self.assertEqual(len(sys.float_info), 12)
645645
self.assertEqual(sys.float_info.radix, 2)
646646
self.assertEqual(len(sys.int_info), 4)
647647
self.assertTrue(sys.int_info.bits_per_digit % 5 == 0)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :data:`sys.float_info.iec_60559 <sys.float_info>`: a boolean flag,
2+
indicating support the IEC 60559 floating-point standard (as specified by the
3+
Annex F of C99). Patch by Sergey B Kirpichev.

Objects/floatobject.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ static PyStructSequence_Field floatinfo_fields[] = {
6868
{"radix", "FLT_RADIX -- radix of exponent"},
6969
{"rounds", "FLT_ROUNDS -- rounding mode used for arithmetic "
7070
"operations"},
71+
{"iec_60559", "test if implementation supports the IEC 60559 "
72+
"floating-point standard"},
7173
{0}
7274
};
7375

7476
static PyStructSequence_Desc floatinfo_desc = {
7577
"sys.float_info", /* name */
7678
floatinfo__doc__, /* doc */
7779
floatinfo_fields, /* fields */
78-
11
80+
12
7981
};
8082

8183
PyObject *
@@ -113,6 +115,11 @@ PyFloat_GetInfo(void)
113115
SetDblFlag(DBL_EPSILON);
114116
SetIntFlag(FLT_RADIX);
115117
SetIntFlag(FLT_ROUNDS);
118+
#ifdef __STDC_IEC_559__
119+
SetFlag(PyBool_FromLong(1));
120+
#else
121+
SetFlag(PyBool_FromLong(0));
122+
#endif
116123
#undef SetIntFlag
117124
#undef SetDblFlag
118125
#undef SetFlag

0 commit comments

Comments
 (0)