-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Conversation
be23b34
to
220be10
Compare
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).
Now it is ready for review. The second commit should clear syntax incompatibility wrt access to public attributes on hexadecimal integer literals, e.g. 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 |
There was a problem hiding this 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
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 |
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. |
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:
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:
Py_ISDIGIT
/Py_ISXDIGIT
macros were transformed to functions (see Convert Py_ISDIGIT/ISXDIGIT/etc to inline functions? #114047 and gh-108767: Convert Py_ISLOWER() macro to static inline function #108770)Notes
As now it's requires a PEP, here is a PEP draft.
Open issues:
float.fromhex()
?float.fromhex()
?Complementary PR: #113805 (formatted output)
📚 Documentation preview 📚: https://cpython-previews--114668.org.readthedocs.build/