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

StringToDoubleConverter: how to detect error AND use nan/inf detection? #53

Open
lsemprini opened this issue Sep 21, 2017 · 2 comments
Open

Comments

@lsemprini
Copy link

Thank you for making double-conversion!

Though it has a very unintuitive interface, the "standard" strtod/strtof can detect the following errors:

fprintf(stderr, "%s: not a decimal number\n", buff);
fprintf(stderr, "%s: extra characters at end of input: %s\n", buff, end);
fprintf(stderr, "%s out of range of type double/float\n", buff);

How can I accomplish the same thing with StringToDoubleConverter ?

How can I detect all of those error cases (ideally separately, but how can I even detect that one of them has happened regardless of which one) while also using the "nan"/"inf" parsing feature?

It seems I can detect "extra characters" with

processed_characters_count < length

But what about overflow and other malformed string errors?

If I set junk_string_value/empty_string_value to NaN then I lose the useful feature of the routine by which it detects the "nan" string (I cannot tell if the string is a well-formed "nan" or whether the string contains malformed content that got mapped to junk_string_value/empty_string_value==NaN).

Is there a way to do this currently? If so, please add it to the dox in double-conversion.h. If not, can we add it?

Thanks

@lsemprini
Copy link
Author

A bit more info: I fed in a string with 2048 '9's (which should overflow doubles with 11 bits of mantissa) and after return from StringToDouble() I got length==processed_characters_count and a double of +Infinity.

So two things:

  1. If this is the intended behavior for reporting overflow, it would be nice to document this in double-conversion.h

  2. This still leads to the same dilemma as in the OP: how to detect overflow as opposed to a well-formed "Inf" in the string?

Currently it looks like I have to parse Inf and Nan outside of StringToDoubleConverter and then pass NULL for infinity_symbol and nan_symbol in order to get useful error checking. It sure would be nice not to have to do this.

Seems like the API needs to separate these two concepts rather than using the same channel to report both a valid parsing result and a parse error.

@floitsch
Copy link
Collaborator

floitsch commented Oct 1, 2017

I'm currently very busy and don't think I will find the time to do anything about this bug.
Consider pinging this bug from time to time.

Another way to detect could be to detect that the processed_characters_count must be pretty high for an overflowing infinity.

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

No branches or pull requests

2 participants