Following situation (code is reduced to make it clearer):
public class Hex {
public float x;
public float y;
public float z;
public Hex(float newX = 0f, float newY = 0f) {
x = newX;
y = newY;
Flatten();
}
public void Flatten() {
z = -x - y;
}
public override int GetHashCode() {
return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
// Calculate hash based on Vector3 calculation
}
}
Hex hex = new Hex(); // all variables equals 0f
So naturally:
Debug.Log(hex.x == hex.y); //true
Debug.Log(hex.x == hex.z); //true
Debug.Log(hex.y == hex.z); //true
But:
Debug.Log(hex.x.GetHashCode()); //0
Debug.Log(hex.y.GetHashCode()); //0
Debug.Log(hex.z.GetHashCode()); //-2147483648
Further investigation concluded:
Debug.Log((-0f).GetHashCode()); //-2147483648
So this is a Problem:
Hex hex2 = new Hex(-0f, -0f);
Debug.Log(hex.GetHashCode() == hex2.GetHashCode()); //false
Because I want to use the Hex-class as a dictionary key, the Hash of both hex and hex2 should be identical. Performance is very important, because the key is called a lot.
So what is the quickest function/calculation to fix this?