From cffad22e809ace980f164ad410c20734872f9bce Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 12 Sep 2025 07:27:47 +0300 Subject: [PATCH 1/5] gh-138580: add sys.float_info.stdc_iec_559 boolean flag 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. --- Doc/library/sys.rst | 15 ++++++++++++--- ...2025-09-12-07-30-13.gh-issue-138580.Qr_fSH.rst | 2 ++ Objects/floatobject.c | 5 ++++- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-09-12-07-30-13.gh-issue-138580.Qr_fSH.rst diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 34764a7e4f097b..02dbf2ae0c4e64 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -694,15 +694,15 @@ always available. Unless explicitly noted otherwise, all variables are read-only A :term:`named tuple` holding information about the float type. It contains low level information about the precision and internal representation. The values correspond to the various floating-point - constants defined in the standard header file :file:`float.h` for the 'C' - programming language; see section 5.2.4.2.2 of the 1999 ISO/IEC C standard + constants defined by C implementation and in the standard header file :file:`float.h` for the 'C' + programming language; see Annex F and section 5.2.4.2.2 of the 1999 ISO/IEC C standard [C99]_, 'Characteristics of floating types', for details. .. list-table:: Attributes of the :data:`!float_info` :term:`named tuple` :header-rows: 1 * - attribute - - float.h macro + - C macro - explanation * - .. attribute:: float_info.epsilon @@ -771,6 +771,15 @@ always available. Unless explicitly noted otherwise, all variables are read-only All other values for :c:macro:`!FLT_ROUNDS` characterize implementation-defined rounding behavior. + * - .. attribute:: float_info.stdc_iec_559 + - :c:macro:`!__STDC_IEC_559__` + - A boolean, indicating support the IEC 60559 floating-point + standard (the Annex F of C99). If enabled, the + :class:`float` type characteristics matches the IEC + 60559 double format and exceptional cases for + the :mod:`math` functions follow to the section F.10 + of the C99 standard. + The attribute :attr:`sys.float_info.dig` needs further explanation. If ``s`` is any string representing a decimal number with at most :attr:`!sys.float_info.dig` significant digits, then converting ``s`` to a diff --git a/Misc/NEWS.d/next/Library/2025-09-12-07-30-13.gh-issue-138580.Qr_fSH.rst b/Misc/NEWS.d/next/Library/2025-09-12-07-30-13.gh-issue-138580.Qr_fSH.rst new file mode 100644 index 00000000000000..9037e2f9a914c4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-12-07-30-13.gh-issue-138580.Qr_fSH.rst @@ -0,0 +1,2 @@ +Add a boolean flag, indicating support the IEC 60559 floating-point standard +(the Annex F of C99). Patch by Sergey B Kirpichev. diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 1fefb12803ec19..412763e1154ff6 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -68,6 +68,8 @@ static PyStructSequence_Field floatinfo_fields[] = { {"radix", "FLT_RADIX -- radix of exponent"}, {"rounds", "FLT_ROUNDS -- rounding mode used for arithmetic " "operations"}, + {"stdc_iec_559", "test if implementation supports the IEC 60559" + " floating-point standard"}, {0} }; @@ -75,7 +77,7 @@ static PyStructSequence_Desc floatinfo_desc = { "sys.float_info", /* name */ floatinfo__doc__, /* doc */ floatinfo_fields, /* fields */ - 11 + 12 }; PyObject * @@ -113,6 +115,7 @@ PyFloat_GetInfo(void) SetDblFlag(DBL_EPSILON); SetIntFlag(FLT_RADIX); SetIntFlag(FLT_ROUNDS); + SetFlag(PyBool_FromLong(__STDC_IEC_559__)); #undef SetIntFlag #undef SetDblFlag #undef SetFlag From d81b486e50ec0e35274109bc13caee868ca421e8 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 12 Sep 2025 07:37:24 +0300 Subject: [PATCH 2/5] +1 --- Objects/floatobject.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 412763e1154ff6..cb8d4a679a6efa 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -115,7 +115,11 @@ PyFloat_GetInfo(void) SetDblFlag(DBL_EPSILON); SetIntFlag(FLT_RADIX); SetIntFlag(FLT_ROUNDS); - SetFlag(PyBool_FromLong(__STDC_IEC_559__)); +#ifdef __STDC_IEC_559__ + SetFlag(PyBool_FromLong(1)); +#else + SetFlag(PyBool_FromLong(0)); +#endif #undef SetIntFlag #undef SetDblFlag #undef SetFlag From f9fa06b8ce6c98f5294c0c2d7fa929401bbff907 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 12 Sep 2025 07:44:16 +0300 Subject: [PATCH 3/5] + update math docs --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 55f2de07553d56..bf0e162f04cccd 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -874,7 +874,7 @@ Constants The :mod:`math` module consists mostly of thin wrappers around the platform C math library functions. Behavior in exceptional cases follows Annex F of - the C99 standard where appropriate. The current implementation will raise + the C99 standard, if :attr:`sys.float_info.stdc_iec_559` is enabled. The current implementation will raise :exc:`ValueError` for invalid operations like ``sqrt(-1.0)`` or ``log(0.0)`` (where C99 Annex F recommends signaling invalid operation or divide-by-zero), and :exc:`OverflowError` for results that overflow (for example, From 942ac086772880bfb11d84b676c28dc3c50cbb27 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 12 Sep 2025 07:45:53 +0300 Subject: [PATCH 4/5] + adjust test --- Lib/test/test_sys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 1198c6d35113c8..31c84ec24c0b4b 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -642,7 +642,7 @@ def test_attributes(self): self.assertIsInstance(sys.exec_prefix, str) self.assertIsInstance(sys.base_exec_prefix, str) self.assertIsInstance(sys.executable, str) - self.assertEqual(len(sys.float_info), 11) + self.assertEqual(len(sys.float_info), 12) self.assertEqual(sys.float_info.radix, 2) self.assertEqual(len(sys.int_info), 4) self.assertTrue(sys.int_info.bits_per_digit % 5 == 0) From e5b864274b2e2e05ab2dcca8d59e3d5edfbca58e Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 12 Sep 2025 09:31:56 +0300 Subject: [PATCH 5/5] gh-138573: restrict testAtan2() the IEC 60559-compatible platforms Section 7.12.4.4 of the C17 doesn't specify special values for atan2() (and in particular permit domain error when both arguments are zero). The F.10.1.4 does. --- Lib/test/test_math.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index e3b0d4fa9eeeb3..d6f062eec6c0e1 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -325,6 +325,8 @@ def testAtanh(self): self.assertRaises(ValueError, math.atanh, NINF) self.assertTrue(math.isnan(math.atanh(NAN))) + @unittest.skipIf(not sys.float_info.stdc_iec_559, + "Support for the IEC 60559 floating-point standard expected.") def testAtan2(self): self.assertRaises(TypeError, math.atan2) self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)