-1

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

  1. In that number, are +∞, −∞ and NaN included?
  2. What is that 2 in "(2 − 2−23)"?
  3. Why 127 in 2127?
phuclv
  • 37,963
  • 15
  • 156
  • 475
Joshua Leung
  • 2,219
  • 7
  • 29
  • 52
  • 1
    https://stackoverflow.com/a/12560672/270986 may be useful? But it sounds as though you're conflating two different things: (1) the number of distinct representable values (of particular types), which is what your title refers to, and (2) the maximum representable finite value, which is what your question body refers to. Which of those are you asking about? – Mark Dickinson Oct 18 '19 at 10:13
  • Possible duplicate: https://stackoverflow.com/q/24369558/270986 – Mark Dickinson Oct 18 '19 at 10:24

3 Answers3

2

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)

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31
2

When you look at the bit-pattern of IEEE-754 binary32

enter image description here

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

kvantour
  • 25,269
  • 4
  • 47
  • 72
  • 1
    I don't understand the "2^(32-24) = 2^26" bit of this answer: 2^(32-24) is not equal to 2^26. There are 2^24 bit patterns that represent infinities or NaNs (or broken down further, there are 2 infinities, 2^23 - 2 signalling NaN bit patterns, and 2^23 quiet NaN bit patterns). – Mark Dickinson Oct 18 '19 at 10:22
  • 1
    @MarkDickinson Yep ... that was a moment of brain-fatigue ;-). I've updated the answer now. – kvantour Oct 18 '19 at 10:30
-1

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Turksarama
  • 1,136
  • 6
  • 13