You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Cyrille Artho (JIRA)" <ji...@apache.org> on 2015/05/07 08:35:59 UTC

[jira] [Reopened] (MATH-1118) Complex: semantics of equals != Double equals, mismatch with hashCode

     [ https://issues.apache.org/jira/browse/MATH-1118?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Cyrille Artho reopened MATH-1118:
---------------------------------

The bug has been fixed for class Complex, but not for Dfp. The code in "Report5a.java" still fails. The nature of the problem is exactly the same as in "Report5.java", so I have reported the bug in the same place.

If you would prefer a new issue on this instead, I can file "Report5a.java" as a new bug.

> Complex: semantics of equals != Double equals, mismatch with hashCode
> ---------------------------------------------------------------------
>
>                 Key: MATH-1118
>                 URL: https://issues.apache.org/jira/browse/MATH-1118
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 3.2
>         Environment: Mac OS 10.9, Java 6, 7
>            Reporter: Cyrille Artho
>             Fix For: 3.3
>
>         Attachments: MATH-1118.patch, MATH-1118.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:  equals-hashcode on c1 and c2
>     assertTrue("Contract failed: equals-hashcode on c1 and c2", c1.equals(c2) ? c1.hashCode() == c2.hashCode() : true);



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)