Re: Convert float to double - weird failure
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Sat, 27 Oct 2007 22:18:36 -0500
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
.
- References:
- Convert float to double - weird failure
- From: tugboat90
- Re: Convert float to double - weird failure
- From: mike3
- Re: Convert float to double - weird failure
- From: tugboat90
- Convert float to double - weird failure
- Prev by Date: Re: Convert float to double - weird failure
- Next by Date: Re: Please help!
- Previous by thread: Re: Convert float to double - weird failure
- Next by thread: C-derived OO Prog Language, Cross Platform GUI Toolkit, 2D/3D Graphics Engine Project
- Index(es):
Relevant Pages
|