Re: Strange bit corruption in a double
- From: Joe Wright <joewwright@xxxxxxxxxxx>
- Date: Sun, 14 Jan 2007 11:09:50 -0500
Digital Puer wrote:
I'm getting a very weird bit corruption in a double. I am on an IntelYou've got something cocked up. I get..
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);
}
}
11001000 00010100 00010100 00001001 10001100 00000010 10111110 00000000
Exp = 1153 (131)
000 10000011
Man = .10100 00010100 00001001 10001100 00000010 10111110 00000000
-1.7080703671901993e+39
...from your 'bits' above.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.
- References:
- Strange bit corruption in a double
- From: Digital Puer
- Strange bit corruption in a double
- Prev by Date: Re: regarding free
- Next by Date: Re: Isn't it time there was a standard align statement?
- Previous by thread: Re: Strange bit corruption in a double
- Next by thread: Re: Strange bit corruption in a double
- Index(es):