D
Digital Puer
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);
}
}
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);
}
}