Hi everyone,

anyone got a solution to this problem; some floating point arithmetic do not return the correct answers in Java one of such is subtracting 0.8 from 0.9; you would expect to get 0.1 but u get 0.09999999999999998.

No one should tell me about trying bigDecimal because that does not solve the problem.

The floor is open any ideas.

This is not a Java probelm. This is a problem with the binary representation of certain numbers (such as 1/3) It can't be done.

Read this entire site http://en.wikipedia.org/wiki/Floating_point specifically the section on Accuracy Problems.

Edit: IOW use BigDecimal if you must 1000% percent accurate (and yes, 1000 was intentional).

You could use:
DecimalFormta
or try Math.round after you have multiplied the result with 100..00 and then divide t=with the same number depending on the precision you want:

double result = 0.0999999999;
double d = result *1000;
d = Math.round(d);
d = d/1000;

1.) using BigDecimal will still not give u the right answer it just gives you a longer floating point value plus this problem does not exist in c# so Sun should have done something about it regardless of floating point representation.

doing this:
double result = 0.0999999999;
double d = result *1000;
d = Math.round(d);
d = d/1000;

is a good idea but it will mess with other calculations that should not be rounded

1.) using BigDecimal will still not give u the right answer it just gives you a longer floating point value plus this problem does not exist in c# so Sun should have done something about it regardless of floating point representation.

Well, sorry, I guess you are just going to have to stick to C# then if you want a 128-bit decimal floating point type. With Java you are stuck with float and double or you can use BigDecimal. The choice is yours, but complaining about it won't really get you anywhere.

1.) using BigDecimal will still not give u the right answer it just gives you a longer floating point value plus this problem does not exist in c# so Sun should have done something about it regardless of floating point representation.

Then C# is doing nothing other than the multiply divide playing around that has already been suggested (it just looks at where it starts to repeat and "rounds" it half-way intelligently). I would definately wonder what the performance cost is though (and there is one, whether you wish to believe it or not).

Hi everyone,

anyone got a solution to this problem; some floating point arithmetic do not return the correct answers in Java one of such is subtracting 0.8 from 0.9; you would expect to get 0.1 but u get 0.09999999999999998.

No one should tell me about trying bigDecimal because that does not solve the problem.

The floor is open any ideas.

Using big decimal does solve the problem but you have to assign the values to the bd objects using strings or the bd objects will inherit the problem. Just try it!

Don't you just hate when ignorant people just post to dead threads that have already been answered?
Check the date of the thread!
What kind of help you think you provided?

If you really want to help someone try a more recent thread and don't pick threads at random

This article has been dead for over six months. Start a new discussion instead.