This discussion is archived
1 2 Previous Next 22 Replies Latest reply: Jan 31, 2009 5:02 AM by 807588

# float operations giving incorrect answers

Currently Being Moderated
Hi.

I have quite a problem, that I don't know if anyone can help me with.
Looking at the code and output explains better:
``````float troco = 10.9F;
troco = troco - 10;
System.out.println(troco);``````
Now the output of this is 0.8999996 when it should have been 0.90.

Thank you for any help.
• ###### 1. Re: float operations giving incorrect answers
Currently Being Moderated
It is doing the correct thing. You just need to learn how floating point numbers work in computers.

[SOME THINGS YOU SHOULD KNOW ABOUT FLOATING-POINT ARITHMETIC|http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2|No, it's not a bug in Java. C/C++ does the same thing, and it's not a bug there either.]
[What Every Computer Scientist Should Know About Floating-Point Arithmetic|http://docs.sun.com/source/806-3568/ncg_goldberg.html|Seriously, it's not a bug. Get over it.]
[Another good (slightly simpler) FP explanation|http://mindprod.com/jgloss/floatingpoint.html|Nope, not a bug in Java]
• ###### 2. Re: float operations giving incorrect answers
Currently Being Moderated
As a side note, you should use double, not float. There's almost never any reason to use float.
• ###### 3. Re: float operations giving incorrect answers
Currently Being Moderated
double ends up doing (almost) the same thing.

Learning how to solve that simple piece of code would go a long way.
• ###### 4. Re: float operations giving incorrect answers
Currently Being Moderated
Define what you mean by "solve".
• ###### 5. Re: float operations giving incorrect answers
Currently Being Moderated
as in to give me 0.9 instead of the 0.899...
• ###### 6. Re: float operations giving incorrect answers
Currently Being Moderated
elvispt wrote:
double ends up doing (almost) the same thing.
Of course it does. It's still a floating point number, represented the same way, just with greater precision. I never said that using double would make this issue vanish.
Learning how to solve that simple piece of code would go a long way.
So take the time to read one or more of the links I provided. I'm not interested in providing a turnkey solution, but I'm happy to help you learn the concepts so you can understand what's going on and how to work with it.
• ###### 7. Re: float operations giving incorrect answers
Currently Being Moderated
elvispt wrote:
as in to give me 0.9 instead of the 0.899...
The numeric value 0.9? Won't happen with float or double. You'd have to use BigDecimal.

If you mean format the output as 0.9, then google for java number formatting.
• ###### 8. Re: float operations giving incorrect answers
Currently Being Moderated
ok. Used BigDecimal and it worked.

Thx for the help.

Edited by: elvispt on 30/Jan/2009 23:22

Edited by: elvispt on 30/Jan/2009 23:22
• ###### 9. Re: float operations giving incorrect answers
Currently Being Moderated
• ###### 10. Re: float operations giving incorrect answers
Currently Being Moderated
If you're dealing with money in a real production environment--somewhere where fixed decimal precision matters--then BigD is probably the way to go. But for most other cases, double will be fine. It will be a lot simpler, and the error will be small enough that when you format for display, you get the desired values.
• ###### 11. Re: float operations giving incorrect answers
Currently Being Moderated
I am learning programming through JAVA, and It's a small project that I have been doing.
I iniatlly said it was an error, because I did come across a bug in Java before.

If a Scanner is used to read an integer it can't be used to read a string later.
I went around this issue bu just creating a new Scanner object.

But anyway Thank you.

I'm still in the process of learning how to correctly use BigDecimal in an If/else statement.

Thank you again.
• ###### 12. Re: float operations giving incorrect answers
Currently Being Moderated
If you do a lot of calculations you may find that BigDecimal is slower than using double or long with fixed precision.
• ###### 13. Re: float operations giving incorrect answers
Currently Being Moderated
jverd wrote:
As a side note, you should use double, not float. There's almost never any reason to use float.
I agree. float and Float shouldn't be used without good reason. Just use double or Double.
• ###### 14. Re: float operations giving incorrect answers
Currently Being Moderated
elvispt wrote:
ok. Used BigDecimal and it worked.

...
And now try to divide 2 by 6 with BigDecimal. Does it still "work"?
1 2 Previous Next