package reason-standard

  1. Overview
  2. Docs

The platform-dependant signed integer type.

An int is a whole number.

ints are subject to overflow, meaning that Int.maximumValue + 1 = Int.minimumValue.

If you need to work with integers larger than maximumValue (or smaller than minimumValue you can use the Integer module.

Valid syntax for ints includes:

42
(-7)
1_000_000
0xFF (* 255 in hexadecimal *)

Note: The number of bits used for an int is platform dependent.

When targeting Bucklescript an int is 32 bits.

When targeting native OCaml uses 31-bits on 32-bit platforms and 63-bits on 64-bit platforms which means that int math is well-defined in the range -2 ** 30 to 2 ** 30 - 1 for 32bit platforms -2 ** 62 to 2 ** 62 - 1 for 64bit platforms.

Outside of that range, the behavior is determined by the compilation target.

If you need to work with values outside that range, you can use the Integer module.

You can read about the reasons for OCaml's unusual integer sizes here.

Historical Note: The name int comes from the term integer. It appears that the int abbreviation was introduced in the programming language ALGOL 68.

Today, almost all programming languages use this abbreviation.

Fixed precision integers

type t = int

Constants

val zero : int

The literal 0 as a named value

val one : int

The literal 1 as a named value

val maximumValue : int

The maximum representable int on the current platform

val minimumValue : int

The minimum representable int on the current platform

Create

val ofString : string -> int option

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_456
Int.ofString "0xFF" = Some 255
Int.ofString "Infinity" = None
Int.ofString "NaN" = None

Operators

Note You do not need to open the Int module to use the (+), (-), (*), (**), (mod) or (/) operators, these are available as soon as you open Standard

val add : int -> int -> int

Add two Int numbers.

You cannot add an int and a float directly though.

See Float.add for why, and how to overcome this limitation.

Examples

Int.add 3002 4004 = 7006

Or using the globally available operator:

3002 + 4004 = 7006
val (+) : int -> int -> int
val subtract : int -> int -> int

Subtract numbers

Examples

Int.subtract 4 3 = 1

Alternatively the operator can be used:

4 - 3 = 1
val (-) : int -> int -> int
val multiply : int -> int -> int

Multiply ints

Examples

Int.multiply 2 7 = 14

Alternatively the operator can be used:

(2 * 7) = 14
val (*) : int -> int -> int
val divide : int -> by:int -> int

Integer division

Notice that the remainder is discarded.

Exceptions

Throws Division_by_zero when by is 0.

Examples

Int.divide 3 ~by:2 = 1
27 / 5 = 5
val (/) : int -> int -> int
val (/.) : int -> int -> float

Floating point division

Examples

Int.(3 /. 2) = 1.5
Int.(27 /. 5) = 5.25
Int.(8 /. 4) = 2.0
val power : base:int -> exponent:int -> int

Exponentiation, takes the base first, then the exponent.

Examples

Int.power ~base:7 ~exponent:3 = 343

Alternatively the ** operator can be used:

7 ** 3 = 343
val (**) : int -> int -> int
val negate : int -> int

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) = 7
Int.negate 0 = 0

Alternatively the ~- operator can be used:

~-(7) = (-7)
val (~-) : int -> int
val absolute : int -> int

Get the absolute value of a number.

Examples

Int.absolute 8 = 8
Int.absolute (-7) = 7
Int.absolute 0 = 0
val modulo : int -> by:int -> int

Perform 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) = 1
Int.modulo ~by:3 (-3) = 0
Int.modulo ~by:3 (-2) = 1
Int.modulo ~by:3 (-1) = 2
Int.modulo ~by:3 0 = 0
Int.modulo ~by:3 1 = 2
Int.modulo ~by:3 2 = 1
Int.modulo ~by:3 3 = 0
Int.modulo ~by:3 4 = 1
val (mod) : int -> int -> int
val remainder : int -> by:int -> int

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) = 1
Int.remainder ~by:3 (-3) = 0
Int.remainder ~by:3 (-2) = 2
Int.remainder ~by:3 (-1) = 1
Int.remainder ~by:3 0 = 0
Int.remainder ~by:3 1 = 1
Int.remainder ~by:3 2 = 2
Int.remainder ~by:3 3 = 0
Int.remainder ~by:3 4 = 1
val maximum : int -> int -> int

Returns the larger of two ints

Examples

Int.maximum 7 9 = 9
Int.maximum (-4) (-1) = (-1)
val minimum : int -> int -> int

Returns the smaller of two ints

Examples

Int.minimum 7 9 = 7
Int.minimum (-4) (-1) = (-4)

Query

val isEven : int -> bool

Check if an int is even

Examples

Int.isEven 8 = true
Int.isEven 7 = false
Int.isEven 0 = true
val isOdd : int -> bool

Check if an int is odd

Examples

Int.isOdd 7 = true
Int.isOdd 8 = false
Int.isOdd 0 = false
val clamp : int -> lower:int -> upper:int -> int

Clamps 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 = 5
Int.clamp ~lower:0 ~upper:8 9 = 8
Int.clamp ~lower:(-10) ~upper:(-5) 5 = (-5)
val inRange : int -> lower:int -> upper:int -> bool

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 = true
Int.inRange ~lower:5 ~upper:8 4 = false
Int.inRange ~lower:(-6) ~upper:(-2) (-3) = true

Convert

val toFloat : int -> float

Convert an int into a float. Useful when mixing Int and Float values like this:

Examples

let halfOf (number : int) : float =
  Float.((Int.toFloat number) / 2.0)
  (* Note that locally opening the {!Float} module here allows us to use the floating point division operator *) 
in
halfOf 7 = 3.5
val toString : int -> string

Convert 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"

Compare

val equal : int -> int -> bool

Test two ints for equality

val compare : int -> int -> int

Compare two ints

type identity

The unique identity for ints Comparator

val comparator : (t, identity) Standard__.Core.Comparator.t
OCaml

Innovation. Community. Security.