• Post Reply Bookmark Topic Watch Topic
  • New Topic

Precision problem with Java  RSS feed

 
Marcel Caraciolo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I'm with a little problem in Java...
If anyone could help, please...

I have this little calculation to do with java:
x = 51.9396/60;
The problem it's the result that's 0.865.
but the real thing it's the result is more precise than that... It's 0.864999999999...
And i want this exactly result (with only showing 15 positions of precision)
I've already tried using BigDecimal.. but the problem persists: Here it~s the code:

If anyone could help...

thks ,

Marcel
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused.

At first you mention 51.9396 / 60, which is 0.86566, exactly.

Assuming that's just a typo, and you really want 51.936 / 60 (as the code shows), that's exactly 0.8656. There are no further decimals.

So I'm not sure what exactly you're looking for? Where does the 0.865 (or 0.864999999999) come from?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you actually trying to find x such that x/60 = 0.864999999999 (to 15 places, rounding down)?

In other words, how many nines you need to put on the numerator 51.89999... in order to get the desired precision in the quotient?
[ September 28, 2007: Message edited by: marc weber ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I imagine your problem is that when you print the result, it's showing ...99999999 and you don't want that. This is happening because you can't represent many decimal values exactly as floating point numbers, and 51.936 is one of those. If you try to print it, you'll see that it's actually got those 99999's in it.

The solution is to create an exact BigDecimal by passing not an inexact double to the constructor, but the exact, precise decimal value as a String:

df2 = new BigDecimal("51.936");

and then you'll get the exact answer!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!