Different results when applying function to equal values

Posted by Johannes Stiehler on Stack Overflow See other posts from Stack Overflow or by Johannes Stiehler
Published on 2010-03-31T14:37:23Z Indexed on 2010/04/01 8:13 UTC
Read the original article Hit count: 324

Filed under:
|
|

I'm just digging a bit into Haskell and I started by trying to compute the Phi-Coefficient of two words in a text. However, I ran into some very strange behaviour that I cannot explain.

After stripping everything down, I ended up with this code to reproduce the problem:

let sumTup = (sumTuples°concat) frequencyLists
let sumFixTup = (138, 136, 17, 204)
putStrLn (show ((138, 136, 17, 204) == sumTup))
putStrLn (show (phi sumTup))
putStrLn (show (phi sumFixTup))

This outputs:

True
NaN
0.4574206676616167

So although the sumTupand sumFixTup show as equal, they behave differently when passed to phi.

The definition of phi is:

phi (a, b, c, d) = 
    let dividend = fromIntegral(a * d - b * c)
        divisor = sqrt(fromIntegral((a + b) * (c + d) * (a + c) * (b + d)))
    in dividend / divisor

© Stack Overflow or respective owner

Related posts about haskell

Related posts about tuples