Re: Strange bit corruption in a double



Ben Bacarisse wrote:
"Digital Puer" <digital_puer@xxxxxxxxxxx> writes:

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??????

There are lots of numbers that are consistent with this data. Any
number too small to have a non-zero decimal digit in the default
precision used by %f format may still be very much != 0.0 and > 0.0.

I think your bit pattern represents a number in the order of
4.3e-305. The %g format will print it as will (on my gcc) %.310f!

No Ben. There is only one value consistent with the 'bits' data as presented. It is a 64-bit double on x86 architecture and is unique.

This particular value, expressed by 'printf(".16e", v)' is..

-1.7080703671901993e+39

...precisely.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.



Relevant Pages