-
September 22nd, 2010, 07:38 AM
#1
Float and Int32
Why do I read when I google for the max value of float that the max value of a float is way higher than an unsigned integer 32????
float = 4 bytes!
int32 = 4 bytes!
Please explain
(I find on google that max value is 3.40282e+038 for a float... lol)
(Max value unsigned int32 = 4.2e+09)
Last edited by ProgrammerC++; September 22nd, 2010 at 07:41 AM.
-
September 22nd, 2010, 08:11 AM
#2
Re: Float and Int32
See http://en.wikipedia.org/wiki/IEEE_754-1985
A float has three parts: a sign, an exponent, and a fraction.
An integer has only one part which is like the fration.
BTW, double is much more frequently used than float in C++/C.
-
September 22nd, 2010, 08:34 AM
#3
Re: Float and Int32
Put simply: A float can represent a very large number, but not accurately. While an int is constrained by magnitude, a float or double is only constrained by number of significant digits.
So if you assigned a float the value 1 trillion, that variable absolutely could not store that value all the way down to the 1s place, but it would still be approximately equal to 1 trillion. In fact, doing var += 1.0f on it might not affect its value at all.
One of the pitfalls of this fact is the following:
Code:
int i = 0;
float f1 = 1000.0, f2 = 0.0001;
while (i < 1000000000)
f1 += f2;
cout << f1;
This will probably not do what you think it should do. Because the magnitudes of the two floats are so different, they don't add properly.
Last edited by Lindley; September 22nd, 2010 at 08:36 AM.
-
September 22nd, 2010, 05:49 PM
#4
Re: Float and Int32
Originally Posted by Lindley
This will probably not do what you think it should do.
Well, that would certainly depend on what I (assuming I were the programmer) would actually think it did. As nothing in the loop has any effect on the termination condition, it would loop endlessly, regardless of the (in)accuracy of the foat type. Might it be missing a ++i somewhere?
Sorry, I don't seriously want to tease you. I myself tend to occasionally make simple mistakes like that, caused by negligence, in simple sample code like that.
-
September 24th, 2010, 09:53 AM
#5
Re: Float and Int32
to explain it simply...
An int is an accurate whole value between a minimum and a maximum.
whereas a float is made of 2 parts. To explain this how it would work in decimal. you have an integer part and a 'to the power of 10' factor. Lets say we take 2 16bit shorts. which is the same size as a single 32 int.
the first short has the integer portion, the next short has the power of 10 factor.
1234 would be represented as (1234 * 10^0)
intpart = 1234
powerof10 = 0
1234000000000000 would be represented as (1234 * 10^12)
intpart = 1234
powerof10 = 12
0.0001234 would be (1234 * 10^-7)
intpart = 1234
powerof10 = -7
Now, This clearly shows that the 'float' solution allows for both very high values, as well as numbers that have many decimals. It can even accurarely store the 2nd example that wouldn't fit into a 32bit int.
The drawback is that it can't accurately store something like
1234567 because that would overflow the intpart.
the closest you can get would be
intpart= 12346
powerof10 = 2
So while a float has a much wider range of values it can represent, it can't represent all of those values accurately. It can store some values accurately, but other values are only approximated.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|