Marcel Caraciolo

Greenhorn

Posts: 6

posted 10 years ago

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

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

posted 10 years ago

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?

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?

posted 10 years ago

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 ]

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

posted 10 years ago

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!

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!