Page
Library
Module
Module type
Parameter
Class
Class type
Source
Standard.IntFixed precision integers
Attempt to parse a string into a int.
Examples
Int.ofString "42" = Some 42.Int.ofString "-3" = Some (-3)Int.ofString "123_456" = Some 123_456Int.ofString "0xFF" = Some 255Int.ofString "Infinity" = NoneInt.ofString "NaN" = NoneNote You do not need to open the Int module to use the (+), (-), (*), (**), (mod) or (/) operators, these are available as soon as you open Standard
See Int.add
Subtract numbers
Examples
Int.subtract 4 3 = 1Alternatively the operator can be used:
4 - 3 = 1See Int.subtract
Multiply ints
Examples
Int.multiply 2 7 = 14Alternatively the operator can be used:
(2 * 7) = 14See Int.multiply
Integer division
Notice that the remainder is discarded.
Exceptions
Throws Division_by_zero when by is 0.
Examples
Int.divide 3 ~by:2 = 127 / 5 = 5See Int.divide
Floating point division
Examples
Int.(3 /. 2) = 1.5Int.(27 /. 5) = 5.25Int.(8 /. 4) = 2.0Exponentiation, takes the base first, then the exponent.
Examples
Int.power ~base:7 ~exponent:3 = 343Alternatively the ** operator can be used:
7 ** 3 = 343See Int.power
Flips the 'sign' of an int so that positive ints become negative and negative ints become positive. Zero stays as it is.
Examples
Int.negate 8 = (-8)Int.negate (-7) = 7Int.negate 0 = 0Alternatively the ~- operator can be used:
~-(7) = (-7)See Int.negate
Get the absolute value of a number.
Examples
Int.absolute 8 = 8Int.absolute (-7) = 7Int.absolute 0 = 0Perform modular arithmetic.
If you intend to use modulo to detect even and odd numbers consider using Int.isEven or Int.isOdd.
The modulo function works in the typical mathematical way when you run into negative numbers
Use Int.remainder for a different treatment of negative numbers.
Exceptions
Throws Division_by_zero when by is 0.
Examples
Int.modulo ~by:3 (-4) = 1Int.modulo ~by:3 (-3) = 0Int.modulo ~by:3 (-2) = 1Int.modulo ~by:3 (-1) = 2Int.modulo ~by:3 0 = 0Int.modulo ~by:3 1 = 2Int.modulo ~by:3 2 = 1Int.modulo ~by:3 3 = 0Int.modulo ~by:3 4 = 1See Int.modulo
Get the remainder after division. Here are bunch of examples of dividing by four:
Use Int.modulo for a different treatment of negative numbers.
Exceptions
Throws Division_by_zero when by is 0.
Examples
Int.remainder ~by:3 (-4) = 1Int.remainder ~by:3 (-3) = 0Int.remainder ~by:3 (-2) = 2Int.remainder ~by:3 (-1) = 1Int.remainder ~by:3 0 = 0Int.remainder ~by:3 1 = 1Int.remainder ~by:3 2 = 2Int.remainder ~by:3 3 = 0Int.remainder ~by:3 4 = 1Returns the larger of two ints
Examples
Int.maximum 7 9 = 9Int.maximum (-4) (-1) = (-1)Returns the smaller of two ints
Examples
Int.minimum 7 9 = 7Int.minimum (-4) (-1) = (-4)Check if an int is even
Examples
Int.isEven 8 = trueInt.isEven 7 = falseInt.isEven 0 = trueCheck if an int is odd
Examples
Int.isOdd 7 = trueInt.isOdd 8 = falseInt.isOdd 0 = falseClamps n within the inclusive lower and upper bounds.
Exceptions
Throws an Invalid_argument exception if lower > upper
Examples
Int.clamp ~lower:0 ~upper:8 5 = 5Int.clamp ~lower:0 ~upper:8 9 = 8Int.clamp ~lower:(-10) ~upper:(-5) 5 = (-5)Checks if n is between lower and up to, but not including, upper.
Exceptions
Throws an Invalid_argument exception if lower > upper
Examples
Int.inRange ~lower:2 ~upper:4 3 = trueInt.inRange ~lower:5 ~upper:8 4 = falseInt.inRange ~lower:(-6) ~upper:(-2) (-3) = trueConvert an int into a string representation.
Guarantees that
Int.(ofString (toString n)) = Some n Examples
Int.toString 3 = "3"Int.toString (-3) = "-3"Int.to_sString 0 = "0"The unique identity for ints Comparator