Re: Convert float to double - weird failure



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

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?

It didn't.

Let's do this in base 10 to make it easier to relate to.

Imagine I invent a datatype called the "float10". It can
represent 5 decimal digits and a one-digit exponent. I also
have a "double10" which can represent 10 decimal digits and
a two-digit exponent.

So, I assign 1/3 to a float10. It comes out as 3.3333 * 10^-1,
or (dispensing with the scientific notation) 0.33333. You can
see already that that's not equivalent to 1/3. In fact, 0.33333
differs from 1/3 by 0.0000033333... (repeating 3 at the end).

Now I assign that to a double10. I get 0.3333300000. This is way
off from 1/3. In fact, half the digits are wrong. Even the closest
value to 1/3 that a double10 can represent, 0.3333333333, is not exact,
but this value is much worse than that.

If I then pass this 0.3333300000 to a routine that takes a double10
and converts it to a fraction, do you think it's going to come up
with anything close to 1/3? The routine already knows that it's
dealing with a double, so it's going to think that those last 5
digits were mean to be zeros.

It's exactly the same scenario with the binary floating point math.
It's just that different sets of values be represented exactly in
each.

If the random
junk is there, how do I get rid of it?

Set the bits to whatever value you want them to be. But don't
rely on the float to be able to store those bits, because it's
not that precise.

- Logan
.



Relevant Pages

  • Re: float? double?
    ... Joe Wright wrote: ... The 53-bit double mantissa demands 16 decimal digits. ... My point is that a given float printed with sprintfwill produce a string that when presented to atofor strtodwill produce the original float value exatcly. ... According to the IEEE 754 standards, %.9e format for float data type, and %.17e for double, are required to avoid losing accuracy, and this can be supported only within well defined ranges. ...
    (comp.lang.c)
  • Re: saving arrays of floats to a file
    ... full range and precision of the float. ... of type float? ... (It probably does on most platforms.) ... with q decimal digits can be rounded into a floating-point number ...
    (comp.lang.c)
  • Re: Packed decimal arithmetic in C
    ... > integer-valued floating point? ... If you are referring to a float in the sense ... using 64-bit integers which only has around 20 decimal digits of precision. ...
    (comp.os.vms)
  • Re: float? double?
    ... Walter Roberson wrote: ... Joe Wright wrote: ... The 53-bit double mantissa demands 16 decimal digits. ... My point is that a given float printed with sprintfwill produce a string that when presented to atofor strtodwill produce the original float value exatcly. ...
    (comp.lang.c)