Natural numbers with infinite precision.
:::note
Most operations on integer numbers (e.g. addition) are available as built-in operators (e.g. 1 + 1).
This module provides equivalent functions and Text conversion.
:::
:::info Function form for higher-order use
Several arithmetic and comparison functions (e.g. add, sub, equal, less, pow) are defined in this module to enable their use as first-class function values, which is not possible with operators like +, -, ==, etc., in Motoko. This allows you to pass these operations to higher-order functions such as map, foldLeft, or sort.
:::
Import from the base library to use this module.
motoko name=import
import Nat "mo:base/Nat";Infinite precision natural numbers.
public func toText(n : Nat) : TextConverts a natural number to its textual representation. Textual representation does not contain underscores to represent commas.
Example:
motoko include=import
Nat.toText 1234 // => "1234"public func fromText(text : Text) : ?NatCreates a natural number from its textual representation. Returns null
if the input is not a valid natural number.
:::note The textual representation must not contain underscores. ::: Example:
motoko include=import
Nat.fromText "1234" // => ?1234public func min(x : Nat, y : Nat) : NatReturns the minimum of x and y.
Example:
motoko include=import
Nat.min(1, 2) // => 1public func max(x : Nat, y : Nat) : NatReturns the maximum of x and y.
Example:
motoko include=import
Nat.max(1, 2) // => 2public func equal(x : Nat, y : Nat) : BoolEquality function for Nat types.
This is equivalent to x == y.
Example:
motoko include=import
ignore Nat.equal(1, 1); // => true
1 == 1 // => true
Example:
motoko include=import
import Buffer "mo:base/Buffer";
let buffer1 = Buffer.Buffer<Nat>(3);
let buffer2 = Buffer.Buffer<Nat>(3);
Buffer.equal(buffer1, buffer2, Nat.equal) // => truepublic func notEqual(x : Nat, y : Nat) : BoolInequality function for Nat types.
This is equivalent to x != y.
Example:
motoko include=import
ignore Nat.notEqual(1, 2); // => true
1 != 2 // => truepublic func less(x : Nat, y : Nat) : Bool"Less than" function for Nat types.
This is equivalent to x < y.
Example:
motoko include=import
ignore Nat.less(1, 2); // => true
1 < 2 // => truepublic func lessOrEqual(x : Nat, y : Nat) : Bool"Less than or equal" function for Nat types.
This is equivalent to x <= y.
Example:
motoko include=import
ignore Nat.lessOrEqual(1, 2); // => true
1 <= 2 // => truepublic func greater(x : Nat, y : Nat) : Bool"Greater than" function for Nat types.
This is equivalent to x > y.
Example:
motoko include=import
ignore Nat.greater(2, 1); // => true
2 > 1 // => truepublic func greaterOrEqual(x : Nat, y : Nat) : Bool"Greater than or equal" function for Nat types.
This is equivalent to x >= y.
Example:
motoko include=import
ignore Nat.greaterOrEqual(2, 1); // => true
2 >= 1 // => truepublic func compare(x : Nat, y : Nat) : {#less; #equal; #greater}General purpose comparison function for Nat. Returns the Order (
either #less, #equal, or #greater) of comparing x with y.
Example:
motoko include=import
Nat.compare(2, 3) // => #less
This function can be used as value for a high order function, such as a sort function.
Example:
motoko include=import
import Array "mo:base/Array";
Array.sort([2, 3, 1], Nat.compare) // => [1, 2, 3]public func add(x : Nat, y : Nat) : NatReturns the sum of x and y, x + y. This operator will never overflow
because Nat is infinite precision.
Example:
motoko include=import
ignore Nat.add(1, 2); // => 3
1 + 2 // => 3
Example:
motoko include=import
import Array "mo:base/Array";
Array.foldLeft([2, 3, 1], 0, Nat.add) // => 6public func sub(x : Nat, y : Nat) : NatReturns the difference of x and y, x - y.
Traps on underflow below 0.
Example:
motoko include=import
ignore Nat.sub(2, 1); // => 1
// Add a type annotation to avoid a warning about the subtraction
2 - 1 : Nat // => 1
Example:
motoko include=import
import Array "mo:base/Array";
Array.foldLeft([2, 3, 1], 10, Nat.sub) // => 4public func mul(x : Nat, y : Nat) : NatReturns the product of x and y, x * y. This operator will never
overflow because Nat is infinite precision.
Example:
motoko include=import
ignore Nat.mul(2, 3); // => 6
2 * 3 // => 6
Example:
motoko include=import
import Array "mo:base/Array";
Array.foldLeft([2, 3, 1], 1, Nat.mul) // => 6public func div(x : Nat, y : Nat) : NatReturns the unsigned integer division of x by y, x / y.
Traps when y is zero.
The quotient is rounded down, which is equivalent to truncating the decimal places of the quotient.
Example:
motoko include=import
ignore Nat.div(6, 2); // => 3
6 / 2 // => 3public func rem(x : Nat, y : Nat) : NatReturns the remainder of unsigned integer division of x by y, x % y.
Traps when y is zero.
Example:
motoko include=import
ignore Nat.rem(6, 4); // => 2
6 % 4 // => 2public func pow(x : Nat, y : Nat) : NatReturns x to the power of y, x ** y. Traps when y > 2^32. This operator
will never overflow because Nat is infinite precision.
Example:
motoko include=import
ignore Nat.pow(2, 3); // => 8
2 ** 3 // => 8