473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

levels of representation of float and int

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
Nov 14 '05 #1
2 2648
"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.
Nov 14 '05 #2

"Shi Jin" <ji********@hotmail.com> wrote in message
news:cp**********@news01.cit.cornell.edu...
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.

--
Chqrlie.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mantorok Redgormor | last post by:
Is it possible to display the underlying representation of integers? Not just integers but also float/double and is it also possible to display the padding bits of signed integer types in standard...
10
by: ben | last post by:
i'm learning about the floating point format that's used on my computer (apple mac so powerpc) i've written a function to print out the bits in a float to see how floats are represented and i...
7
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
10
by: 63q2o4i02 | last post by:
Hi, I'm using python to run some lab equipment using PyVisa. When I read a list of values from the equipment, one of the fields is 32 bits of flags, but the value is returned as a floating...
7
by: Stein Gulbrandsen | last post by:
What is the best way to get to the integer representation of a float? I would like to do int& toIntByCast (float& a) {return *reinterpret_cast<int*(&a);} but is this legal? Is this safer?...
13
by: Gary Wessle | last post by:
Hi there I have a method which returns time_t and another two methods return double data types and I cann't change that since the library is provided by Big Bucks Inc. I think time_t is long but...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
7
by: Gary Baydo | last post by:
In an effort to write a simple rounding function, I wrote the following code out of curiosity. My question is, can I rely on the output to 'make sense'? As an added 'exercise' I tried to write...
1
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.