On Apr 28, 12:37*pm, Fred <fred.l.kleinschm...@boeing.com> wrote:
>
> strtod only sets errno if a range error occurred, so you do not need
> that check to determine whether the field had bad characters in it.
Interestingly, it sets errno when the input is "[+-]inf" or "nan".
The conversion is correct, but it sets EDOM.
>
> You do not need the check *(*to != '\0') since if 'to' is not NULL, it
> points to the first unused character, which is guaranteed to be
> something other than the NUL character.
>
> That leaves you with:
> * *if ( to ) { ... }
No it doesn't (at least if (...) is supposed to be the error code):
dtmp = strtod("foo",&to);
has "to" as not NULL and it points to 'f', because nothing was
converted.
So if you want to be able to accept any valid number, including inf
and nan,
this seems to be the way to go:
dtmp=strtod(atoken,&to);
if(!to || *to){ fprintf(stderr,"PROBLEM\n"); }
Again, this is only if atoken is to hold exactly one valid double, and
nothing else.
Regards,
David Mathog
|