You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2023/07/26 14:22:00 UTC
[jira] [Created] (NUMBERS-201) FP64 equals is not consistent with hashCode
Alex Herbert created NUMBERS-201:
------------------------------------
Summary: FP64 equals is not consistent with hashCode
Key: NUMBERS-201
URL: https://issues.apache.org/jira/browse/NUMBERS-201
Project: Commons Numbers
Issue Type: Bug
Components: field
Affects Versions: 1.1
Reporter: Alex Herbert
The FP64 class wraps a {{double}} so that it can be used as a field. The implementation of {{equals}} uses Precision with an allowed difference of up to 2 ULP:
{code:java}
public boolean equals(Object other) {
if (other instanceof FP64) {
final FP64 o = (FP64) other;
return Precision.equals(value, o.value, 1);
}
return false;
}{code}
The hashCode is generated using Double.hashCode(value).
The result is that two objects that are equal can generate a different hash code. This breaks the contract of Object.equals where two objects that are equal have the same hash code.
Possible fixes:
# Use the equivalent of (({{{}Double)value).equals(o.value){}}} for a binary equality check between values that is consistent with double.
# Document the class as having an equals method that is not consistent with hash code.
I do not understand the requirement for using Precision. If a user has their own tolerance for field equivalence then this should be left to them to set the threshold. So I would recommend option 1.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)