[prog] c types confusion
Daniel.
cristofd at hevanet.com
Wed Mar 24 10:46:40 EST 2004
>You have 8*sizeof(float) bits to work with
>(on my computer that would be 32)
(Maybe this was the missing piece of information that led dominik to
wonder why you would assume float would lose precision before 2^31;
sizeof(float) is 8 on some machines.)
>Those 32 bits now have to be split between the base (I
>don't think that's quite the right word) and the
>exponent (mantissa?).
That's correct.
>So if you have: 2.456e14 then you'd have x bits to
>store the 2456 and y bits to store the 14 and you are
>constrained by x + y = 32.
>
>Now, 2^32 = 4294967296 = 4.294967296e9, and its going
>to take all your 32 bits to store the: 4.294967296,
>leaving nothing to store the: 9, so won't it get
>approximated to: 4.294967e9, or something?
The base and exponent are stored in binary, so when you lose
precision, the base gets rounded to a round binary number, i.e. a
multiple of a power of 2. But you happen to be storing powers of two
in it anyway, which means it happens to come out exact. Try storing
2^32-1 in there; it won't work.
-Daniel.
--
() ASCII ribbon campaign () Hopeless ribbon campaign
/\ against HTML mail /\ against gratuitous bloodshed
More information about the Programming
mailing list