7

I am learning about floating point formats (IEEE). In the single precision floating point format ,it is mentioned that the mantissa has 24 bits and so it has 6 1/2 decimal digits of precision (as the per the book "understanding the machine") , and 7.22 decimal digits of precision.

I don't understand how the decimal digits of precision is calculated. Can somebody please enlighten me ?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Yogi
  • 105
  • 1
  • 3
  • 9
  • See [Is the most significant decimal digits precision that can be converted to binary and back to decimal without loss of significance 6 or 7.225?](http://stackoverflow.com/q/30688422/183120) for workouts and explanation on the answer. – legends2k Aug 25 '15 at 10:34

1 Answers1

10

With 24 bits, assuming one bit is reserved for the sign, then the largest decimal number you can represent is 2^23-1=8388607. That is, you can get 6 digits and sometimes a 7th. This is often expressed as "6 1/2 digits". If the 24 bits are representing an unsigned number, then the maximum value you can store is 2^24-1=16,777,215, or 7 and a fraction digits.

When someone quotes you a number with explicit decimal places like 7.22 decimal digits, what they're doing is taking the log (base 10) of the maximum value. So log(16777115)=7.22.

In general, the number of decimal digits you'll get from a given number of bits is:

d=log[base 10](2^b)

where b is the number of bits and d is the number of decimal digits. Then:

d=b * log(2)
d~=b * .3010

So 24 bits gives 24 * .3010 = 7.224

Jay
  • 26,876
  • 10
  • 61
  • 112
  • @jay, shoudnt this be log(16777215) instead of log(16777115) ? – Shmil The Cat May 08 '14 at 15:58
  • @ShmilTheCat Yes. Typo. – Jay May 08 '14 at 20:55
  • 2
    I just wanted to add that your point about the sign bit is invalid. The mantissa is 24 bits and the sign bit is **not** part of the mantissa. Actually, it is [sign_bit, exponent, mantissa] if you look at the bits. See the standard [here](http://en.wikipedia.org/wiki/Single-precision_floating-point_format). The sign bit is always present as part of the IEEE 754 standard, so the number is always _signed_. In conclusion, all 24 bits are used to represent the mantissa, so log[10](2^24)=7.22 as mentioned in the question. – Westy92 Feb 06 '15 at 06:03
  • @westy92 I was speaking of integers. Yes, with floats some number of bits are allocated to the exponent. How many affects the range of numbers that we can record, but not the number of places of precision. For those unaware, the bits assigned to hold actual digits, as opposed to an exponent or sign, are called the "mantissa", and that's what matters for our purposes here. – Jay Feb 06 '15 at 14:18
  • 1
    @westy92 Oh, looking back I see that the question specifically asked about IEEE floats. So yes, the sign bit is not included in the 24. – Jay Feb 06 '15 at 19:09
  • @Jay Although with 24 bits we'd get a precision of 7.22 decimal digits (24 * log₁₀ 2) the correct value of precision in decimal digits is only 6½. See the comment below the question for the dupe which has the right answer. – legends2k Aug 25 '15 at 05:01
  • @legends2k Hmm. The precision is 7.22 decimal digits, period. The other answer you refer to says that the number of decimal digits that you can convert from integer to float and back and not get rounding errors is 6 1/2. That's not the same thing, and I don't see anywhere that someone calls that the "precision". It's a different concept. It may be that in some context it's a more useful concept, but it isn't the question that was asked. – Jay Aug 25 '15 at 05:58
  • @Jay Agreed, I've removed the comment on the question. Thanks for the clarification without which I'd still be confused between round-tripping and precision! – legends2k Aug 26 '15 at 09:31
  • Precision is not really 7.22 decimal digits -- for floating-point (vs. integers), the picture is more complicated than that: http://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/ @legends2k – Rick Regan Jun 13 '16 at 00:42
  • @RickRegan A statement like "7.22 digits of precision" clearly is not true in a simple, literal sense. A number might be accurate to 7 digits or to 8, but it can't be accurate to a fraction of a digit. Perhaps a better answer would discuss what such a fractional precision means. The article you cite basically invents its own definition of "precision". Of course if you make up your own definition of a word, you'll get different answers to questions using that word. – Jay Jun 13 '16 at 13:19
  • Yes I agree an article on fractional precision would be interesting. But the point here is not that it is fractional, but where it comes from (log10(2^24)). That does not apply to floating-point numbers. Also, I would be interested to know what your definition of precision is, – Rick Regan Jun 13 '16 at 13:30