473,566 Members | 3,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2657
"Shi Jin" <ji********@hot mail.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_Keit h) 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********@hot mail.com> wrote in message
news:cp******** **@news01.cit.c ornell.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
2113
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 C?
10
2837
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 also have a software programmer's calculator called BinCalc which shows the bits of whatever number. the bits that my code and the bits that the...
7
3636
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: 3.141593
10
4167
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 point number, either in ASCII format, or pure binary. In either case, since I'm using PyVisa, it converts the number to a single precision floating...
7
2960
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? int toIntByUnion (const float& a) { union {float f; int i;} u = {a}; return u.i;
13
1688
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 I could not verify that from time.h using sizeof(type) and numeric_limits<type>::max() tells me that int and long give the same output.
15
3731
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 bit-pattern. My question is if a program refers to that specific value which is used by the machine to refer to null pointer, how compiler treats that?.
7
8374
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 the code in a portable manner, not just for my box that uses 8-bit bytes and 4-byte floats. Comments and critiques are welcome. Thanks.
1
7960
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 understanding why and how a double can be represented in bits and bytes and which of the following can allow us to view the binary representation, unsigned...
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.