Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abs and minBound

Tags:

math

haskell

When playing around with minBound for various instances, I tried

> minBound :: Int
=> -9223372036854775808

which seems reasonable.

However,

> abs minBound :: Int 
=> -9223372036854775808

How is this possible? Shouldn't abs x >= 0, assuming x :: Int?

Is there some sort of understanding of semantics I am missing here?

like image 219
Eliza Brandt Avatar asked May 12 '26 15:05

Eliza Brandt


1 Answers

Prelude> minBound :: Int
-9223372036854775808
Prelude> maxBound :: Int
9223372036854775807

Note that the number 9223372036854775808 is not representable as an integer. This is a consequence of the two's complement representation likely being used in the background here. The absolute value of minBound cannot be represented as a member of the type Int, so you end up getting an overflowing value.

like image 82
Silvio Mayolo Avatar answered May 15 '26 10:05

Silvio Mayolo