Problems with Maths (decimals) In java calculations

In a normal calculator type in(27.8 / 3) * 12 = 111.2Do this in Java and I get111.20000000000002This is causing a problem in my calculations, is this a known bug and how can i get around it ?Friendly RegardsSteven
[262 byte] By [Lunnya] at [2007-10-3 3:01:49]
# 1
http://docs.sun.com/source/806-3568/ncg_goldberg.html
CeciNEstPasUnProgrammeura at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 2
This is not a bug it is normal functionality for IEEE floating point numbers.Search these forums and you will find thread after thread after thread about this behaviour.Also, read this for what's really going on: http://docs.sun.com/source/806-3568/ncg_goldberg.html
jbisha at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 3
Hey guys thanks for the reply,I don't really have the time to read the links kinda need this fixed quick, any clues about how to get around this then ?
Lunnya at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 4
DecimalFormat I suppose. By the way, the article linked is titled "What Every Computer Scientist Should Know About Floating point Arithmetics", and it's correct with that assumption. So you should take the time to read it.
CeciNEstPasUnProgrammeura at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 5

Yes i know I will read the article but as I said just wanted get this working before i go home today.

This is how I fixed it anyway

DecimalFormat threeDecimals = new DecimalFormat("0.000");

thirdOfWk = Double.parseDouble(threeDecimals.format(thirdOfWk));

Thanks for the help guys

Lunnya at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 6

> Yes i know I will read the article but as I said just

> wanted get this working before i go home today.

>

> This is how I fixed it anyway

>

> DecimalFormat threeDecimals = new DecimalFormat("0.000");

> thirdOfWk = Double.parseDouble(threeDecimals.format(thirdOfWk));

That doesn't really resolve the problem. What does the following output now?

System.out.println(thirdOfWk);

I doubt it will be what you want (and even if it is, this isn't a reliable method for getting the printed format that you desire).

doremifasollatidoa at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 7

> Yes i know I will read the article but as I said just

> wanted get this working before i go home today.

>

> This is how I fixed it anyway

>

> DecimalFormat threeDecimals = new

> DecimalFormat("0.000");

> thirdOfWk =

> Double.parseDouble(threeDecimals.format(thirdOfWk));

>

> Thanks for the help guys

I think you are going to need to do some rounding otherwise this will not fix all the problems.

Consider that:

111.20000000000002

truncated to 3 decimals is:

111.200

But what if the number comes up something like this (I am not sure that it can happen but I would guess that it can):

123.19999999999998 (instead of 123.2)

truncated to 3 decimals is:

123.199

And I think doremifasollatido is correct and you have another problem (because you are putting the truncated number back into a Double).

You really need to use BigDecimal or integers. For integers you would need to scale the numbers and then display with decimals.

Message was edited by:

jbish

jbisha at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 8

> I think you are going to need to do some rounding

> otherwise this will not fix all the problems.

>

> Consider that:

> 111.20000000000002

> truncated to 3 decimals is:

> 111.200

>

> But what if the number comes up something like this

> (I am not sure that it can happen but I would guess

> that it can):

> 123.19999999999998 (instead of 123.2)

> truncated to 3 decimals is:

> 123.199

>

Happily NumberFormat with a limited number of fraction digits set rounds, rather than trucates. An the moral is - always use DecimalFormat/NumberFormat when outputing float values as text except when it's debugging and dosen't matter.

And there's really not much point in rounding a floating point value except when converting to an integer or string representation, because the errors may always come back. Particularly since the internal representation is binary, not decimal.

malcolmmca at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 9

> Happily NumberFormat with a limited number of

> fraction digits set rounds, rather than trucates. An

> the moral is - always use DecimalFormat/NumberFormat

> when outputing float values as text except when it's

> debugging and dosen't matter.

Thanks for the clarification. I have to admit I was too lazy to look at the API even though the thought had crossed my mind.

jbisha at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...
# 10
> I don't really have the time to read the links kinda> need this fixed quickIsn't it this exact mentality that results in the need for another quick fix again when there is a new bug ? The sooner you catch a bug the less havoc it can have on your entire
Aknibbsa at 2007-7-14 20:51:32 > top of Java-index,Java Essentials,Java Programming...