Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strtod usage for "just the number"?

Thread Tools

strtod usage for "just the number"?

David Mathog
Posts: n/a
On Apr 28, 12:37*pm, Fred <(E-Mail Removed)> 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

So if you want to be able to accept any valid number, including inf
and nan,
this seems to be the way to go:

if(!to || *to){ fprintf(stderr,"PROBLEM\n"); }

Again, this is only if atoken is to hold exactly one valid double, and
nothing else.


David Mathog

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Aliasing/Torek's strtod() experience Adam Warner C Programming 9 07-13-2005 10:48 PM
Aliasing/Torek's strtod() experience Adam Warner C Programming 0 06-29-2005 01:23 AM
strtod / setlocale Mathieu Malaterre C++ 1 12-13-2004 06:24 PM
std::string and strtod() Leslaw Bieniasz C++ 3 09-21-2004 07:22 AM
strtod - Dynamic Memory? Marky C C Programming 20 04-04-2004 12:21 AM