Skip to content

gh-114667: Support hexadecimal floating-point literals #114668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from

Conversation

skirpichev
Copy link
Member

@skirpichev skirpichev commented Jan 28, 2024

This add hexadecimal floating-point literals (IEEE 754-2008 §5.12.3) and support construction of floats from hexadecimal strings. Note that the syntax is more permissive: everything that is currently accepted by the float.fromhex(), but with a mandatory base specifier; it also allows grouping digits with underscores.

Examples:

>>> 0x1.1p-1
0.53125
>>> float('0x1.1')
1.0625
>>> 0x1.1
1.0625
>>> 0x1.1_1_1
1.066650390625

Using hexadecimal floating-point literals allow us directly construct exact arbitrary floating-point values, which was available before via float.fromhex() class method. The later is less convenient, especially doing interactive work.

This also could be used by some projects in the python ecosystem to provide multiple precision binary floating-point arithmetic in CLI interface: hexadecimal floating-point numbers now are valid tokens and can be wrapped by appropriate type, e.g. gmpy2.mpfr. See the Decimal math example on how it's possible with decimal floating-point numbers.

Minor changes:


Notes

As now it's requires a PEP, here is a PEP draft.

Open issues:

  • Should the exponent part be mandatory?
  • Should we change the float constructor? This doesn't make much sense in the current version, where the exponent is optional.
  • Should we adapt tests to use hexadecimal literals instead of more verbose float.fromhex()?
  • Should we deprecate float.fromhex()?

Complementary PR: #113805 (formatted output)



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

This add hexadecimal floating point literals (IEEE 754-2008 §5.12.3) and
support construction of floats from hexadecimal strings.  Note that the
syntax is more permissive: everything that is currently accepted by the
``float.fromhex()``, but with a mandatory base specifier; it also allows
grouping digits with underscores.

Examples:
```pycon
>>> 0x1.1p-1
0.53125
>>> float('0x1.1')
1.0625
>>> 0x1.1
1.0625
>>> 0x1.1_1_1
1.066650390625
```

Minor changes:
* Py_ISDIGIT/ISXDIGIT macros were transformed to functions
E.g. 0x1.bit_length() will not require parentheses around the
hexadecimal integer literal (like 1.bit_length() for decimal int).
@skirpichev skirpichev marked this pull request as ready for review April 2, 2024 06:03
@skirpichev
Copy link
Member Author

Now it is ready for review. The second commit should clear syntax incompatibility wrt access to public attributes on hexadecimal integer literals, e.g. 0x1.to_bytes.

One note for reviewers (also valid for #113805, but more relevant here): do we still support non-IEEE 754 platforms? Build requirements claims: "Support for IEEE 754 floating point numbers and floating point Not-a-Number (NaN)." is required.

The current pr has special case, using the system strtod() if _PY_SHORT_FLOAT_REPR == 0. This was tested locally on IEEE system. But will this be tested by some CPython's build bot (hardly it's tested by regular GA checks)? More generally, is there reasons (after 10+ years) to not use system's strtod() (in C locale) to do hard work?

Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

This needs a PEP accepted before it can be reviewed

@bedevere-app
Copy link

bedevere-app bot commented Apr 2, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@skirpichev
Copy link
Member Author

skirpichev commented Apr 2, 2024

PEP Draft and pr were updated to match each other. I'll convert this pr to a draft if one will touch some new files.

@skirpichev skirpichev marked this pull request as draft May 31, 2024 03:42
@skirpichev skirpichev changed the title gh-114667: Support hexadecimal floating point literals gh-114667: Support hexadecimal floating-point literals Jul 19, 2024
@skirpichev skirpichev closed this Apr 12, 2025
@skirpichev skirpichev deleted the hex-floatlit branch April 12, 2025 10:09
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.

2 participants