package reason-standard

  1. Overview
  2. Docs

Arbitrary precision integers.

type t

Arbitrary precision integers.

Backed by BigInt when targeting Javascript and Zarith when targetting native.

Create

val ofInt : int -> t

Create an Integer from an Int

val ofInt64 : Int64.t -> t

Create an Integer from an Int64

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)

Constants

val zero : t
val one : t

Operators

val add : t -> t -> t

Add two Integers.

Examples

Integer.(add (ofInt 3002) (ofInt 4004) = ofInt 7006)

Or using the operator:

Integer.((ofInt 3002) + (ofInt 4004) = (ofInt 7006))
val (+) : t -> t -> t
val subtract : t -> t -> t

Subtract numbers

Examples

Integer.(subtract one one = zero)

Alternatively the operator can be used:

Integer.((ofInt 4) - (ofInt 3) = one)
val (-) : t -> t -> t
val multiply : t -> t -> t

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)
val (*) : t -> t -> t
val divide : t -> by:t -> t

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))
val (/) : t -> t -> t
val power : ?modulo:t -> base:t -> exponent:int -> t

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

Examples

Integer.(negate (ofInt 8) = ofInt (-8))
Integer.(negate (ofInt (-7)) = ofInt 7)
Integer.(negate zero = zero)
val absolute : t -> t

Get the absolute value of a number.

Examples

Integer.(
  assert (absolute 8 = 8);
  assert (absolute (-7) = 7);
  assert (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 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)
)
val (mod) : t -> t -> t
val remainder : t -> by:t -> t

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)
)
val maximum : t -> t -> t

Returns the larger of two Integerss

Examples

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

Returns the smaller of two Integerss

Examples

Integer.(minimum (ofInt 7) (ofInt 9) = (ofInt 7))
Integer.(minimum (ofInt -4) (ofInt -1) = (ofInt -4))

Query

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)
val clamp : t -> lower:t -> upper:t -> t

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))
val inRange : t -> lower:t -> upper:t -> bool

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)

Convert

val toInt : t -> int option

Convert an Integer to an Int

Returns None when greater than Int.maximumValue or less than Int.minimumValue

Examples

Integer.(ofInt 4 |> toInt) = Some 4
String.repeat "9" ~times:10_000 
|> Integer.ofString 
|> Option.flatMap ~f:Integer.toString
   = None
val toInt64 : t -> Int64.t 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

Convert an Integer to a 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

Compare

val equal : t -> t -> bool

Test two Integers for equality

val compare : t -> t -> int

Compare two Integers

type identity

The unique identity for ints Comparator

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