3 ms·
I recall there being an issue with double.GetHashCode() in the early versions of .Net, where it would return different hash values for 0.0 and -0.0. That got f
by magicalhippo 6y ago
I recall there being an issue with double.GetHashCode() in the early versions of .Net, where it would return different hash values for 0.0 and -0.0.
That got fixed, but it seems a variation with signed NaN recently got fixed[1] in .Net Core (taking care to handle all the possible NaN values).
Floats are deceptively easy to use. Which is nice but kinda sucks too, given all the footguns they bring to the party.
[1]: https://github.com/dotnet/runtime/issues/6307 https://github.com/dotnet/runtime/issues/6307
- TacticalCoder 6y ago> I recall there being an issue with double.GetHashCode() in the early versions of .Net, where it would return different hash values for 0.0 and -0.0. I never thought about that (I avoid FP as much as I can) but, oh boy, the can of worms! .Net (or Java) OOP where an object's hashcode needs to give the same (or not?) value for 0.0 or -0.0: this is the kind of stuff nightmares are made off. I'm sure this can be turned into some very funny "Java puzzler".
- mattgreenrocks 6y agoThe fact that NaN is technically a set of values in IEEE-754 (rather than just one agreed-upon value) was quite eye-opening to me.
- emergie 6y agoWhat is wrong in having different hashes for different numbers? https://float.exposed/0x0000000000000000 https://float.exposed/0x0000000000000000 https://float.exposed/0x8000000000000000 https://float.exposed/0x8000000000000000
- magicalhippo 6y agoIf you compare for equality then those two numbers, 0.0 and -0.0, are considered equal. It seems a lot of people consider that a good reason for them to have the same hash value, which I consider entirely fairly reasonable. Of course since NaN != x, whatever x is (including infinity and NaN), one could argue it's fine for different NaNs to return different hash codes. But I think most people think of NaN as one thing, and not the 9007199254740990 or so different NaN encodings[1] there are in a double. [1]: https://en.wikipedia.org/wiki/IEEE_754-1985#NaN https://en.wikipedia.org/wiki/IEEE_754-1985#NaN
- emergie 6y agoYou inspired me to check java world. // 0.0d / 0 equals 0x7ff8000000000000 (NaN) // Math.sqrt(-1) equals 0xfff8000000000000 (NaN) // 0x0p+0d is a funky way to specify 0x0000000000000000 (0) // -0x0p+0d is a funky way to specify 0x8000000000000000 (-0) // without 0xHEXp+NUMd my compiller optimizes "-0" literal to 0 // 0 == -0 0x0p+0d == -0x0p+0d // hashCodes for 0 and -0 are different Double.hashCode(0x0p+0d) != Double.hashCode(-0x0p+0d) // hashCodes for different NaNs collapse to the same value Double.hashCode(0.0d / 0) == Double.hashCode(Math.sqrt(-1))
- magicalhippo 6y agoSo yeah, same issue that .Net had. I can't find the original complaints about this (was back in early 2000s after all), but as I recall it someone was very surprised that something that compares as equality ended up two times in the hashmap, and that messed up their code bad.