Repository navigation
Description
The built in floating point types compare NaN as unequal when using == and != (following IEEE semantics) but compare it as equal when using the Equals method.
Your floating point based types currently use IEEE semantics even for Equals. I suggest using the same behaviour as the built in types in your floating point based types like vectors or quaternions.
The MSDN documentation of Equals contains an exception that allows A.Equals(A) to return false on floating point types, so you don't strictly violate its contract.
But returning false still breaks hash tables and does not match the behaviour of the built in types, so I consider it a bad idea.
This can be avoided by calling Equals on the members instead of == in the implementation of Equals but not in == and !=.
For example with quaternion,
replace
public bool Equals(Quaternion other)
{
return (X == other.X &&
Y == other.Y &&
Z == other.Z &&
W == other.W);
}
with
public bool Equals(Quaternion other)
{
return (X.Equals(other.X) &&
Y.Equals(other.Y) &&
Z.Equals(other.Z) &&
W.Equals(other.W));
}
You might want to add tests that check that == and != compare all the above cases as unequal, so that they match the IEEE specification.
Replace:
// Counterintuitive result - IEEE rules for NaN comparison are weird!
Assert.False(a.Equals(a));
Assert.False(b.Equals(b));
Assert.False(c.Equals(c));
Assert.False(d.Equals(d));
with:
// Equals does not follow IEEE semantics since many consumers rely on equality being reflexive.
// This includes collections like `Dictionary<K,V>` or `HashSet<T>`
Assert.True(a.Equals(a));
Assert.True(b.Equals(b));
Assert.True(c.Equals(c));
Assert.True(d.Equals(d));
// Counterintuitive result - IEEE rules for NaN comparison are weird!
Assert.False(a == a);
Assert.False(b == b);
Assert.False(c == c);
Assert.False(d == d);
Assert.True(a != a);
Assert.True(b != b);
Assert.True(c != c);
Assert.True(d != d);
Activity
mellinoe commentedon Nov 13, 2014
We can have a discussion about whether we want to change this behavior, but currently there's a couple more points to consider:
CodesInChaos commentedon Nov 13, 2014
In my experience
Equalsis mostly used by containers and search algorithms which rely on reflexivity. For these algorithms a performance gain is irrelevant if prevents them from working. Don't sacrifice correctness for performance.This breaks all hash based sets and dictionaries,
Contains,Find,IndexOfon various collections/LINQ, set based LINQ operations (Union,Except, etc.).Code that does actual computations where IEEE semantic is acceptable usually works on concrete types and uses
==/!=(or more likely epsilon comparisons). You currently can't write high performance computations using generics since you need arithmetic operations for that, but these aren't available through interfaces/virtual methods. If you add a common interface for float/double that enables arithmetic, you can use the same interface to add aIeeeEqualsmethod.I don't know any precedent in the BCL where
Equalsfollows IEEE instead of being reflexive. Counter examples includeSingle,Double,TupleandSystem.Numerics.Complex.I agree that the behaviour shouldn't depend on the availability of SIMD. My suggestion is to be reflexive in
Equalsand follow IEEE for==/!=regardless of SIMD availability even if that could reduce performance in some cases.jods4 commentedon Dec 7, 2014
@CodesInChaos basically you say that you use equality on
doubleorfloatand use them as hashtable keys orFindorIndexOf? This is a very dangerous proposition. Unless your floating point numbers have been carefully constructed you can't expect equality to work reliably even for non-NaN numbers.I think using
Equalson floating point numbers shouldn't be a supported scenario at all. That said @CodesInChaos has a point about consistent behavior with existing types.A workarounds exists as it is possible to write a
IEqualityComparerthat does what you want.mellinoe commentedon Jan 9, 2015
I apologize for leaving this issue open for so long without giving any closure. For a couple of reasons, we won't be changing this behavior:
Merge pull request #72 from dotnet/rc2samples