469,089 Members | 1,213 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,089 developers. It's quick & easy.

atoi and INFINITY

I see that atof and strtod both recognize "INF", or "+INF", or "-INF"
as special case and return accordingly infinite value. But atoi and
strtol do not know about this special case - why? Is there a trick to
it?

example to demonstrate this feature:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
main(int argc, char *argv[])
{
long lx;
double dx;
char buf[20], *p = NULL;

strcpy(buf, argc == 2 ? argv[1] : "+INF");

lx = atol(buf); printf("lx=%li\n", lx);
dx = atof(buf); printf("dx=%f\n", dx);

lx = strtol(buf, &p, 10);
printf("lx=%li, p=%s, errno=%i\n", lx, p, errno);

dx = strtod(buf, &p);
printf("dx=%f, p=%s, errno=%i\n", dx, p, errno);
exit(0);
}

Please see results below

Compiled under SCO OSR5 (tried both native SCO compiler and gcc)

../c '+INF'
lx=0
dx=inf
lx=0, p=INF, errno=22
dx=inf, p=, errno=22

../c '-INF'
lx=0
dx=-inf
lx=0, p=-INF, errno=22
dx=-inf, p=, errno=22
Very similar behavoir under linux and gcc (errno works differently
somehow):
../c '+INF'
lx=0
dx=inf
lx=0, p=+INF, errno=0
dx=inf, p=, errno=0

../c '-INF'
lx=0
dx=-inf
lx=0, p=-INF, errno=0
dx=-inf, p=, errno=0

Any thoughts whether this is a problem with atoi/strtol? I expected
them behave similarly to atof/strtod, maybe my expectations are
unwarranted?
Jun 27 '08 #1
1 1795
On Apr 15, 5:00*pm, migurus <migu...@gmail.comwrote:
I see that atof and strtod both recognize "INF", or "+INF", or "-INF"
as special case and return accordingly infinite value. But atoi and
strtol do not know about this special case - why? Is there a trick to
it?

example to demonstrate this feature:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
main(int argc, char *argv[])
{
* long *lx;
* double dx;
* char *buf[20], *p = NULL;

* * * * strcpy(buf, argc == 2 ? argv[1] : "+INF");

* * * * lx = atol(buf); printf("lx=%li\n", lx);
* * * * dx = atof(buf); printf("dx=%f\n", dx);

* * * * lx = strtol(buf, &p, 10);
* * * * printf("lx=%li, p=%s, errno=%i\n", lx, p, errno);

* * * * dx = strtod(buf, &p);
* * * * printf("dx=%f, p=%s, errno=%i\n", dx, p, errno);
* * * * exit(0);

}

Please see results below

Compiled under SCO OSR5 (tried both native SCO compiler and gcc)

./c '+INF'
lx=0
dx=inf
lx=0, p=INF, errno=22
dx=inf, p=, errno=22

./c '-INF'
lx=0
dx=-inf
lx=0, p=-INF, errno=22
dx=-inf, p=, errno=22

Very similar behavoir under linux and gcc (errno works differently
somehow):
./c '+INF'
lx=0
dx=inf
lx=0, p=+INF, errno=0
dx=inf, p=, errno=0

./c '-INF'
lx=0
dx=-inf
lx=0, p=-INF, errno=0
dx=-inf, p=, errno=0

Any thoughts whether this is a problem with atoi/strtol? I expected
them behave similarly to atof/strtod, maybe my expectations are
unwarranted?
Even on systems where floating-point values have representable
infinities, integral types do not have infinities. There would be no
value to correctly represent the result of atol("INF").

-o
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Mike Moum | last post: by
6 posts views Thread by Steve Gallagher | last post: by
5 posts views Thread by Peter Hansen | last post: by
9 posts views Thread by Would | last post: by
14 posts views Thread by Jim Langston | last post: by
13 posts views Thread by kimiraikkonen | last post: by
50 posts views Thread by Bill Cunningham | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.