package tablecloth-base

  1. Overview
  2. Docs

Module Tablecloth.IntSource

Fixed precision integers

The platform-dependant signed integer type.

An int is a whole number.

ints are subject to overflow, meaning that Int.maximum_value + 1 = Int.minimum_value.

If you need to work with integers larger than maximum_value (or smaller than minimum_value you can use the Float module.

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.

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.

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.

Sourcetype t = int

Constants

Sourceval zero : t

The literal 0 as a named value.

Sourceval one : t

The literal 1 as a named value.

Sourceval maximum_value : t

The maximum representable int on the current platform.

Sourceval minimum_value : t

The minimum representable int on the current platform.

Create

Sourceval from_string : string -> t option

Attempt to parse a string into a int.

Examples

Int.from_string "0" = Some 0
Int.from_string "42" = Some 42
Int.from_string "-3" = Some (-3)
Int.from_string "123_456" = Some 123_456
Int.from_string "0xFF" = Some 255
Int.from_string "0x00A" = Some 10
Int.from_string "Infinity" = None
Int.from_string "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 Tablecloth

Sourceval 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.

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

Subtract numbers.

Examples

Int.subtract 4 3 = 1

Alternatively the operator can be used:

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

Multiply ints.

Examples

Int.multiply 2 7 = 14

Alternatively the operator can be used:

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

Integer division.

Notice that the remainder is discarded.

Exceptions

Throws Division_by_zero when the divisor is 0.

Examples

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

Floating point division.

Examples

Int.(3 /. 2) = 1.5
Int.(27 /. 5) = 5.25
Int.(8 /. 4) = 2.0
Sourceval divide_float : by:t -> t -> float

Floating point division.

Examples

Int.divide_float 3 ~by:2 = 1.5
Int.divide_float 27 ~by:5 = 5.25
Int.divide_float 8 ~by:4 = 2.0
Sourceval power : base:t -> exponent:t -> t

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
Sourceval (**) : t -> t -> t
Sourceval 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.

Examples

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

Alternatively the ~- operator can be used:

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

Get the absolute value of a number.

Examples

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

Perform modular arithmetic.

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

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.

Examples

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

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

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

Examples

  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]
Sourceval maximum : t -> t -> t

Returns the larger of two ints.

Examples

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

Returns the smaller of two ints.

Examples

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

Query

Sourceval is_even : t -> bool

Check if an int is even.

Examples

Int.is_even 8 = true
Int.is_even 7 = false
Int.is_even 0 = true
Sourceval is_odd : t -> bool

Check if an int is odd.

Examples

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

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

Convert

Sourceval to_float : t -> float

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

Examples

  let half_of (number : int) : float =
    Float.((Int.to_float number) / 2.)
    (* Note that locally opening the {!Float} module here allows us to use the floating point division operator *)
  in
  half_of 7 = 3.5
Sourceval to_string : t -> string

Convert an int into a string representation.

Guarantees that

Int.(from_string (to_string n)) = Some n 

Examples

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

Compare

Sourceval equal : t -> t -> bool

Test two ints for equality.

Sourceval compare : t -> t -> int

Compare two ints.

Sourcetype identity

The unique identity for Comparator.

Sourceval comparator : (t, identity) Base.Comparator.t
OCaml

Innovation. Community. Security.