Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# double and long

James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Hi,
We know that type long has 64bits, so the the amount of the distinct numbers it can hold is 2 to the power of 64.
My puzzle is: since type double has the same number of bits, why it could hold the number greater than long both in precision and in magnitude? How many numbers could it distinguish?
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Originally posted by James Du:
Hi,
We know that type long has 64bits, so the the amount of the distinct numbers it can hold is 2 to the power of 64.
My puzzle is: since type double has the same number of bits, why it could hold the number greater than long both in precision and in magnitude? How many numbers could it distinguish?

Hi James,
You can find the answer in JLS or IEEE 754 standard. But here is a short answer:
The finite nonzero values of any floating-point value set can all be expressed in the form s * m * Math.pow(2,e-N-1) where
s is +1 or -1, m is a positive integer less than Math.pow(2,N), and
e, the exponential component, is an integer between
Emin = -(Math.pow(2,K-1) - 2) and
Emax = (Math.pow(2,K-1) - 1), inclusive.
For float N = 24 and K = 8. Emin = -126, Emax = 127
For double N = 53 and K = 11. Emin = -1022. Emax = 1023.
So as you can see, the exponential component for a double is quite large compared to a long.

- Lam -

[This message has been edited by Lam Thai (edited May 16, 2001).]
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
I think in a arithmetic sense, it's imposible for a type of 64bits to distinguish more than 2^64 distinct numbers, they have just 2^64 different combinations! how does it accomplish that?
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Originally posted by James Du:
I think in a arithmetic sense, it's imposible for a type of 64bits to distinguish more than 2^64 distinct numbers, they have just 2^64 different combinations! how does it accomplish that?

Hi James,
The position of the decimal point floats!
The answer lies in the formulea above where N is the precision that represents the mantisa and K represents the exponential component.
According to the IEEE standard, out of 64 bits, they use 11 bits to store the component, and the remanining 53 bits to store the mantisa. That is the reason why they give up the precision because with 53 bits, they can only have upto 17 digit precision (2^N, where N is 53,is equal 9007199254740992).
Now do you see why the same 64 bit can be used differently? In short, long has 64-bit precision but no exponent component. Double has only 53-bit precision, but it has 11-bit component! The exponential component is what floats the decimal point and hence helps extend the range of the double.
Now for the same 64 bits, you can even extend the range bigger by giving up a bit more precision and increasing the number of bit for the exponent component, let's say 49-bit precision and 15-bit exponent... And that how we got the extended-double number with the same 64 bit.
- Lam -

[This message has been edited by Lam Thai (edited May 17, 2001).]
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Thanks Lam,
It took me about 1 hour and 2 pieces of scratch paper to dissect the mantissa and exponent component part of a double type and to analyse their relationship. and it really cause me a headache!
Finally, I think i got it.
Within the scope of maximum mantissa, type double can hold every long number precisely. but if the long number is beyond the scope of the maximum mantissa of double(2^53-1), the double can't represent the number precisely, it just give a approximation. the reason is that the interval between 2 adjacent numbers of double that beyond the the scope of the maximum mantissa is larger than 1, while as for type long, the interval is a constant 1 throughout the whole range of long(i.e. [-2^63, 2^63-1]).
In other words, double still can't distinguish more than 2^64 different numbers.
In the following example, the 2 double variables hold 2 different numbers but they can't distinguish them while the 2 long variables can.

Thanks again, Lam, the question baffled me a long time!
James

I agree. Here's the link: http://aspose.com/file-tools

subject: double and long