12 Replies Latest reply: Dec 22, 2009 8:59 AM by dcminter

# Need help with calculating floating point numbers in JAVA

Hi,

I am using JDK1.5.0_07. I performed a particular calculation in JAVA - 4671.11 * 100
The value returned is 467110.99999999994 instead of 467111.

The reason being 4671.11 cannot be represented exactly in floating point.

Can anyone please tell me which are the other numbers in JAVA which cannot be represented as floating point numbers.

Thanks,
• ###### 1. Re: Need help with calculating floating point numbers in JAVA
Can anyone please tell me which are the other numbers in JAVA which cannot be represented as floating point numbers.
That would be all the numbers that aren't expressible in binary floating point. You really want a list? It's pretty long!
• ###### 2. Re: Need help with calculating floating point numbers in JAVA
SASPROGR wrote:
The reason being 4671.11 cannot be represented exactly in floating point.

Can anyone please tell me which are the other numbers in Java which cannot be represented as floating point numbers.
We can tell you which numbers CAN be represented in 64-bit IEEE 754 floating point (Java double);
scalars up to 2 ^53^ and
fractions with a numerator up to 2 ^53^ and a denominator that is a power of 2.
• ###### 3. Re: Need help with calculating floating point numbers in JAVA
ejp wrote:
It's pretty long!
&#8734;

:o)
• ###### 4. Re: Need help with calculating floating point numbers in JAVA
yawmark wrote:
ejp wrote:
It's pretty long!
&#8734;
Let J denote the set of numbers that CAN be represented in 64-bit IEEE 754 floating point (Java double),
then the answer to the original question is a simple complement; R [\|http://en.wikipedia.org/wiki/Set_%28mathematics%29#Complements] J
• ###### 5. Re: Need help with calculating floating point numbers in JAVA
[What Every Computer Scientist Should Know About Floating-Point Arithmetic|http://docs.sun.com/source/806-3568/ncg_goldberg.html]
• ###### 6. Re: Need help with calculating floating point numbers in JAVA
SASPROGR wrote:
Can anyone please tell me which are the other numbers in JAVA which cannot be represented as floating point numbers.
Why do people insist on writing Java in all-caps? Do they think it's an abbreviation? I don't know why, but this always grinds my gears...
• ###### 7. Re: Need help with calculating floating point numbers in JAVA
Do they think it's an abbreviation?
Yep. Someone took a dozen posts of near incomprehensible broken English to ask what it stood for the other day. Probably not helped by the fact that Sun's stock ticker is JAVA all caps (I presume that'll go when Snoracle goes live).

Edited by: dcminter on 22-Dec-2009 14:33
• ###### 8. Re: Need help with calculating floating point numbers in JAVA
dcminter wrote:
Do they think it's an abbreviation?
Sun's stock ticker is JAVA all caps (I presume that'll go when Snoracle goes live).
Never figured that one out. Was Sun honestly not publicly traded before Java was released? Or did they actually change it from something else in a PR move?
• ###### 9. Re: Need help with calculating floating point numbers in JAVA
Or did they actually change it from something else in a PR move?
They were SUNW before and changed it. One of two utterly fatuous things Sun did before the Oracle offer - the other being the purchase of MySQL for (pinkie to corner of mouth) one beellion dollars.
• ###### 10. Re: Need help with calculating floating point numbers in JAVA
Can you please provide info over the list. Also, how you got to know these numbers .... if by computation or is it published.

Thanks.
• ###### 11. Re: Need help with calculating floating point numbers in JAVA
SASPROGR wrote:
Can you please provide info over the list. Also, how you got to know these numbers .... if by computation or is it published.

Thanks.
For numbers that can be represented, google IEEE 754.

For numbers that cannot be represented, please understand that since double is 64 bits, at most 2^64 distinct values can be represented, and also please understand that between any two values that can be represented, there are uncountably infinitely many that cannot.

For instance, between the numbers
``````0.0000000000000000000000000000000000000000_1_00000000000000000000000000000_0_
and
0.0000000000000000000000000000000000000000_1_00000000000000000000000000000_1_``````
there are infinitely many numbers, and not only infinitely many, but "more infinitely many" than there are integers from 0 to infinity.

Edited by: jverd on Dec 23, 2009 12:17 AM
• ###### 12. Re: Need help with calculating floating point numbers in JAVA
SASPROGR wrote:
Can you please provide info over the list.