Can someone explain how floating point numbers are stored in C, as for 32-bit float, 1 bit is parity, 23 bits for mantissa and 8 bits for exponent. So for numbers greater than 2^23(still in the float range), there is no space in the mantissa to store them.
1 Answer
The 23 bits in the mantissa determine the precision of the number. The 8 bits in the exponent determine the range of the number.
Consider the same problem in decimal terms. To keep it simple, let's say you're allowed a mantissa that's a positive number with 6 decimal digits of precision, and a number from -20 to 20 as the exponent. That means you would be able to store numbers in the range of
.000000000000000000001
to
99,999,900,000,000,000,000,000,000.
(give or take a few zeroes)
The mantissa varies from 1 to 999999. The exponent determines where the decimal point goes.
-
2I feel like this answer would be improved if you had a "therefore..." section. something along the lines of "therefore, a 32-bit float stores numbers greater than 2^23 with limited precision, meaning some integers above that range can't be represented exactly." Apr 12, 2019 at 4:38
-
A 32 bit float also stores numbers less than 2^23 with limited precision. Apr 12, 2019 at 6:40
-
@user1118321 the discussion of FP and float versus double combined with topics like "when is == not really ==" and "what the heck is this EPSILON thing" is a full page essay all on its own. Apr 12, 2019 at 18:10