strtod oddities?

Hey all

I’m having some problems with strtod, and stuff containing the string
‘infinit’

See example:

int main()
{
char *p;

int err=strtod(“infinit”,&p);

fprintf(stderr,"%s,err=%d,errno=%s\n",p,err,strerror(errno));

return 0;
}
This gives : ,err=0,errno=No error.

If it really is infinit, and strtod is suppose to handle it, shouldn’t
errno = ERANGE ?


Then changing the “infinit” string to

“inch” ,
ch,0,errno=No Error.

I’m not sure what the standard say, but system like Linux doesn’t seem
to care about ‘infinit’, and returns “inch” when the input string is “inch”.


“5inch”
inch,err=5,errno=No error


/Johan

ed1k wrote:

phearbear <> phearbear@home.se> > wrote in article <alj23f$ni$> 1@inn.qnx.com> >…

Hey all

I’m having some problems with strtod, and stuff containing the string
‘infinit’

See example:

int main()
{
char *p;

int err=strtod(“infinit”,&p);


string to double (strtod) returns double, not int.


fprintf(stderr,"%s,err=%d,errno=%s\n",p,err,strerror(errno));

return 0;
}
This gives : ,err=0,errno=No error.

If it really is infinit, and strtod is suppose to handle it, shouldn’t
errno = ERANGE ?


Why strtod is suppose to handle string “infinit”?
Here is what doc says
http://www.qnx.com/developer/docs/momentics_nc_docs/neutrino/lib_ref/s/strtod.html

The function recognizes a string containing the following:
optional white space
an optional plus or minus sign
a sequence of digits containing an optional decimal point
an optional e or E, followed by an optionally signed sequence of digits.

If the correct value would cause overflow, plus or minus HUGE_VAL is returned according to the
sign, and errno is set to ERANGE. If the correct value would cause underflow, then zero is
returned, and errno is set to ERANGE.
This function returns zero when the input string can’t be converted

So, if you want ERANGE you have to pass string like “9.999e9999” or something big enough for duble.

But I think it should gives
infinit,err=0,errno=No error.

Try
double err;
Sorry, the double part I was just using in my short example, sorry bout

that :stuck_out_tongue:

The issue is that i’m using it to strip of numbers before some notion.
ie ‘inch’ but since something is phucked, and QNX strtod seems to think
‘infinit’ is a number, it gives p = “ch”
Which, won’t match a strcmp(“inch”…) :wink:
And since it do think infinit is a number, then it really should set
ERANGE, since well, infinit is kind of… out of range eh? :stuck_out_tongue:
Or , as i think it’s supposed to, simply ignore infinit and give me my
inch back :}


/Johan

Wojtek Lerch wrote:

phearbear <> phearbear@home.se> > wrote:

I’m having some problems with strtod, and stuff containing the string
‘infinit’


According to the latest C standard, strtod() is supposed to recognize
“inf” and “infinity”. strtod(“infinit”,&p) should return infinity and
leave p pointing at “init”.

There was a thread in comp.os.c in March discussing this – a Google
search for “strtod INFINIT” finds it. (In particular, take a look at
P.J.Plaguer’s responses: QNX uses his C library.)

Thanks. this helps alot :slight_smile:

phearbear <phearbear@home.se> wrote in article <alj23f$ni$1@inn.qnx.com>…

Hey all

I’m having some problems with strtod, and stuff containing the string
‘infinit’

See example:

int main()
{
char *p;

int err=strtod(“infinit”,&p);

string to double (strtod) returns double, not int.

fprintf(stderr,"%s,err=%d,errno=%s\n",p,err,strerror(errno));

return 0;
}
This gives : ,err=0,errno=No error.

If it really is infinit, and strtod is suppose to handle it, shouldn’t
errno = ERANGE ?

Why strtod is suppose to handle string “infinit”?
Here is what doc says
http://www.qnx.com/developer/docs/momentics_nc_docs/neutrino/lib_ref/s/strtod.html

The function recognizes a string containing the following:
optional white space
an optional plus or minus sign
a sequence of digits containing an optional decimal point
an optional e or E, followed by an optionally signed sequence of digits.

If the correct value would cause overflow, plus or minus HUGE_VAL is returned according to the
sign, and errno is set to ERANGE. If the correct value would cause underflow, then zero is
returned, and errno is set to ERANGE.
This function returns zero when the input string can’t be converted

So, if you want ERANGE you have to pass string like “9.999e9999” or something big enough for duble.

But I think it should gives
infinit,err=0,errno=No error.

Try
double err;

Eduard.
ed1k at ukr dot net


Then changing the “infinit” string to

“inch” ,
ch,0,errno=No Error.

I’m not sure what the standard say, but system like Linux doesn’t seem
to care about ‘infinit’, and returns “inch” when the input string is “inch”.


“5inch”
inch,err=5,errno=No error


/Johan

phearbear <phearbear@home.se> wrote:

I’m having some problems with strtod, and stuff containing the string
‘infinit’

According to the latest C standard, strtod() is supposed to recognize
“inf” and “infinity”. strtod(“infinit”,&p) should return infinity and
leave p pointing at “init”.

There was a thread in comp.os.c in March discussing this – a Google
search for “strtod INFINIT” finds it. (In particular, take a look at
P.J.Plaguer’s responses: QNX uses his C library.)


Wojtek Lerch QNX Software Systems Ltd.

The issue is that i’m using it to strip of numbers before some notion.
ie ‘inch’ but since something is phucked, and QNX strtod seems to think
‘infinit’ is a number, it gives p = “ch”

The standard actually says that the string can contain (case instensive)
INF, INFINITY, NAN or NAN(n-char-sequence).

And since it do think infinit is a number, then it really should set
ERANGE, since well, infinit is kind of… out of range eh? > :stuck_out_tongue:

ERANGE is used if the result isn’t representable from the ranges HUGE_VAL
and HUGE_VAL[FL].

Or , as i think it’s supposed to, simply ignore infinit and give me my
inch back :}

Why not use strtok() to search for your notation, or use sscanf() to do the
conversion if you can be sure of the format of the string.

\

Cheers,
Adam

QNX Software Systems Ltd.
[ amallory@qnx.com ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <pschon@baste.magibox.net>

Wojtek Lerch <wojtek_l@yahoo.ca> wrote:
: According to the latest C standard, strtod() is supposed to recognize
: “inf” and “infinity”. strtod(“infinit”,&p) should return infinity and
: leave p pointing at “init”.

: There was a thread in comp.os.c in March discussing this – a Google
: search for “strtod INFINIT” finds it. (In particular, take a look at
: P.J.Plaguer’s responses: QNX uses his C library.)

Before anyone else can say it, no, it isn’t documented. I’ll add it to the
queue.


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems