Size of int and float
Solution 1
They are totally different - typically int
is just a straightforward 2's complement signed integer, while float
is a single precision floating point representation with 23 bits of mantissa, 8 bits exponent and 1 bit sign (see http://en.wikipedia.org/wiki/IEEE_754-2008).
Solution 2
They have different ranges of values because their contents are interpreted differently; in other words, they have different representations.
Floats and doubles are typically represented as something like
+-+-------+------------------------+
| | | |
+-+-------+------------------------+
^ ^ ^
| | |
| | +--- significand
| +-- exponent
|
+---- sign bit
where you have 1 bit to represent the sign s (0 for positive, 1 for negative), some number of bits to represent an exponent e, and the remaining bits for a significand, or fraction f. The value is being represented is s * f * 2e.
The range of values that can be represented is determined by the number of bits in the exponent; the more bits in the exponent, the wider the range of possible values.
The precision (informally, the size of the gap between representable values) is determined by the number of bits in the significand. Not all floating-point values can be represented exactly in a given number of bits. The more bits you have in the significand, the smaller the gap between any two representable values.
Each bit in the significand represents 1/2n, where n is the bit number counting from the left:
110100...
^^ ^
|| |
|| +------ 1/2^4 = 0.0625
||
|+-------- 1/2^2 = 0.25
|
+--------- 1/2^1 = 0.5
------
0.8125
Here's a link everyone should have bookmarked: What Every Computer Scientist Should Know About Floating Point Arithmetic.
Solution 3
Two types with the same size in bytes can have different ranges for sure.
For example, signed int and unsigned int are both 4 bytes, but one has one of its 32 bits reserved for the sign, which lowers the maximum value by a factor of 2 by default. Also, the range is different because the one can be negative. Floats on the other hand lose value range in favor of using some bits for decimal range.
Solution 4
You are mixing the representation of a number, which is dependent upon some rules you (or somebody else) defines, and the way you use to keep the number in the computer (the bytes).
For example, you can use only one bit to keep a number, and decide that 0
represents -100, and 1
represents +100. Or that 0
represents .5 and 1
represents 1.0. The 2 things, the data and the meaning of the data, are independent.
Solution 5
An integer is just a number... It's range depends on the number of bits (different for a signed or unsigned integer).
A floating point number is a whole different thing. It's just a convention about representing a floating point number in binary...
It's coded with a sign bit, an exponent field, and a mantissa.
Read the following article:
http://www.eosgarden.com/en/articles/float/
It will make you understand what are floating point values, from a binary perspective. The you'll understand the range thing...
atul
Updated on September 23, 2020Comments
-
atul over 3 years
I have a question about the ranges of ints and floats:
If they both have the same size of 4 bytes, why do they have different ranges?
-
cnicutar over 12 years+1 Of course, you know better than me those are just the usual representations.
-
Paul R over 12 yearsIndeed - since it was evidently a noob question I thought it best to keep things simple.
-
Andreas Brinck over 12 years@cnicutar Are you implying that a
float
can be represented arbitrarily? -
cnicutar over 12 years@Andreas Brinck I am implying that the standard doesn't say those have to be the representations.
-
Oliver Charlesworth over 12 years@Andreas, cnicutar: To clarify, the C standard doesn't require the implementation to use IEEE-754.
-
Oliver Charlesworth over 12 yearsTo be pedantic, C integral types are also, like floating-point types, just "a convention about representation". Also, you have a circular definition in your second paragraph!
-
sepp2k over 12 years"Floats [...] lose value range" makes it sound as if the largest number representable by a floating point number was smaller than the largest number representable by an integer number of the same size, which is definitely not true.
-
John Bode over 12 years@Andreas Brinck - there are plenty of floating point formats apart from IEEE 754; back in the late Cretaceous I worked on a VAX, which used the VAX F format. The C language standard doesn't care about the specific float format used by an implementation, it just mandates the minimum range and precision.