"Shi Jin" <ji********@hotmail.com> writes:
I have been thinking this for quite a while: if we are considering the
number of different representations of one type,say int and float,
is there any difference as long as they are the same bit long?
I am thinking this as an example for representing different
colors. Is the number of differnt colors a 32-bit integer can
represent any differnt from using a 32-bit floating point? It seems
to me float has much more choices than int. But just thinking of the
binary possibility, they are both 2^32 kinds of differnt combination
of 1s and 0s. So they are the same in this sense.
Assume, for the sake of concreteness, that int is a 32-bit signed
2's-complement type, and that float is a 32-bit IEEE single-precision
floating-point type. (There are other possibilities, but these are
very common.)
Type float can represent a much wider *range* of values than type int.
The maximum representable value of type float is somewhere around
3.4e38, while INT_MAX is only about 2.1e9. And float can represent a
lot of values that int can't, such as 0.5. But the total count of
representable float values is no more than the total count of
representable int values. (In fact it's less, because of things like
signed zero, NaNs, and Infinities.) The float representation
sacrifices precision for range; 8 bits are devoted to representing the
exponent value.
But type float can represent all int value in the range -16777216
... +16777216, which is likely to cover most of the values you're
actually going to use, even though the vast majority of int values are
outside that range. That's probably why it *seems* like float can
represent more values than int: float values are much more densely
distributed in the range that you're likely to be dealing with.
These numbers are going to be different for different integer and
floating-point representations, but the general idea should be the
same.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.