Library
Module
Module type
Parameter
Class
Class type
Arbitrary precision integers.
val ofFloat : float -> t option
Create an Integer
from a Float
.
Returns None
when called with Float.nan
, Float.infinity
or Float.negativeInfinity
val ofString : string -> t option
Attempt to parse a String
into a Integer
.
Examples
Integer.(ofString "0" = Some (ofInt 0))
Integer.(ofString "42" = Some (ofInt 42))
Integer.(ofString "-3" = Some (ofInt (-3)))
Integer.(ofString "123_456" = Some (ofInt 123_456))
Integer.(ofString "0xFF" = Some (ofInt 255))
Integer.(ofString "0x00A" = Some (ofInt 10))
Integer.(ofString "Infinity" = None)
Integer.(ofString "NaN" = None)
val zero : t
val one : t
Add two Integer
s.
Examples
Integer.(add (ofInt 3002) (ofInt 4004) = ofInt 7006)
Or using the operator:
Integer.((ofInt 3002) + (ofInt 4004) = (ofInt 7006))
See Integer.add
Subtract numbers
Examples
Integer.(subtract one one = zero)
Alternatively the operator can be used:
Integer.((ofInt 4) - (ofInt 3) = one)
See Integer.subtract
Multiply two integers
Examples
Integer.(multiply (ofInt 2) (ofInt 7) = (ofInt 14))
Alternatively the operator can be used:
Integer.((ofInt 2) * (ofInt 7) = ofInt 14)
See Integer.multiply
Integer division
Notice that the remainder is discarded.
Exceptions
Throws Division_by_zero
when the divisor is zero
.
Examples
Integer.(divide (ofInt 3) ~by:(ofInt 2) = (ofInt 1))
Integer.((ofInt 27) / (ofInt 5) = (ofInt 5))
See Integer.divide
Exponentiation, takes the base first, then the exponent.
Alternatively the **
operator can be used.
Examples
Integer.(
power ~base:(ofInt 7) ~exponent:3 ~modulo:(ofInt 300) = ofInt 43
)
Integer.(
(ofInt 7) ** 4 = ofInt 2401
)
See Integer.power
Flips the 'sign' of an integer so that positive integers become negative and negative integers become positive. Zero stays as it is.
Examples
Integer.(negate (ofInt 8) = ofInt (-8))
Integer.(negate (ofInt (-7)) = ofInt 7)
Integer.(negate zero = zero)
Get the absolute value of a number.
Examples
Integer.(
assert (absolute 8 = 8);
assert (absolute (-7) = 7);
assert (absolute 0 = 0);
)
Perform modular arithmetic.
If you intend to use modulo
to detect even and odd numbers consider using Integer.isEven
or Integer.isOdd
.
Our modulo
function works in the typical mathematical way when you run into negative numbers
Use Integer.remainder
for a different treatment of negative numbers.
Examples
Integer.(
let three = ofInt 3 in
let two = ofInt 2 in
assert (modulo three ~by:three = zero);
assert (modulo two ~by:three = two);
assert (modulo one ~by:three = one);
assert (modulo zero ~by:three = zero);
assert (modulo (negate one) ~by:three = one);
assert (modulo (negate two) ~by:three = two);
assert (modulo (negate three) ~by:three = zero)
)
See Integer.modulo
Get the remainder after division. Here are bunch of examples of dividing by four:
Use Integer.modulo
for a different treatment of negative numbers.
Examples
Integer.(
let three = ofInt 3 in
let two = ofInt 2 in
assert (remainder three ~by:three = zero);
assert (remainder two ~by:three = two);
assert (remainder one ~by:three = one);
assert (remainder zero ~by:three = zero);
assert (remainder (negate one) ~by:three = (negate one));
assert (remainder (negate two) ~by:three = (negate two));
assert (remainder (negate three) ~by:three = zero)
)
Returns the larger of two Integers
s
Examples
Integer.(maximum (ofInt 7) (ofInt 9) = (ofInt 9))
Integer.(maximum (ofInt -4) (ofInt -1) = (ofInt -1))
Returns the smaller of two Integers
s
Examples
Integer.(minimum (ofInt 7) (ofInt 9) = (ofInt 7))
Integer.(minimum (ofInt -4) (ofInt -1) = (ofInt -4))
val isEven : t -> bool
Check if an int
is even
Examples
Integer.(isEven (ofInt 8)) = true
Integer.(isEven (ofInt 7)) = false
Integer.(isEven (ofInt 0)) = true
val isOdd : t -> bool
Check if an int
is odd
Examples
Integer.(isOdd (ofInt 7) = true)
Integer.(isOdd (ofInt 8) = false)
Integer.(isOdd (ofInt 0) = false)
Clamps an integer within the inclusive lower
and upper
bounds.
Exceptions
Throws an Invalid_argument
exception if lower > upper
Examples
Integer.(clamp ~lower:zero ~upper:(ofInt 8) (ofInt 5) = (ofInt 5))
Integer.(clamp ~lower:zero ~upper:(ofInt 8) (ofInt 9) = (ofInt 8))
Integer.(clamp ~lower:(ofInt -10) ~upper:(ofInt -5) (ofInt 5) = (ofInt -5))
Checks if an integer is between lower
and up to, but not including, upper
.
Exceptions
Throws an Invalid_argument
exception if lower > upper
Examples
Integer.(inRange ~lower:(ofInt 2) ~upper:(ofInt 4) (ofInt 3) = true)
Integer.(inRange ~lower:(ofInt 5) ~upper:(ofInt 8) (ofInt 4) = false)
Integer.(inRange ~lower:(ofInt -6) ~upper:(ofInt -2) (ofInt -3) = true)
val toInt : t -> int option
Convert an Integer
to an Int64.t
Returns None
when greater than Int64.max_int
or less than Int64.min_int
Examples
Integer.ofInt 1 |> Integer.toInt64 = Some Int64.one
String.repeat "9" ~times:10_000
|> Integer.ofString
|> Option.flatMap ~f:Integer.toString
= None
val toFloat : t -> float
Returns Float.infinity
when greater than Float.largestValue
.
Examples
Integer.ofString "8" |> Integer.toFloat = 8.0
String.repeat "9" ~times:10_000
|> Integer.ofString
|> Option.map ~f:Integer.toFloat
= Some Float.infinity
val toString : t -> string
Gives a human-readable, decimal string representation
The unique identity for ints
Comparator