Skip to content

Parsing of floats should be more permissive with regards to NaN and +/- inf syntax #21652

@dhardy

Description

@dhardy

When parsing an f32, f64, etc., more special syntaxes for infinity and not-a-number should be expected:

fn f64_parse(text: &str){
    match text.parse::<f64>(){
        Some(v) => println!("Parsing {} yields {}", text, v),
        None => println!("Parsing {} fails", text)
    };
}
fn main(){
    f64_parse("NaN");
    f64_parse("nan");
    f64_parse("NAN");
    f64_parse("-nan");
    f64_parse("inf");
    f64_parse("INF");
    f64_parse("infinity");
}

Result:

Parsing NaN yields NaN
Parsing nan fails
Parsing NAN fails
Parsing -nan fails
Parsing inf yields inf
Parsing INF fails
Parsing infinity fails

It is debatable whether the default parser should support all these syntaxes, but it would be very useful to have this functionality somewhere. Links:

The floating point conversion functions convert infinity to inf or
infinity. Which one is used is implementation defined.
Not-a-number is converted to nan or nan(char_sequence). Which one is
used is implementation defined.
The conversions F, E, G, A output INF, INFINITY, NAN instead.

I'm sure I've come across -nan (possibly different case) as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions