Skip to content

gh-138580: add sys.float_info.iec_60559 boolean flag#138811

Merged
vstinner merged 15 commits intopython:mainfrom
skirpichev:annex-f-flag/138580
Mar 24, 2026
Merged

gh-138580: add sys.float_info.iec_60559 boolean flag#138811
vstinner merged 15 commits intopython:mainfrom
skirpichev:annex-f-flag/138580

Conversation

@skirpichev
Copy link
Member

@skirpichev skirpichev commented Sep 12, 2025

This value indicating support the IEC 60559 floating-point standard (specified by the Annex F of C99, for C language). 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.


📚 Documentation preview 📚: https://cpython-previews--138811.org.readthedocs.build/

https://cpython-previews--138811.org.readthedocs.build/en/138811/library/sys.html#sys.float_info
https://cpython-previews--138811.org.readthedocs.build/en/138811/library/math.html#math.nan

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.
@picnixz
Copy link
Member

picnixz commented Sep 13, 2025

I think it would be better if the field was in uppercase but I don't know if we have many other cases like that.

@skirpichev
Copy link
Member Author

I think it would be better if the field was in uppercase

This will be inconsistent wrt the rest (other field names are also come from macros).

@skirpichev skirpichev requested a review from vstinner September 13, 2025 12:59
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Co-authored-by: Victor Stinner <vstinner@python.org>
@skirpichev
Copy link
Member Author

I'm thinking if it does make sense to mention this flag in cmath docs.

We could say that:

  1. Functions selection mostly follows to the Annex G.6 section (modulo conversions and classifications functions). Currently we just mention that functions set is different wrt the math module.
  2. Functions behavior follows to the Annex G.6 in exceptional cases (well, with some exceptions, see gh-133895: correct values of cmath.cosh/sinh in case of overflows #135603, which aren't user-visible right now), if sys.float_info.stdc_iec_559 is true.

@vstinner
Copy link
Member

@picnixz @serhiy-storchaka: What do you think of this change?

@serhiy-storchaka
Copy link
Member

I am not sure what is the best place for this constant. What do @mdickinson and @tim-one think about this? Other options are discussed in the issue.

@picnixz
Copy link
Member

picnixz commented Sep 17, 2025

I'm not really comfortable with float_info but I don't really know whether a module-wide constant is better. I would be more comfortable with a new struct, say stdc_info or features_info which contains other feature for the C standard.

@skirpichev
Copy link
Member Author

I would be more comfortable with a new struct, say stdc_info or features_info which contains other feature for the C standard.

I think, this flag should rather describe conformance with the IEEE 754 / IEC 60559.

@skirpichev skirpichev requested a review from vstinner October 29, 2025 07:57
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@skirpichev
Copy link
Member Author

Lets try to make some progress here.

I am not sure what is the best place for this constant. Other options are discussed in the issue.

@serhiy-storchaka, other options so far:

  1. sysconfig
  2. some flag in sys.stdc_info or sys.features_info, which contains other features for the C standard.
  3. some flag in the sys module namespace.

(1) or (2) don't make any sense, as the flag is not C-specific at all. So, (3) is only option. But the sys namespace is already big. And the new flag logically fits float_info: it offers some additional information about float type.

Maybe we should rename this flag to sys.float_info.iec_60559?

Other fields in this named tuple also have no strict binding to the C standard. Floating-point numbers in C are defined by same model as in the IEC 60559, i.e. have radix ($b$), precision ($p$), emax ($e_{\rm max}$) and emin ($e_{\rm min}$) parameters. Everything else can be expressed via such parameters, e.g. min_10_exp is just $\lceil\log_{10}b^{e_{\rm min}-1}\rceil$. In other words, references to C macros might be convenient here, but these field are actually language-agnostic and do make sense e.g. for the RustPython. Maybe it's better to avoid mentioning the C standard here?

@vstinner
Copy link
Member

vstinner commented Nov 20, 2025

Maybe we should rename this flag to sys.float_info.iec_60559?

I still support this option (sys.float_info).

@vstinner vstinner changed the title gh-138580: add sys.float_info.stdc_iec_559 boolean flag gh-138580: add sys.float_info.iec_60559 boolean flag Nov 20, 2025
@vstinner
Copy link
Member

@serhiy-storchaka:

I am not sure what is the best place for this constant. What do @mdickinson and @tim-one think about this? Other options are discussed in the issue.

@mdickinson and @tim-one didn't reply. Would you be ok with sys.float_info.iec_60559 flag? IMO it's a good fit.

@skirpichev
Copy link
Member Author

@serhiy-storchaka, see #145633 (comment).

It looks like the float.__getformat__() class method often misused in the CPython tests. The sys.float_info.iec_60559 flag is a good replacement for this private interface. The sysconfig module doesn't look as a right place to keep this stuff, just because it's dependency on Python implementation (we don't mention somewhere in docs which variables must be available here and how they should be named).

IMO, we only have to decide: where in the sys module put this flag?

@skirpichev
Copy link
Member Author

Maybe open a DPO thread?

@vstinner vstinner requested a review from AA-Turner as a code owner March 24, 2026 11:09
@vstinner vstinner enabled auto-merge (squash) March 24, 2026 11:11
@vstinner vstinner merged commit 68c7fad into python:main Mar 24, 2026
51 checks passed
@skirpichev skirpichev deleted the annex-f-flag/138580 branch March 24, 2026 11:55
@vstinner
Copy link
Member

I decided to merge the change adding sys.float_info.iec_60559 attribute. I completed the PR to mention the addition in What's New in Python 3.15.

Since sys.float_info also maps other C float macros, IMO it's a good home to add iec_60559.

@vstinner
Copy link
Member

Build info on CI runs on this PR:

  • Ubuntu x86-64: sys.float_info: sys.float_info(..., iec_60559=True)
  • macOS 14: sys.float_info: sys.float_info(..., iec_60559=False)
  • Windows x64: sys.float_info: sys.float_info(..., iec_60559=False)
  • Emscripten: sys.float_info: sys.float_info(..., iec_60559=False)
  • WASI: sys.float_info: sys.float_info(..., iec_60559=False)

So iec_60559 is only true on Ubuntu CI.

@skirpichev
Copy link
Member Author

Yes, it looks adoption of the standard is poor as before.

@vstinner, maybe it does make sense to revert this pr. Certainly, this flag can't be used for the support.requires_IEEE_754 decorator - it filters out too much.

@vstinner
Copy link
Member

I also looked at buildbots.

iec_60559=True:

  • Debian root: sys.float_info(..., iec_60559=True)
  • AMD64 CentOS9 NoGIL 3.x: sys.float_info(..., iec_60559=True)
  • ARM Raspbian 3.x: sys.float_info(..., iec_60559=True)
  • PPC64LE CentOS9 3.x: sys.float_info(..., iec_60559=True)
  • riscv64 Ubuntu23 3.x: sys.float_info(..., iec_60559=True)

iec_60559=False:

  • FreeBSD 13: sys.float_info(..., iec_60559=False)
  • FreeBSD 14: sys.float_info(..., iec_60559=False)
  • FreeBSD 15: sys.float_info(..., iec_60559=False)
  • ARM64 MacOS M1 NoGIL 3.x: sys.float_info(..., iec_60559=False)
  • SPARCv9 Oracle Solaris 11.4 3.x: sys.float_info(..., iec_60559=False)
  • WASM Emscripten 3.x: sys.float_info(..., iec_60559=False)
  • wasm32-wasi 3.x: sys.float_info(..., iec_60559=False)

Note: Some buildbots didn't run yet with the new iec_60559 attribute (I restarted the buildbot server this morning, it was stuck for an unknown reason). Some buildbots don't dump test.pythoninfo (ex: Android).

@vstinner
Copy link
Member

@vstinner, maybe it does make sense to revert this pr.

Why? It remains useful in the platforms where it's true, no?

Certainly, this flag can't be used for the support.requires_IEEE_754 decorator - it filters out too much.

Well, that's another issue.

@vstinner
Copy link
Member

I built Python with clang 21.1.8 on my Fedora 43 with glibc 2.42-10: sys.float_info(..., iec_60559=True).

That's surprising because https://clang.llvm.org/c_status.html says about "C99 implementation status" that "Clang does not define __STDC_IEC_559__":

Partial
Clang supports much of the language requirements for Annex F, but full conformance is only possible to determine when considering the compiler's language support, the C runtime library's math library support, and the target system's floating-point environment support. Clang does not currently raise an "invalid" floating-point exception on certain conversions, does not raise floating-point exceptions for arithmetic constant expressions, and other corner cases. Note, Clang does not define __STDC_IEC_559__ because the compiler does not fully conform. However, some C standard library implementations ( glibc, musl will define the macro regardless of compiler support unless the compiler defines __GCC_IEC_559, which Clang does not currently define.
Additionally, Clang intentionally will not conform to Annex F on 32-bit x86 without SSE2 due to the behavior of floating-point operations in x87.

/usr/include/stdc-predef.h contains:

/* glibc's intent is to support the IEC 559 math functionality, real
   and complex.  If the GCC (4.9 and later) predefined macros
   specifying compiler intent are available, use them to determine
   whether the overall intent is to support these features; otherwise,
   presume an older compiler has intent to support these features and
   define these macros by default.  */

#ifdef __GCC_IEC_559
# if __GCC_IEC_559 > 0
#  define __STDC_IEC_559__		1
#  define __STDC_IEC_60559_BFP__ 	201404L
# endif
#else
# define __STDC_IEC_559__		1
# define __STDC_IEC_60559_BFP__ 	201404L
#endif

@skirpichev
Copy link
Member Author

iec_60559=True

That's essentially linux and glibc with "intent is to support" ("An implementation that defines STDC_IEC_559 shall conform to the specifications in this annex." - this is not about best wishes, right?). The rest looks same as 20+ years ago :(, quite disappointing for me.

It remains useful in the platforms where it's true, no?

Yes, at least we can fill bugreports, when it's enabled and things goes wrong.

The cons is that this not useful in the test suite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants