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.

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