[Haskell-beginners] Int V.S. Word32

Daniel Fischer daniel.is.fischer at googlemail.com
Sun Nov 20 14:34:38 CET 2011


On Sunday 20 November 2011, 13:53:20, Sebastian Hungerecker wrote:
> On 20.11.2011 07:50, Haisheng Wu wrote:
> > Hmm... I think I made a little confusion so I put my finding here:
> > http://haisgwu.info/posts/2011-11-20-euler-problem-14.html
> > 
> > I do got stack overflow thus need several compile opts to fix it.
> > Not sure if it is what you mean by "You get overflow using 32-bit
> > types here."
> 
> No, what he meant was that your n does not always fit into a 32-bit
> integer, so the integer will overflow (i.e. once it exceeds its maximum
> value it will wrap around and become negative).

That.

With Word32 you won't get negative values of course, but the wrapped-around 
values are completely bogus nevertheless.
With signed 32-bit integers, the overflow puts you into an infinite loop of 
negative values for several starting values, the smallest is something 
around 113000 iirc.
With Word32, I never tested what the smallest starting value landing you in 
an infinite loop is, but (2^32-1)/3 surely does (~> 0 ~> 0).



More information about the Beginners mailing list