4

That is the maximal integer value could be assigned to Java double and still behave as an integer value? I mean, it sill must satisfy the usual conditions

a + 1 > a;
a - 1 < a;

With the values big enough, even a + 1000 may still be a due rounding errors.

I need to use double as a counter and want to know where is the upper limit of the reliable counting.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93

3 Answers3

4

I need to use double as a counter and want to know where is the upper limit of the reliable counting.

I can't imagine why you would use a double as a counter to store an integer, but the limit is in the range of eight million billion (2^53). If you use a long as a counter the limit is 9 billion billion.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Looks like the limit is exactly 2^53! How did you find that value? – wheels Nov 29 '13 at 17:01
  • 1
    @wheels - See [Wikipedia on "Machine epsilon"](http://en.wikipedia.org/wiki/Machine_epsilon) – Ted Hopp Nov 29 '13 at 17:03
  • @wheels 2^53-1 is the largest number where all the bits fit into the mantissa and 2^53 also work because it is a power of two and the mantissa is all zeros, one more and the mantissa can't store the value accurately. – Peter Lawrey Nov 29 '13 at 17:08
4

The number that you're looking for is 9,007,199,254,740,991 because 9,007,199,254,740,991 + 1 = 9,007,199,254,740,992 but 9,007,199,254,740,992 + 1 = 9,007,199,254,740,992.

I found this experimentally using the following snippet.

double a = 9.007199254E15;
while (a + 1 > a) {
    a += 1;
}
System.out.println(a);

Given the fact that you are using this value as a counter, and that the maximum value for longs is 2^63 - 1 = 9.22E18 (as Peter pointed out), there seems to be no reason not to use longs instead.

wheels
  • 198
  • 12
  • Might be clearer to write 9,007,199,254,740,992 (or 9 007 199 254 740 992 depending on your country). – Ted Hopp Nov 29 '13 at 17:02
  • the theory supports this too: a double has a precision of approximately 16 decimal digits (~15.955). int has a maximum of 10 decimal digits, therefore +1 will always change the value as long as you are within the 32bit integer range. for a 64bit integer that changes at the stated value (long has a maximum of 19 decimal digits) – x4rf41 Nov 29 '13 at 17:04
  • The mantissa in a double is 53 bits, which is why the maximum value is 2^53. – Mark Elliot Nov 29 '13 at 17:06
  • I have tried this code as well and confirm. 9.007199254740992E15. – Audrius Meškauskas Dec 02 '13 at 08:25
  • It can be true but you can't talk for sure based only on that test. You can't prove that a+1>a for every int smaller that that. To do so, you'd have to analyse the representation of double in JRE and prove it mathematically. – Danubian Sailor Dec 02 '13 at 08:55
1

If, by integer, you mean int, then all possible int values fit into a double since int is defined to be a 32-bit signed integer quantity, and a double has a mantissa size of 53 bits (into which an int value fits comfortably). If you mean long, on the other hand, then things are different, since longs are 64-bit signed integer quantities.

The maximum long value representable should be something like 253 - 1 or so (didn't try it, so may be wrong).

Dirk
  • 30,623
  • 8
  • 82
  • 102