Int structure

August 11, 2026 ยท View on GitHub

Up to index

The Int structure provides arithmetic, comparison, and conversion operations for the default fixed-precision integer type.

Specified by the Standard ML Basis Library.

Synopsis

type int

val toLarge : int -> int
val fromLarge : int -> int
val toInt : int -> int
val fromInt : int -> int
val precision : int option
val minInt : int option
val maxInt : int option
val + : int * int -> int
val - : int * int -> int
val * : int * int -> int
val div : int * int -> int
val mod : int * int -> int
val quot : int * int -> int
val rem : int * int -> int
val compare : int * int -> order
val < : int * int -> bool
val <= : int * int -> bool
val > : int * int -> bool
val >= : int * int -> bool
val ~ : int -> int
val abs : int -> int
val min : int * int -> int
val max : int * int -> int
val sign : int -> int
val sameSign : int * int -> bool
val fmt : radix -> int -> string
val toString : int -> string
val scan : radix -> (char, 'a) reader -> (int, 'a) reader
val fromString : string -> int option

type int

is the type of fixed-precision integers.

toLarge

toLarge i

fromLarge

fromLarge i

toInt

toInt i

fromInt

fromInt i

precision

precision

minInt

minInt is the minimal (most negative) integer representable by int. If a value is NONE, int can represent all negative integers, within the limits of the heap size. If precision is SOME (n), then we have minInt = -2(n-1).

maxInt

maxInt is the maximal (most positive) integer representable by int. If a value is NONE, int can represent all positive integers, within the limits of the heap size. If precision is SOME (n), then we have maxInt = 2(n-1) - 1.

+

i + j is the sum of i and j. It raises Overflow when the result is not representable.

-

i - j is the difference of i and j. It raises Overflow when the result is not representable.

*

i * j is the product of i and j. It raises Overflow when the result is not representable.

div

i div j returns the greatest integer less than or equal to the quotient of i by j, i.e., floor(i / j). It raises Overflow when the result is not representable, or Div when j = 0. Note that rounding is towards negative infinity, not zero.

mod

i mod j returns the remainder of the division of i by j. It raises Div when j = 0. When defined, (i mod j) has the same sign as j, and (i div j) * j + (i mod j) = i.

quot

quot (i, j) (or i.quot j) returns the truncated quotient of the division of i by j, i.e., it computes (i / j) and then drops any fractional part of the quotient. It raises Overflow when the result is not representable, or Div when j = 0. Note that unlike div, quot rounds towards zero. In addition, unlike div and mod, neither quot nor rem are infix by default; an appropriate infix declaration would be infix 7 quot rem. This is the semantics of most hardware divide instructions, so quot may be faster than div.

rem

rem (i, j) (or i.rem j) returns the remainder of the division of i by j. It raises Div when j = 0. (i rem j) has the same sign as i, and it holds that (i quot j) * j + (i rem j) = i. This is the semantics of most hardware divide instructions, so rem may be faster than mod.

compare

compare (i, j) (or i.compare j) returns LESS, EQUAL, or GREATER according to whether its first argument is less than, equal to, or greater than the second.

<

i < j returns true if i is less than j.

<=

i <= j returns true if i is less than or equal to j.

>

i > j returns true if i is greater than j.

>=

i >= j returns true if i is greater than or equal to j.

~

~ i returns the negation of i.

abs

abs i (or i.abs ()) returns the absolute value of i.

min

min (i, j) (or i.min j) returns the smaller of the arguments.

max

max (i, j) (or i.max j) returns the larger of the arguments.

sign

sign i (or i.sign ())

sameSign

sameSign (i, j) (or i.sameSign j) returns true if i and j have the same sign. It is equivalent to (sign i = sign j).

fmt

fmt radix i returns a string containing a representation of i with #"~" used as the sign for negative numbers. Formats the string according to radix; the hexadecimal digits 10 through 15 are represented as #"A" through #"F", respectively. No prefix "0x" is generated for the hexadecimal representation.

toString

toString i (or i.toString ()) converts a int into a string; equivalent to (fmt StringCvt.DEC r).

scan

scan radix getc strm returns SOME (i,rest) if an integer in the format denoted by radix can be parsed from a prefix of the character stream strm after skipping initial whitespace, where i is the value of the integer parsed and rest is the rest of the character stream. NONE is returned otherwise. This function raises Overflow when an integer can be parsed, but is too large to be represented by type int.

fromString

fromString s scans a int value from a string. Returns SOME (r) if a int value can be scanned from a prefix of s, ignoring any initial whitespace; otherwise, it returns NONE. Equivalent to StringCvt.scanString (scan StringCvt.DEC).