Calculation on large numbers return 0

32 views
Skip to first unread message

rvdalen

unread,
Aug 11, 2017, 5:34:48 AM8/11/17
to purescript
Hi everyone,

I have the following function definition.

factorial :: Int -> Int

factorial n = go n 1 1

 where

   go :: Int -> Int -> Int -> Int

   go n i r =

     if i == n

       then i * r

       else go n (i + 1) (i * r)


If I apply the `factorial` function to the value `999999999`, it returns 0, which is unexpected.
If I use a smaller number, say `5` it returns 120.

Can someone explain this behavior please?

Regards
--Rouan

Phil Freeman

unread,
Aug 11, 2017, 12:55:58 PM8/11/17
to purescript
You could try using a library like purescript-hugenums which supports larger integers.

Thanks,

Phil.

--
You received this message because you are subscribed to the Google Groups "purescript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to purescript+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/purescript.
For more options, visit https://groups.google.com/d/optout.

rvdalen

unread,
Aug 12, 2017, 2:54:43 AM8/12/17
to purescript
Hi Phil,

Thanks for the tip about the purescript-hugenums library.  I will look into that.

What I am more interested at the moment is how to explain the behavior.
Why is this happening? 
What are the limits of `Int` and when can I expect this behavior to kick in? 
Is this documented somewhere?

Regards
--Rouan

Michael Ficarra

unread,
Aug 12, 2017, 5:10:30 AM8/12/17
to purescript
Reply all
Reply to author
Forward
0 new messages