Larger numbers become negative in haskell

(product (take 9 [1000,999..])) `div` (product (map (\j-> product [1..j]) (map (\j->length(j)) (group [2,2,2,2,2,2,2,2,2]))))

The above code has the form X divY, s X=product (take 9 [1000,999..])andY=(product (map (\j-> product [1..j]) (map (\j->length(j)) (group [2,2,2,2,2,2,2,2,2]))))

If I copy and paste the code into ghci, it gives me -12740133310672as an answer, but if I count X & Yindividually, I get 964541486381834014456320000 & 362880, and then dividing them gives me 2658017764500203964000as an answer.

I think that this incoherence may be due to the fact that the number is too large, but since the computer can calculate X and Y correctly individually, how can it also not separate them together?

+4
source share
2 answers
Prelude Data.List> let x = product $ take 9 [1000,999..]

Prelude Data.List> x
964541486381834014456320000
Prelude Data.List> :t x
x :: (Enum a, Num a) => a

, x : , . GHCi, "" , Integer, , . , , :

Prelude Data.List> x :: Integer
964541486381834014456320000
Prelude Data.List> x :: Float
9.645414e26
Prelude Data.List> x :: Double
9.645414863818342e26
Prelude Data.List> x :: Int
-4623139575776374784

, , Int ( ) .

, Integer.

, - .

Prelude Data.List> let y = product (map (\j-> product [1..j]) $ map length (group [2,2,2,2,2,2,2,2,2]))

Prelude Data.List> y
362880
Prelude Data.List> :t y
y :: Int

x, y : Int, length. (: , , Int, .)

div, Haskell, , . x`div`y Int Integer, x. , , x Int .

OTOH, 362880 , length. , x, GHCi Integer,

Prelude Data.List> x `div` 362880
2658017764500203964000

, y:

Prelude Data.List> x `div` fromIntegral y
2658017764500203964000
+10

Integer Int :

let r :: Integer ; r =  (product (take 9 [1000,999..])) `div` (product (map (\j-> product [1..j]) (map (\j->fromIntegral $ length(j)) (group [2,2,2,2,2,2,2,2,2]))))

: 2658017764500203964000

, , , - Int .

, - - (Int , ( ). , , , Integer, .

0

All Articles