[ https://issues.apache.org/jira/browse/MATH1118?page=com.atlassian.jira.plugin.system.issuetabpanels:commenttabpanel&focusedCommentId=13968963#comment13968963
]
Cyrille Artho commented on MATH1118:

It is indeed not clear which way this bug should be fixed.
Another option (assuming the problem only occurs due to the sign with value 0.0) is to override
hashCode such that hashCode(0.0d) returns hashCode(0.0d), and works as is in the other cases.
> Complex: semantics of equals != Double equals, mismatch with hashCode
> 
>
> Key: MATH1118
> URL: https://issues.apache.org/jira/browse/MATH1118
> Project: Commons Math
> Issue Type: Bug
> Affects Versions: 3.2
> Environment: Mac OS 10.9, Java 6, 7
> Reporter: Cyrille Artho
> Attachments: MATH1118.patch, Report5.java, Report5a.java
>
>
> Two complex numbers with real/imaginary parts 0.0d but different signs compare as equal
numbers. This is according to their mathematical value; the comparison is done via
> return (real == c.real) && (imaginary == c.imaginary);
> Unfortunately, two Double values do NOT compare as equal in that case, so real.equals(c.real)
would return false if the signs differ.
> This becomes a problem because for the hashCode, MathUtils.hash is used on the real and
imaginary parts, which in turn uses Double.hash.
> This violates the contract on equals/hashCode, so Complex numbers cannot be used in a
hashtable or similar data structure:
> Complex c1 = new Complex(0.0, 0.0);
> Complex c2 = new Complex(0.0, 0.0);
> // Checks the contract: equalshashcode on c1 and c2
> assertTrue("Contract failed: equalshashcode on c1 and c2", c1.equals(c2) ? c1.hashCode()
== c2.hashCode() : true);

This message was sent by Atlassian JIRA
(v6.2#6252)
