• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing two variables  RSS feed

 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's get it out of the way -- I'm stumped. On something that should be pretty simple to solve. Code follows.



The output:

I wrote another simple program and hardcoded the values: aDouble = 100.0; bDouble= 100.0000 Using aDouble.equals(bDouble) returns true, just as one would expect. So what am I overlooking? Only partly embarrassed to say I've spent a bunch of hours trying to solve this.

Regards,
Robert
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For objects, == is a comparison for identity, not equality.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. the code is needlessly complicated.

will suffice.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, read this... http://www.coderanch.com/how-to/java/JavaBeginnersFaq

especially the item regarding "arithmetic inaccuracies".


Second, I would recommend increasing the precision of your printf(), to better see what is going on... try something like this...




Hope this helps,
Henry
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:For objects, == is a comparison for identity, not equality.
I know. The point I was trying to make is that it returned false using == and .equals(). And thanks for the simplification -- much appreciated.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
First, read this... http://www.coderanch.com/how-to/java/JavaBeginnersFaq
especially the item regarding "arithmetic inaccuracies".

Second, I would recommend increasing the precision of your printf(), to better see what is going on... try something like this...

Hope this helps,
Henry
Thank you, Henry. This indeed did help. Still not sure where i am going with it, but at least I can see why it returns false.

Regards,
Robert

edited to clean up some white space.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:Thank you, Henry. This indeed did help. Still not sure where i am going with it, but at least I can see why it returns false.

Basically, you should never use floating point types if you need exact values. The full explanation involves understanding how floats and doubles are stored (basically a binary representation of standard form; you can find full details here), but 0.1 is an often-used example of a value that can't be stored exactly.

If you need exact values you should either use BigDecimal, or store your values as integers and only divide right at the end.

HIH

Winston
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your help. Once I replaced Double with BigDecimal, all works as one would hope. If a moderator would mark this solved, I would be most appreciative (not sure if I have the rights/privileges to do so).

Regards,
Robert
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:Thank you all for your help. Once I replaced Double with BigDecimal, all works as one would hope. If a moderator would mark this solved, I would be most appreciative (not sure if I have the rights/privileges to do so).

Regards,
Robert


There should be a large resolve button on the bottom of this topic (which will mark it as such) -- which will likely disappear if I mark it as resolved. I'll refrain from doing that for a bit, so that you can do it.

Henry
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you compare BigDecimals and you want to compare values, never use equals. Use compareTo instead.

This prints:
equals: false
compareTo: 0


The reason is: BigDecimal internally holds a BigInteger for unscaled value and int for scale.
The equals method compares them for exact values, so n1 (unscaled value = 1, scale = 1) does not equal n2 (unscaled value = 10, scale = 2)

Read javadoc of java.lang.BigDecimal for more details.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To continue from what PP showed you. It is not in the documentation, though you will find it in resources like Bloch's Effective Java™.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, thank you for your insights. I will keep compareTo in mind.

Regards,
Robert
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!