Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many distinct values can be stored in floating-point formats?

My assumptions for IEEE 754-2008:

binary16 - 2^16 distinct values, binary32 - 2^32 distinct values, ... binary128 - 2^128 distinct values.

Is this correct?

like image 414
user894319twitter Avatar asked Feb 22 '23 17:02

user894319twitter


1 Answers

This is a trick question.

The floating-point formats define some special values. Whether you count these as distinct depends on your point of view. The following is for double-precision (binary64):

  1. There are two representations of 0: with the sign bit 0 or 1 and both the exponent and mantissa all zero. The values are distinguishable by the fact that 1/+0 = infinity and 1/-0 = -infinity. But they compare equal.
  2. There are 2 infinities, where the first 12 bits are 0x7ff or 0xfff and the mantissa is all zero. These are not finite real numbers, but they are values.
  3. There is a whole range of Not-A-Number (NaN) values, having sign+exponent bits 0x7ff (signaling NaN) or 0xfff ("quiet" NaN) and a nonzero mantissa. Again, these are not real numbers but they are distinguishable values.

So, to summarize:

  1. The total number of distinguishable values (real numbers or otherwise) is 2^64.
  2. The number of distinct real numbers, excluding infinities and counting zero only once, is 2*(2^11-1)*2^52-1 = 18,437,736,874,454,810,623.

For binary16, the number of distinct real numbers is 2*(2^5-1)*2^10-1 = 63,487. For binary32, it's 2*(2^8-1)*2^23-1=4,278,190,079. For binary128, it's 2*(2^15-1)*2^112-1 or about 3.4*10^38.

like image 149
Jeffrey Sax Avatar answered Feb 25 '23 08:02

Jeffrey Sax