Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 52AC310238 for ; Thu, 17 Apr 2014 11:15:26 +0000 (UTC) Received: (qmail 88258 invoked by uid 500); 17 Apr 2014 11:15:21 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 88095 invoked by uid 500); 17 Apr 2014 11:15:17 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 88084 invoked by uid 99); 17 Apr 2014 11:15:16 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 17 Apr 2014 11:15:16 +0000 Date: Thu, 17 Apr 2014 11:15:16 +0000 (UTC) From: "Gilles (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Comment Edited] (MATH-1118) Complex: semantics of equals != Double equals, mismatch with hashCode MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/MATH-1118?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13972842#comment-13972842 ] Gilles edited comment on MATH-1118 at 4/17/14 11:14 AM: -------------------------------------------------------- Uploaded patch for making "equals" compatible with "hashCode" and adding new "equals" methods for testing floating-point equality (using implementations in "o.a.c.m.util.Precision"). OK to commit? was (Author: erans): Uploaded patch for making "equals" compatible with "hashCode" and adding new "equals" methods for testing floating equality (using implementations in "o.a.c.m.util.Precision"). OK to commit? > 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 > 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.2#6252)