
*X> 3^40 `mod` 3 == modexp2 3 40 3 False *X> modexp2 3 40 3 0 *X> 3^40 `mod` 3 0 I'm confused. Last I checked, 0 == 0. Using GHC 7.4.1, and the file x.hs (which has been loaded in ghci) can be found here: http://hpaste.org/69342 I noticed this after prop_sanemodexp was failing. Any help would be appreciated, - Clark

It looks like you are overflowing `Int` with 3^40.
In your QuickCheck test, the function signature uses Int:
prop_sanemodexp :: Int -> Int -> Int -> Property
Note:
Prelude> 3^40
12157665459056928801
Prelude> 3^40 :: Int
689956897
Prelude> 3^40 `mod` 3
0
Prelude> (3^40 `mod` 3) :: Int
1
L.
On Thu, May 31, 2012 at 5:35 PM, Clark Gaebel
*X> 3^40 `mod` 3 == modexp2 3 40 3 False *X> modexp2 3 40 3 0 *X> 3^40 `mod` 3 0
I'm confused. Last I checked, 0 == 0.
Using GHC 7.4.1, and the file x.hs (which has been loaded in ghci) can be found here: http://hpaste.org/69342
I noticed this after prop_sanemodexp was failing.
Any help would be appreciated, - Clark _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 5/31/12 12:48 PM, Lorenzo Bolla wrote:
It looks like you are overflowing `Int` with 3^40. In your QuickCheck test, the function signature uses Int:
+1. I was bitten by this issue recently as well. When playing around with properties and debugging in GHCi, always beware of type defaulting to Integer. -- Live well, ~wren

Hi Clark, ghci is defaulting to Integer modexp2 forces Int Int overflows with 3^40 On 31/05/12 17:35, Clark Gaebel wrote:
*X> 3^40 `mod` 3 == modexp2 3 40 3 False
*X> fromInteger (3^40 `mod` 3) == modexp2 3 40 3 True
*X> modexp2 3 40 3 0 *X> 3^40 `mod` 3 0
*X> 3^40 `mod` 3 ::Int 2
I'm confused. Last I checked, 0 == 0.
Int overflow is ugly! *X> 3^40 12157665459056928801 *X> maxBound :: Int 9223372036854775807 Claude
Using GHC 7.4.1, and the file x.hs (which has been loaded in ghci) can be found here: http://hpaste.org/69342
I noticed this after prop_sanemodexp was failing.
Any help would be appreciated, - Clark
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Wow, thanks! That was subtle.
- Clark
On Thu, May 31, 2012 at 12:49 PM, Claude Heiland-Allen
Hi Clark,
ghci is defaulting to Integer modexp2 forces Int Int overflows with 3^40
On 31/05/12 17:35, Clark Gaebel wrote:
*X> 3^40 `mod` 3 == modexp2 3 40 3 False
*X> fromInteger (3^40 `mod` 3) == modexp2 3 40 3 True
*X> modexp2 3 40 3
0 *X> 3^40 `mod` 3 0
*X> 3^40 `mod` 3 ::Int
2
I'm confused. Last I checked, 0 == 0.
Int overflow is ugly!
*X> 3^40 12157665459056928801 *X> maxBound :: Int 9223372036854775807
Claude
Using GHC 7.4.1, and the file x.hs (which has been loaded in ghci) can be found here: http://hpaste.org/69342
I noticed this after prop_sanemodexp was failing.
Any help would be appreciated, - Clark
______________________________**_________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe
______________________________**_________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe

Clark Gaebel
*X> 3^40 `mod` 3 == modexp2 3 40 3 False *X> modexp2 3 40 3 0 *X> 3^40 `mod` 3 0
I'm confused. Last I checked, 0 == 0.
This has to do with types: > 3^40 `mod` 3 0 > 3^40 `mod` 3 :: Int 2 When doing number theory always use Integer. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

Hi Clark,
On Thu, May 31, 2012 at 10:35 AM, Clark Gaebel
*X> 3^40 `mod` 3 == modexp2 3 40 3 False *X> modexp2 3 40 3 0 *X> 3^40 `mod` 3 0
I'm confused. Last I checked, 0 == 0.
Your HPaste shows the type of modexp2 is Int -> Int -> Int -> Int, so ghci will infer that (3^40) is an Int. But an Int isn't big enough to hold 3^40. Prelude> 3^40 :: Int -6289078614652622815 Prelude> (3^40 :: Int) `mod` 3 2 Kevin -- Kevin Charter kevin.charter@acm.org

On Thu, May 31, 2012 at 6:35 PM, Clark Gaebel wrote:
*X> 3^40 `mod` 3 == modexp2 3 40 3 False *X> modexp2 3 40 3 0 *X> 3^40 `mod` 3 0
*X> 3^40 `mod` 3 :: Int 1 *X> 3^40 `mod` 3 :: Integer 0 I'm confused. Last I checked, 0 == 0.
Yes, but 3^40 /= 3^40 when you have arithmetic overflow: *X> 3^40 :: Int 689956897 *X> 3^40 :: Integer 12157665459056928801 Regards, Sean

The cafe is certainly responsive today! Thanks everyone - got it. Integer overflow ;) Regards, - Clark
participants (8)
-
Clark Gaebel
-
Claude Heiland-Allen
-
Ertugrul Söylemez
-
Kevin Charter
-
Lorenzo Bolla
-
Sean Leather
-
Simon Hengel
-
wren ng thornton