The wikipedia page states that
an IEEE 754 32-bit base-2 floating-point variable has a maximum value of (2 − 2−23) × 2127 ≈ 3.4028235 × 1038
- In that number, are +∞, −∞ and NaN included?
- What is that 2 in "(2 − 2−23)"?
- Why 127 in 2127?
The wikipedia page states that
an IEEE 754 32-bit base-2 floating-point variable has a maximum value of (2 − 2−23) × 2127 ≈ 3.4028235 × 1038
In that number, are +∞, −∞ and NaN included?
No, they are special "numbers"
What is that 2 in "(2 − 2^−23)"?
The largest mantissa is 1.11111....111 and this value is equal to 2 − 2^−23. If you add it the ULP (2^-23), you obtain 2.0
Why 127 in 2^127?
This is based on the way single precision floats are coded. There are 8 bits to code the exponent, and the actual exponent code is obtained by adding 127 to the exponent of the number. The largest exponent code is 254 (as 255 is used for NaNs) and the largest exponent for a number is 254-127=127.
In all IEEE 754 codes, if the exponent is coded on k bits, the largest exponent is 2^(2^(k-1)-1)
When you look at the bit-pattern of IEEE-754 binary32
Then you see that there are 2 32 possible bit combinations. All of these potentially represent a floating-point number. However, from those combinations, a few have a special meaning. Those are the combination where the exponent is given by 11111111
. Any of these combinations represent NaN
or Inf
. In total there are 2 32 − 8 = 2 24 such combinations. Also, due to the sign-bit, the number zero is represented as -0
and +0
. So in short, the total amount of binary32 floating-point numbers is given by:
232 − 224 − 1 = 4 278 190 079
Image is taken from Wikipedia
You have kind of asked two questions here. Note that there is a difference between "how many different values can be encoded" and the minimum and maximum values.
The number of values is very easy to calculate. In 32 bits, you can have 232 or 4,294,967,296 values.
"But", I hear you say, "1038 is larger than 232!". Yes, it is, and this is because there comes a point where the float encoding is not actually able to encode all of the integers within its range.