Strange bit corruption in a double
- From: "Digital Puer" <digital_puer@xxxxxxxxxxx>
- Date: 13 Jan 2007 22:00:05 -0800
I'm getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux
I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.
However, when I compare
if (variable == 0.0), I get false.
and if (variable > 0.0), I get true.
I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:
printf = 0.000000000000000
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000
Any ideas??????
FWIW, I know the function to print the bit pattern of the double
is correct:
void print_binary_double(double value)
{
unsigned char *a;
a = (unsigned char *)&value;
int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc(*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc(unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}
.
- Follow-Ups:
- Re: Strange bit corruption in a double
- From: Barry Schwarz
- Re: Strange bit corruption in a double
- From: Joe Wright
- Re: Strange bit corruption in a double
- From: Ben Bacarisse
- Re: Strange bit corruption in a double
- From: Lane Straatman
- Re: Strange bit corruption in a double
- From: David T. Ashley
- Re: Strange bit corruption in a double
- Prev by Date: Re: Best Place to Define TRUE/FALSE
- Next by Date: Re: Best Place to Define TRUE/FALSE
- Previous by thread: Best Place to Define TRUE/FALSE
- Next by thread: Re: Strange bit corruption in a double
- Index(es):
Relevant Pages
|