Skip to content

Should floats allow omission of integer or fractional/exponent? #759

@marzer

Description

@marzer

I recently did a conformance pass over toml++ to ensure it passed any remaining test cases from both BurntSushi's and iarna's test suites, and was surprised to find that I wasn't passing some of the floating-point tests. Specifically, I was accepting some inputs that would be valid as literals in C and C++, but are apparently not in TOML. Excerpt from my test suite before I updated it:

// omitting leading integer part
parse_expected_value(FILE_LINE_ARGS, "     .1 "sv,       .1 );
parse_expected_value(FILE_LINE_ARGS, "    +.1 "sv,      +.1 );
parse_expected_value(FILE_LINE_ARGS, "    -.1 "sv,      -.1 );
parse_expected_value(FILE_LINE_ARGS, "   .1e1 "sv,     .1e1 );
parse_expected_value(FILE_LINE_ARGS, "  .1e+1 "sv,    .1e+1 );
parse_expected_value(FILE_LINE_ARGS, "  .1e-1 "sv,    .1e-1 );
parse_expected_value(FILE_LINE_ARGS, "  +.1e1 "sv,    +.1e1 );
parse_expected_value(FILE_LINE_ARGS, " +.1e+1 "sv,   +.1e+1 );
parse_expected_value(FILE_LINE_ARGS, " +.1e-1 "sv,   +.1e-1 );
parse_expected_value(FILE_LINE_ARGS, "  -.1e1 "sv,    -.1e1 );
parse_expected_value(FILE_LINE_ARGS, " -.1e+1 "sv,   -.1e+1 );
parse_expected_value(FILE_LINE_ARGS, " -.1e-1 "sv,   -.1e-1 );

// omitting trailing fractional part
parse_expected_value(FILE_LINE_ARGS, "     1. "sv,       1. );
parse_expected_value(FILE_LINE_ARGS, "    +1. "sv,      +1. );
parse_expected_value(FILE_LINE_ARGS, "    -1. "sv,      -1. );
parse_expected_value(FILE_LINE_ARGS, "   1.e1 "sv,     1.e1 );
parse_expected_value(FILE_LINE_ARGS, "  1.e+1 "sv,    1.e+1 );
parse_expected_value(FILE_LINE_ARGS, "  1.e-1 "sv,    1.e-1 );
parse_expected_value(FILE_LINE_ARGS, "  +1.e1 "sv,    +1.e1 );
parse_expected_value(FILE_LINE_ARGS, " +1.e+1 "sv,   +1.e+1 );
parse_expected_value(FILE_LINE_ARGS, " +1.e-1 "sv,   +1.e-1 );
parse_expected_value(FILE_LINE_ARGS, "  -1.e1 "sv,    -1.e1 );
parse_expected_value(FILE_LINE_ARGS, " -1.e+1 "sv,   -1.e+1 );
parse_expected_value(FILE_LINE_ARGS, " -1.e-1 "sv,   -1.e-1 );

All of those representations are valid in C and C++ (and I assume other languages, too), but not legal in TOML.

Upon re-reading the TOML spec I realized that it is fairly clear that the integer part is always required (it's never subject to an 'or'), so my knee-jerk assumption that followed the same rules as C and C++ wasn't particularly clever. Despite that, prefixing or suffixing an integer with a '.' character is a common short-hand for expressing that the value is in fact a float of some sort, and until encountering these test case violations, my assumption was that it was allowed in TOML seemed like a fairly natural one.

I guess the question is: should these representations, or some subset of them, be legal? If the answer is ultimately 'No', I think it would be wise to add a few invalid examples to the spec doc to explicitly call them out.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions