Skip to content
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

Accept floating-point NaN and Inf as fnumber #538

Merged
merged 1 commit into from Feb 25, 2024

Conversation

rwpeterson
Copy link
Contributor

I was working on parsing numerical data and found that none of pyparsing's numerical expressions support the non-finite floating point values (NaN, +Inf, and -Inf).

Just based on the implementation, ppc.fnumber looks like a reasonable place to add support for this, since it's independent of the other numerical expressions and has the float conversion baked in. (Indeed, I initially thought it would accept these values due to the "float" part.) Essentially, this PR makes fnumber match anything that Python's float can itself parse. cPython's parsing is fully case-insensitive and accepts (optionally signed) "NaN", "Inf", and "Infinity".

One note of caution would be if users somehow rely on fnumber's failure to parse such constants as a way to deal with NaNs or infinities in their programs.

@ptmcg
Copy link
Member

ptmcg commented Feb 13, 2024

To avoid introducing a regression on fnumber, what do you think about adding separate expressions for nan's and inf's, so that apps that need them could use an expression like numeric = fnumber | nan | inf? Or create another version of fnumber that adds these - just need to come up with a decent name (one of the 2 hardest problems in CS).

@rwpeterson
Copy link
Contributor Author

I agree on keeping it separate from fnumber, and on the difficulty of a name :)

I factored it out into a separate ieee_float, since NaNs in particular are associated with the standard. Some other options for the name could be float_literal, ieee754, etc.

@ptmcg
Copy link
Member

ptmcg commented Feb 25, 2024

ieee_float looks good to me, and I admit, I never would have come up with that. Thanks for submitting!

@ptmcg ptmcg merged commit 640d75b into pyparsing:master Feb 25, 2024
9 checks passed
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.

None yet

2 participants