commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject [math] Fixed equals/hashcode contract failure for Dfp.
Date Thu, 07 May 2015 14:29:29 GMT
Repository: commons-math
Updated Branches:
  refs/heads/master e4b3ac859 -> 51cb31ef2


Fixed equals/hashcode contract failure for Dfp.

Thanks to Cyrille Artho.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/51cb31ef
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/51cb31ef
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/51cb31ef

Branch: refs/heads/master
Commit: 51cb31ef21f879220b8fa20e37bae15bfb2c4d21
Parents: e4b3ac8
Author: Luc Maisonobe <luc@apache.org>
Authored: Thu May 7 16:28:51 2015 +0200
Committer: Luc Maisonobe <luc@apache.org>
Committed: Thu May 7 16:28:51 2015 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                                 |  5 ++++-
 src/main/java/org/apache/commons/math4/dfp/Dfp.java     |  2 +-
 src/test/java/org/apache/commons/math4/dfp/DfpTest.java | 10 ++++++++++
 3 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/51cb31ef/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7967a96..19f4046 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,7 +54,10 @@ If the output is not quite correct, check for invisible trailing spaces!
     </release>
 
     <release version="4.0" date="XXXX-XX-XX" description="">
-      <action dev="luc" type="fix" issue="MATH-1223">
+      <action dev="luc" type="fix" issue="MATH-1118" > <!-- backported to 3.6 -->
+        Fixed equals/hashcode contract failure for Dfp.
+      </action>
+      <action dev="luc" type="fix" issue="MATH-1223"> <!-- backported to 3.6 -->
         Fixed wrong splitting of huge number in extended accuracy algorithms.
       </action>
       <action dev="luc" type="fix" issue="MATH-1143">

http://git-wip-us.apache.org/repos/asf/commons-math/blob/51cb31ef/src/main/java/org/apache/commons/math4/dfp/Dfp.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/dfp/Dfp.java b/src/main/java/org/apache/commons/math4/dfp/Dfp.java
index 7f15e88..4c02ac0 100644
--- a/src/main/java/org/apache/commons/math4/dfp/Dfp.java
+++ b/src/main/java/org/apache/commons/math4/dfp/Dfp.java
@@ -924,7 +924,7 @@ public class Dfp implements RealFieldElement<Dfp> {
      */
     @Override
     public int hashCode() {
-        return 17 + (sign << 8) + (nans << 16) + exp + Arrays.hashCode(mant);
+        return 17 + (isZero() ? 0 : (sign << 8)) + (nans << 16) + exp + Arrays.hashCode(mant);
     }
 
     /** Check if instance is not equal to x.

http://git-wip-us.apache.org/repos/asf/commons-math/blob/51cb31ef/src/test/java/org/apache/commons/math4/dfp/DfpTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/dfp/DfpTest.java b/src/test/java/org/apache/commons/math4/dfp/DfpTest.java
index 9725f36..c9b375a 100644
--- a/src/test/java/org/apache/commons/math4/dfp/DfpTest.java
+++ b/src/test/java/org/apache/commons/math4/dfp/DfpTest.java
@@ -1652,4 +1652,14 @@ public class DfpTest extends ExtendedFieldElementAbstractTest<Dfp>
{
         Assert.assertTrue(field.newDfp("NaN").isNaN());
     }
 
+    @Test
+    public void testEqualsHashcodeContract() {
+        DfpField var1 = new DfpField(1);
+        Dfp var6 = var1.newDfp(-0.0d);
+        Dfp var5 = var1.newDfp(0L);
+
+        // Checks the contract:  equals-hashcode on var5 and var6
+        Assert.assertTrue(var5.equals(var6) ? var5.hashCode() == var6.hashCode() : true);
+    }
+
 }


Mime
View raw message