levels of representation of float and int

S

Shi Jin

Hi there,

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.

I am confused.
Please advice.
Thanks a lot
Shi
 
K

Keith Thompson

Shi Jin said:
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.
 
C

Charlie Gordon

Shi Jin said:
Hi there,

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.

In addition to Keith's detailed response, please note that floats can be
considerably slower than ints on many systems, that they are converted to
doubles when passed to unprototyped or varadic functions, that you cannot assume
that initializing them to all bits zero with calloc() or memset() yields a value
of 0.0.
Integral types are much preferable if you deal with fixed precision, fixed range
value sets.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top