PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > atoi and INFINITY
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
atoi and INFINITY

Réponse
 
LinkBack Outils de la discussion
Vieux 15/04/2008, 22h00   #1
migurus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?
  Réponse avec citation
Vieux 15/04/2008, 22h09   #2
Owen Jacobson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: atoi and INFINITY

On Apr 15, 5:00pm, migurus <migu...@gmail.com> wrote:
> 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09362 seconds with 10 queries