Re: Convert float to double - weird failure



"tugboat90" <bmshipe@xxxxxxxxx> wrote in message
news:1193530070.911559.316470@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<snips>

Thank you for the responses, but I understand how the numbers are
represented inside of a computer. I think I phrased my question
wrong.

I don't believe that you do understand how numbers are represented inside a
computer. If you did, you would have understood the excellent answers that
have been given. You still seem to think the numbers are decimal but they
are binary and not an exact binary interpretation of the decimal number. For
example, your 1.2 is represented inside the computer you are probably
working on is given below and there are many systems on which this isn't
correct.

Float 1.2 is uninterpreted hex 0x3f99999a
float = 1.20000004768371582000000000000000
sign = 0x0
mantissa = 0x19999a
exponent = 0x7f
bias = 0x7f
radix = 0x2
value = (-1)^sign * (radix)^(exponent - bias) * (1).mantissa
where calculations are in binary (for radix 2)

When I converted from a float to a double, why did it add random junk
at the end of the number instead of making it zeros? If the random
junk is there, how do I get rid of it?

Thanks!

It's converting the float representation of 1.2 which is 0x3f99999a to a
double representation which is not even 0x3f99999a00000000.

Here's what a float looks like in Little Endian (ix86)
typedef struct {
unsigned int mantissa:23;
unsigned int exponent:8;
unsigned int negative:1;
} ieee_float;

Here's what a double looks like in Big and Little Endian (ix86)
#if BIGENDIAN
unsigned int s : 1;
unsigned int e : 11;
unsigned int m : 20;
unsigned int m2 : 32;
#else
unsigned int m2 : 32; // 2nd part of mantissa
unsigned int m : 20; // 1st mantissa
unsigned int e : 11; // exponent - which is why range of double is
larger
unsigned int s : 1; // sign
#endif


.