Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 698503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:16:56+00:00 2026-05-14T03:16:56+00:00

I’m just digging a bit into Haskell and I started by trying to compute

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:16:57+00:00Added an answer on May 14, 2026 at 3:16 am

    This might be a case of integer overflow. The value being passed into fromIntegral in your divisor is 3191195800, which is larger than a 32-bit signed Int can hold.

    In ghci (or whatever you’re using), use

    :t sumTup
    :t sumFixTup
    

    to see the types of those variables. I’m guessing you’ll find that sumTup is (Int, Int, Int, Int) (overflows) and sumFixTup is (Integer, Integer, Integer, Integer) (doesn’t overflow).

    Edit: on second thought, a tuple of Ints can’t be equal to a tuple of Integers. Even so, I think that ghci will fix the type of sumFixTup to be a tuple of Integers, while sumTup probably has a type of the form (Num a) => (a, a, a, a) or (Integral a) => (a, a, a, a), which depends on the function defining it.

    Ghci will then convert them to Integers to compare with sumFixTup, but may convert them to Ints when calculating the divisor in phi, causing overflow.


    Another edit: KennyTM, you’re half right:

    Prelude> :t (1,2,3,4)
    (1,2,3,4) :: (Num t, Num t1, Num t2, Num t3) => (t, t1, t2, t3)
    Prelude> let tup = (1,2,3,4)
    Prelude> :t tup
    tup :: (Integer, Integer, Integer, Integer)
    

    So for the examples given in the question:

    putStrLn (show ((138, 136, 17, 204) == sumTup))
    

    The literal (138, 136, 17, 204) is inferred to be a tuple of Int to match sumTup, and they compare equal.

    putStrLn (show (phi sumTup))
    

    sumTup consists of Ints, causing overflow as suggested above.

    putStrLn (show (phi sumFixTup))
    

    sumFixTup consists of Integers, giving a correct result. Note that sumTup and sumFixTup were never compared directly, so my earlier edit was based on a misreading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.