Int is 4 bytes with a range of +- 2^31
Float is 4 bytes with a range of +- 1.2E(+- 38)
Float encompasses so many more points on the Real Line and yet is equal to the size of int. Is the Sign-Exponent-Fraction representation of float so awesome (or the 2’s complement of Int so pathetic) that this size disparity arises? Am I missing something?
I just find it highly surprising that something which represents (virtually) the entire real line is of the same size as that of which represents the Integers.
In C, an
intandfloateach take up 4 bytes, or 32 bits, in memory. The difference betweenintandfloatis not the number of bits they take up in memory, but rather in the way the ALU (Arithmetic Logic Unit) treats each number. Anintis treated as the integer represented by its bits using two’s complement notation. Afloat, on the other hand, is encoded (typically in IEEE 754 format) to represent a number in exponential form (e.g. 2.9979245×108 is a number in exponential form with base 2.9979245 and exponent 8). The number of significant digits in the decimal representation of a floating point number is always about the same: 6-9 digits for 32-bit float and 15-17 digits for a 64-bit float (ordouble). For example, the value 4.2949673×109 is what I get as the decimal representation of the closest 32-bit float to the number 232 , and it has 8 significant digits.It can be a common misconception that a floating point number somehow represents more information than a fixed-point number, which is not true. While a 32-bit
floatcan represent numbers of greater magnitude than a 32-bitint, it cannot represent all of them with as much precision. In total, this answer derives that a 32-bitfloatcan represent around 4,278,190,081 unique numbers. This other answer contains a nice plot showing how these 32-bitfloats are distributed on the real line.While an
intmay not be able to represent numbers with as large of a magnitude as afloat, it represents each number in its valid range (-232 to 232-1) with the same precision. The largest number it can represent is 2,147,483,647 and has 10 significant digits. And in total, it can represent 232=4,294,967,296 unique numbers, which is more than for the 32-bitfloat!