1
$\begingroup$

How to calculate the number of Integers that can be represent in Double-Precision floating-point form?

$\endgroup$
1
$\begingroup$

Just count them.

You need to know the format of a double-precision floating point number. That's technically platform dependent, but practically all platforms these days use the same representation, IEEE-754 binary64. As you might expect from the format's name, that's a 64-bit encoding. It consists of three bit fields:

  • sign: 1 bit
  • exponent: 11 bits The actual exponent is the stored exponent - 1023 ("the exponent bias") and stored exponents 0 and 2047 (the largest value) are special. None of the values with stored exponent 2047 is an integer; these are either infinity (if the fraction is 0) or different "not a number" (NaN) values, if the fraction is not 0. If the stored exponent is 0, then the value is either 0 (if the fraction is 0) or a "subnormal" number; no subnormal number is an integer. Note that the result is that there are exactly two ways of representing 0, because the sign bit is independent. However, these are considered the same value.
  • fraction: 52 bits The fraction has an implicit binary point to its left; the actual significand is the fraction plus 1. (In other words, there is an implicit 1 to the left of the implicit binary point.)

Since the fraction has 52 bits, every valid number whose exponent is at least 52 is an integer. Those numbers have stored exponents between $1023+52$ and $2046$, inclusive, which is a total of $972$ possible exponent values. For each of these, there are $2^{52}$ possible positive numbers, all distinct.

In addition, there is a representation for every integer from $1$ to $2^{52}-1$. And for every positive integer, the corresponding negative integer can be formed by setting the sign bit to 1.

So, in all, we have $(972 * 2^{52}) + (2^{52}-1) = 973*2^{52}-1$ positive integers, the same number of negative integers, and $0$.

$\endgroup$
0
$\begingroup$

As rici answered, all large floating-point numbers are indeed integers, plus some small floating-point numbers, that's almost half the floating-point numbers.

But maybe you actually want to know: Which is the largest interval so that all integer values in that interval can be represented exactly as floating-point numbers?

Using the IEEE-754 format, 32 bit floating point numbers have a 23 bit mantissa plus one implicit leading mantissa bit, 64 bit floating point numbers have 52 + 1 bit mantissa, and 80 bit floating point numbers have a 64 bit mantissa.

As a result, all integers $-2^{24} ≤ i ≤ 2^{24}$, $-2^{53} ≤ i ≤ 2^{53}$ and $-2^{64} ≤ i ≤ 2^{64}$, depending on the format, can be represented exactly as floating point numbers; the next integer outside those intervals can't.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.