miphis | 21 Jan 2013 00:17
Picon
Favicon

Silent error

The program looks like this:

--***********************
factorial :: Int->Int
factorial n = product [1..n]

main = do
        print $ factorial 50
--***********************

And that yields "0" (no errors). Is it a bug or feature? :)

______________________________
Акция! Скидка до - 50% на колготы зимних коллекций в
Линии магазинов EVA!
http://go.meta.ua/eva_colgotes 

_______________________________________________
Haskell mailing list
Haskell <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell
Gregory Collins | 21 Jan 2013 00:23
Gravatar

Re: Silent error

Integer overflow. factorial 50 mod 2^32 = 0.

Change "Int" to "Integer" to get the correct result (30414093201713378043612608166064768844377641568960512000000000000).


On Mon, Jan 21, 2013 at 12:17 AM, <miphis <at> meta.ua> wrote:
The program looks like this:

--***********************
factorial :: Int->Int
factorial n = product [1..n]

main = do
        print $ factorial 50
--***********************

And that yields "0" (no errors). Is it a bug or feature? :)

______________________________
Акция! Скидка до - 50% на колготы зимних коллекций в Линии магазинов EVA!
http://go.meta.ua/eva_colgotes


_______________________________________________
Haskell mailing list
Haskell <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell



--
Gregory Collins <greg <at> gregorycollins.net>
_______________________________________________
Haskell mailing list
Haskell <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell
Henning Thielemann | 21 Jan 2013 00:27
Picon

Re: [Haskell] Silent error


On Mon, 21 Jan 2013, miphis <at> meta.ua wrote:

> The program looks like this:
>
> --***********************
> factorial :: Int->Int
> factorial n = product [1..n]
>
> main = do
>        print $ factorial 50
> --***********************
>
> And that yields "0" (no errors). Is it a bug or feature? :)

This question is certainly better posted to haskell-cafe.

The answer is: Int has limited bitsize (32 ord 64 bit depending on your 
machine), thus it computes factorial modulo 2^32 or 2^64. You can get the 
correct result by replacing Int by Integer.
Benoit T | 21 Jan 2013 02:26
Picon

Re: Silent error

On Mon, Jan 21, 2013 at 01:17:17AM +0200, miphis <at> meta.ua wrote:
> The program looks like this:
> 
> --***********************
> factorial :: Int->Int
> factorial n = product [1..n]
> 
> main = do
>         print $ factorial 50
> --***********************
> 
> And that yields "0" (no errors). Is it a bug or feature? :)

Int is fixed-size (*) and is expected to use modular arithmetics (**)
therefore there is no notion of overflow, it just computes modulo
maxBound::Int

You must have been using a 32-bit haskell, that uses full-word Int, such
as HUGS, and it turns out that using 32-bit signed integers, you
eventually get -2**31 (this happens for n==32) and then multiplying by
any even integer yields 0 (34!==0).

GHC uses 31-bit or 63-bit Int (on 32- or 64-bit arch) and does not
encounter this case before factorial 50 (however the answer is also not
what you may have been expecting because it still doesn't fit).

cheers

*: this is specified in the standard
**: this does not seem to be specified in the standard

--

-- 
Benoit Triquet <benoit.triquet at gmail.com>
 .''`.
: :' :      We are debian.org. Lower your prices, surrender your code.
`. `'       We will add your hardware and software distinctiveness to
  `-        our own. Resistance is futile.
kudah | 16 Feb 2013 09:02
Picon

Re: Silent error

ghc's Int is 32-bit on 32-bit platforms, according to maxBound.

On Mon, 21 Jan 2013 02:26:34 +0100 Benoit T <benoit.triquet <at> gmail.com>
wrote:

> On Mon, Jan 21, 2013 at 01:17:17AM +0200, miphis <at> meta.ua wrote:
> > The program looks like this:
> > 
> > --***********************
> > factorial :: Int->Int
> > factorial n = product [1..n]
> > 
> > main = do
> >         print $ factorial 50
> > --***********************
> > 
> > And that yields "0" (no errors). Is it a bug or feature? :)
> 
> Int is fixed-size (*) and is expected to use modular arithmetics (**)
> therefore there is no notion of overflow, it just computes modulo
> maxBound::Int
> 
> You must have been using a 32-bit haskell, that uses full-word Int,
> such as HUGS, and it turns out that using 32-bit signed integers, you
> eventually get -2**31 (this happens for n==32) and then multiplying by
> any even integer yields 0 (34!==0).
> 
> GHC uses 31-bit or 63-bit Int (on 32- or 64-bit arch) and does not
> encounter this case before factorial 50 (however the answer is also
> not what you may have been expecting because it still doesn't fit).
> 
> cheers
> 
> *: this is specified in the standard
> **: this does not seem to be specified in the standard
> 

Gmane