Nat

The convention is to name the module alias after the file name it is defined in:

import Nat "mo:base/Nat";

Conversion

Function toText

Comparison

Function min
Function max
Function compare
Function equal
Function notEqual
Function less
Function lessOrEqual
Function greater
Function greaterOrEqual

Numerical operations

Function add
Function sub Careful! Traps if result is a negative number
Function mul
Function div
Function rem
Function pow

Nat.toText

func toText(n : Nat) : Text

The function toText takes one Nat argument and returns a Text value.

import Nat "mo:base/Nat";

let natural : Nat = 10;

Nat.toText(natural);

Nat.min

func min(x : Nat, y : Nat) : Nat

The function min takes two Nat arguments and returns the smallest Nat value.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

Nat.min(a, b);

Nat.max

func max(x : Nat, y : Nat) : Nat

The function max takes two Nat arguments and returns the largest Nat value.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

Nat.max(a, b);

Nat.compare

func compare(x : Nat, y : Nat) : {#less; #equal; #greater}

The function compare takes two Nat arguments and returns an Order variant value.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

Nat.compare(a, b);

Nat.equal

func equal(x : Nat, y : Nat) : Bool

The function equal takes two Nat arguments and returns a Bool value. It is equivalent to the == relational operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

let isEqual = Nat.equal(a, b);

let isEqualAgain = a == b;

Nat.notEqual

func notEqual(x : Nat, y : Nat) : Bool

The function notEqual takes two Nat arguments and returns a Bool value. It is equivalent to the != relational operator

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

let isNotEqual = Nat.notEqual(a, b);

let notEqual = a != b;

Nat.less

func less(x : Nat, y : Nat) : Bool

The function less takes two Nat arguments and returns a Bool value. It is equivalent to the < relational operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

let isLess = Nat.less(a, b);

let less = a < b;

Nat.lessOrEqual

func lessOrEqual(x : Nat, y : Nat) : Bool

The function lessOrEqual takes two Nat arguments and returns a Bool value. It is equivalent to the <= relational operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 50;

let isLessOrEqual = Nat.lessOrEqual(a, b);

let lessOrEqual = a <= b;

Nat.greater

func greater(x : Nat, y : Nat) : Bool

The function greater takes two Nat arguments and returns a Bool value. It is equivalent to the > relational operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

let isGreater = Nat.greater(a, b);

let greater = a > b;

Nat.greaterOrEqual

func greaterOrEqual(x : Nat, y : Nat) : Bool

The function greaterOrEqual takes two Nat arguments and returns a Bool value. It is equivalent to the >= relational operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

let isGreaterOrEqual = Nat.greaterOrEqual(a, b);

let greaterOrEqual = a >= b;

Nat.add

func add(x : Nat, y : Nat) : Nat

The function add takes two Nat arguments and returns a Nat value. It is equivalent to the + numeric operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

let add = Nat.add(a, b);

let addition = a + b;

Nat.sub

func sub(x : Nat, y : Nat) : Nat

The function sub takes two Nat arguments and returns a Nat value. It is equivalent to the - numeric operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

let subtract = Nat.sub(a, b);

// Result has type `Int` because subtracting two `Nat` may trap due to undeflow

NOTE
Both the Nat.sub function and the - operator on Nat values may cause a trap if the result is a negative number (underflow).

Nat.mul

func mul(x : Nat, y : Nat) : Nat

The function mul takes two Nat arguments and returns a Nat value. It is equivalent to the * numeric operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 20;

let multiply = Nat.mul(a, b);

let multiplication = a * b;

Nat.div

func div(x : Nat, y : Nat) : Nat

The function div takes two Nat arguments and returns a Nat value. It is equivalent to the / numeric operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 50;

let divide = Nat.div(a, b);

Nat.rem

func rem(x : Nat, y : Nat) : Nat

The function rem takes two Nat arguments and returns a Nat value. It is equivalent to the % numeric operator.

import Nat "mo:base/Nat";

let a : Nat = 50;
let b : Nat = 50;

let remainder = Nat.rem(a, b);

let remains = a % b;

Nat.pow

func pow(x : Nat, y : Nat) : Nat

The function pow takes two Nat arguments and returns a Nat value. It is equivalent to the ** numeric operator.

import Nat "mo:base/Nat";

let a : Nat = 5;
let b : Nat = 5;

let power = Nat.pow(a, b);

let pow = a ** b;