New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance `Hashable Double` does not behave according to the laws. #173

Open
NorfairKing opened this Issue Nov 6, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@NorfairKing
Copy link

NorfairKing commented Nov 6, 2018

It is broken for -0.0:

      1) Test.Validity.Hashable, Hashable Double, hashWithSalt :: Int -> Double -> Int, satisfies (a == b) => (hashWithSalt n a) == (hashWithSalt n b), for every n and for valid Double
           Falsifiable (after 1 test):
             (-0.0,0.0)
             0
           expected: 0
            but got: -9223372036854775808

It's also broken for different values of NaN, if you ignore that NaN /= Nan holds.

@NorfairKing

This comment has been minimized.

Copy link

NorfairKing commented Nov 14, 2018

This brings the following problem with it:

someFunc :: IO ()
someFunc = do
    let pz = read "0.0" :: Double
    let nz = read "-0.0" :: Double
    print $ HM.fromList [(pz, "positive"), (nz, "negative")]
fromList [(0.0,"positive"),(-0.0,"negative")]
@hvr

This comment has been minimized.

Copy link
Collaborator

hvr commented Nov 14, 2018

sigh Double and Float's special semantic values keep make subtle bugs appear everywhere :-(

@NorfairKing

This comment has been minimized.

Copy link

NorfairKing commented Nov 14, 2018

@hvr There is also the problem that NaN causes trivial hash collisions. Although I guess that's broken as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment