bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

hexadecimal input to strtod


From: Kenneth MacKenzie
Subject: hexadecimal input to strtod
Date: Mon, 28 May 2001 14:25:18 +0100

It appears that since ISO C99 the C library function strtod is
supposed to accept hexadecimal input:  see
http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_414.html

However,  the version which I'm using doesnt seem to handle this properly:

$ gcc --version
egcs-2.91.66

$ ls -l /lib/libc-*
-rwxr-xr-x    1 root     root      4107583 Feb 16 11:15 /lib/libc-2.1.3.so*

$ cat strtodbug.c
#include <stdio.h>
#include <stdlib.h>
main () {
  char *s;
  double x = strtod ("0x111", &s);
  printf ( "result = %g\n", x );
}

$ gcc -o strtodbug strtodbug.c
$ ./strtodbug
result = 1.06641
$ 

(This is happening under Red Hat Linux)

The result I would have expected to get is 273;  the result that
actually appears is 273/256.  It looks as if the problem is that the
exponent of the number isn't being adjusted properly after
conversion.  The result that strtod gives is actually 0x1.11.
Similarly,  0xfedbca is treated as 0xf.edcba.

I found out about this because a friend of mine who is learning perl
was playing about with programs to do simple arithmetic; he discovered
that if you type in hex numbers then the output is completely wrong.
This is because perl uses atof (& hence strtod) to convert strings to
numbers.


----
Kenneth MacKenzie
Department of Computer Science
University of Edinburgh



reply via email to

[Prev in Thread] Current Thread [Next in Thread]