Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Haskell provide min/max constants for the range of Int?

Does Haskell provide any constants for knowing the limits of Int? I understand Int is platform-dependent, but nevertheless I would like to utilize it and to initialize some values at the extremes in my particular case. The equivalent constants (for instance) in C would be INT_MAX and INT_MIN.

like image 796
DuckMaestro Avatar asked Jan 17 '13 07:01

DuckMaestro


People also ask

What is the difference between int and integer in Haskell?

What's the difference between Integer and Int ? Integer can represent arbitrarily large integers, up to using all of the storage on your machine. Int can only represent integers in a finite range.

Is int max value?

Being a signed data type, it can store positive values as well as negative values. Takes a size of 32 bits where 1 bit is used to store the sign of the integer. A maximum integer value that can be stored in an int data type is typically 2, 147, 483, 647, around 231 – 1, but is compiler dependent.


1 Answers

The maximum and minimum bounds for different types are accessed via the Bounded type-class using the values minBound and maxBound.

The values are polymorphic based on the context they are in, so in some cases you might have to explicitly indicate the type if the compiler is unable to infer it. E.g.

x = minBound :: Int 
like image 141
shang Avatar answered Sep 23 '22 08:09

shang