Accept floating-point NaN and Inf as fnumber #538
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 makesfnumber
match anything that Python'sfloat
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.