Strange Behavior (^)

I am working on a puzzle that requires an exponent function, so I define

exp' :: Int -> Int -> Int
exp' = (^)

It is strange here:

*Main> exp' 6 25

-8463200117489401856

But

*Main> 6^25

+28430288029929701376

I could not find the difference between mine exp'and (^)from ghci.

Is an error ghc?

Glasgow Haskell's glorious compilation system, version 8.0.1

+4
source share
1 answer

I could not find the difference

Yes, there are some differences.

:t exp'
exp' :: Int -> Int -> Int

:t (^)
(^) :: (Num a, Integral b) => a -> b -> a

namely

:t (^)
--                           Num    Integral    Num
(^) :: (Num a, Integral b) => a  ->     b    ->  a

Cm? This is about type.

Simply put, it is Intlimited, so it can overflow to a negative value when the allowable range is exceeded:

> (6::Int) ^ (25::Int)

-8463200117489401856

But it is Integerunlimited, therefore it is not overflowed:

> (6::Integer) ^ (25::Integer)

+28430288029929701376

, , Int Integer:

exp' :: Integer -> Integer -> Integer
exp' = (^)

https://www.haskell.org/tutorial/numbers.html .

+14

Source: https://habr.com/ru/post/1659595/


All Articles