Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Double

 
changu mani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I've this code.


It prints 866.0799999999999.

If I change d2 to 123.02 it gives the output as 866.05. Please let me know if this is called rounding error in java. If this topic has already been discussed , please give me the link. I'll take a look and understand.

Thanks in Advance.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this has nothing to do with java. it has to do with how all computers store a number. floating point math is never precise. If you search this forum, you will find many topics on this subject.

Basically, the rule is that if you need accuracy, don't use doubles.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changu mani wrote:If this topic has already been discussed , please give me the link. I'll take a look and understand.

It has , and this link is so well known now, it's just called "Goldberg".

Winston
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to investigate the BigDecimal class.
 
changu mani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you All. In our Banking Product, they have made use of Double in upload Payroll files module. Now the total Debit amount is not equal to total credit amount for a few scenarios. It created all the problem. Thanks again for the links.
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you All. In our Banking Product, they have made use of Double in upload Payroll files module.
You should never use floating point numbers to store financial values. You should always use integer types or the BigDecimal class.
See this article http://www.javapractices.com/topic/TopicAction.do?Id=13 on how to use BigDecimal.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:You should never use floating point numbers to store financial values. You should always use integer types...

I would expand that to say you should never use a floating point type to store something that has discrete values. You wouldn't use a float to store the day of the month. You wouldn't use a float to store the number of students in a classroom. Since money is in discrete amounts, you should use some discrete variable type - like an int. This requires you to store values in pennies rather than dollars (or whatever currency equivalent you have), but that is trivial.
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expand that to say you should never use a floating point type to store something that has discrete values.
A fair point.

 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should never use a floating point type to store something that has discrete values.



Some nitpicking: a floating point type has a value range consisting of discrete values, albeit not evenly distributed ones.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
you should never use a floating point type to store something that has discrete values.

Some nitpicking: a floating point type has a value range consisting of discrete values, albeit not evenly distributed ones.

That's not a nitpick -- it's a non sequitor.

The statement "you should never use a floating point type to store something that has discrete values." is true. It did not say that there are no discrete values possible.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true, floating point numbers do constitute a list of discrete values. However this list is extremely unlikely to correspond to the list in anybody's real-life business scenario.
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> The statement "..." is true.

Did I say it was not? Methinks not.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic