package tablecloth-native

  1. Overview
  2. Docs

The platform-dependant signed integer type. An int is a whole number. Valid syntax for ints includes:

0
42
9000
1_000_000
1_000_000
0xFF (* 255 in hexadecimal *)
0x000A (* 10 in hexadecimal *)

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

When targeting native OCaml uses 31-bits on a 32-bit platforms and 63-bits on a 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.

You can read about the reasons for OCamls unusual integer sizes here.

When targeting JavaScript, that range is -2 ** 53 to 2 ** 53 - 1.

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

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

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.

type t = int

Constants

val zero : t

The literal 0 as a named value

val one : t

The literal 1 as a named value

val maximumValue : t

The maximum representable int on the current platform

val maximum_value : t
val minimumValue : t

The minimum representable int on the current platform

val minimum_value : t

Operators

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

val add : t -> t -> t

Add two Int numbers.

Int.add 3002 4004 = 7006

Or using the globally available operator:

3002 + 4004 = 7006

You cannot add an int and a float directly though.

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

val (+) : t -> t -> t
val subtract : t -> t -> t

Subtract numbers

Int.subtract 4 3 = 1

Alternatively the operator can be used:

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

Multiply ints like

Int.multiply 2 7 = 14

Alternatively the operator can be used:

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

Integer division:

Int.divide 3 ~by:2 = 1
27 / 5 = 5

Notice that the remainder is discarded.

Throws Division_by_zero when the by (the divisor) is 0.

val (/) : t -> t -> t
val (//) : t -> t -> float

Floating point division

3 // 2 = 1.5
27 // 5 = 5.25
8 // 4 = 2.0
val power : base:t -> exponent:t -> t

Exponentiation, takes the base first, then the exponent.

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

Alternatively the ** operator can be used:

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

Flips the 'sign' of an integer so that positive integers become negative and negative integers become positive. Zero stays as it is.

Int.negate 8 = (-8)
Int.negate (-7) = 7
Int.negate 0 = 0

Alternatively the operator can be used:

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

Get the absolute value of a number.

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

Perform modular arithmetic.

If you intend to use modulo to detect even and odd numbers consider using Int.isEven or Int.isOdd.

Int.modulo ~by:2 0 = 0
Int.modulo ~by:2 1 = 1
Int.modulo ~by:2 2 = 0
Int.modulo ~by:2 3 = 1

Our modulo function works in the typical mathematical way when you run into negative numbers:

List.map ~f:(Int.modulo ~by:4) [(-5); (-4); -3; -2; -1;  0;  1;  2;  3;  4;  5 ] =
  [3; 0; 1; 2; 3; 0; 1; 2; 3; 0; 1]

Use Int.remainder for a different treatment of negative numbers.

val remainder : t -> by:t -> t

Get the remainder after division. Here are bunch of examples of dividing by four:

List.map ~f:(Int.remainder ~by:4) [(-5); (-4); (-3); (-2); (-1); 0; 1; 2; 3; 4; 5] =
  [(-1); 0; (-3); (-2); (-1); 0; 1; 2; 3; 0; 1]

Use Int.modulo for a different treatment of negative numbers.

val maximum : t -> t -> t

Returns the larger of two ints

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

Returns the smaller of two ints

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

Checks

val isEven : t -> bool

Check if an int is even

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

Check if an int is odd

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

Clamps n within the inclusive lower and upper bounds.

Int.clamp ~lower:0 ~upper:8 5 = 5
Int.clamp ~lower:0 ~upper:8 9 = 8
Int.clamp ~lower:(-10) ~upper:(-5) 5 = (-5)

Throws an Invalid_argument exception if lower > upper

val inRange : t -> lower:t -> upper:t -> bool

Checks if n is between lower and up to, but not including, upper.

Int.inRange ~lower:2 ~upper:4 3 = true
Int.inRange ~lower:5 ~upper:8 4 = false
Int.inRange ~lower:(-6) ~upper:(-2) (-3) = true

Throws an Invalid_argument exception if lower > upper

val in_range : t -> lower:t -> upper:t -> bool

Conversion

val toFloat : t -> float

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

let halfOf (number : int) : float =
  Float.((Int.toFloat number) / 2)

halfOf 7 = 3.5

Note that locally opening the Float module here allows us to use the floating point division operator

val to_float : t -> float
val toString : t -> string

Convert an int into a string representation.

Int.toString 3 = "3"
Int.toString (-3) = "-3"
Int.toString 0 = "0"

Guarantees that

Int.(fromString (toString n)) = Some n 
val to_string : t -> string
val fromString : string -> t option

Attempt to parse a string into a int.

Int.fromString "0" = Some 0.
Int.fromString "42" = Some 42.
Int.fromString "-3" = Some (-3)
Int.fromString "123_456" = Some 123_456
Int.fromString "0xFF" = Some 255
Int.fromString "0x00A" = Some 10
Int.fromString "Infinity" = None
Int.fromString "NaN" = None
val from_string : string -> t option
OCaml

Innovation. Community. Security.