programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Precision problem with Java

Greenhorn
Posts: 6
Hi!
I'm with a little problem in Java...

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

Rancher
Posts: 42975
76
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?

Sheriff
Posts: 11343
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 ]

author and iconoclast
Sheriff
Posts: 24217
38
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!

 Ever since I found this suit I've felt strange new needs. And a tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS