Math, Numbers & Mav
LIGO offers three built-in numerical types: int
, nat
and
mav
. Values of type int
are integers; values of type nat
are
natural numbers (integral numbers greater than or equal to zero);
values of type mav
are units of measure of Mavryk tokens.
Integer literals are the same found in mainstream programming languages, for example,
10
,-6
and0
, but there is only one canonical zero:0
(so, for instance,-0
and00
are invalid).Natural numbers are written as digits followed by the suffix
n
, like so:12n
,0n
, and the same restriction on zero as integers applies:0n
is the only way to specify the natural zero.Mavryk tokens can be specified using literals of three kinds:
- units of millionth of
mav
, using the suffixmumav
after a natural literal, like10000mumav
or0mumav
; - units of
mav
, using the suffixtz
ormav
, like3mv
or3mav
; - decimal amounts of
tz
ormav
, like12.3mv
or12.4mav
.
- units of millionth of
Note that large integral values can be expressed using underscores to
separate groups of digits, like 1_000mumav
or 0.000_004mav
.
Addition
Addition in LIGO is accomplished by means of the +
infix
operator. Some type constraints apply, for example you cannot add a
value of type mav
to a value of type nat
.
In the following example you can find a series of arithmetic
operations, including various numerical types. However, some bits
remain in comments as they would otherwise not compile, for example,
adding a value of type int
to a value of type mav
is invalid. Note
that adding an integer to a natural number produces an integer.
Tip: you can use underscores for readability when defining large numbers:
let sum : mav = 100_000mumav;
Subtraction
Subtraction looks as follows.
⚠️ Even when subtracting two
nats
, the result is anint
.
From protocol Ithaca
onwards subtracting values of type mav
yeilds an optional value (due to the Michelson instruction
SUB_MUMAV
)
Multiplication
You can multiply values of the same type, such as:
Euclidean Division
In LIGO you can divide int
, nat
, and mav
. Here is how:
⚠️ Division of two
mav
values results into anat
.
LIGO also allows you to compute the remainder of the Euclidean division. In LIGO, it is a natural number.
The behaviour of the
%
operator in JsLIGO is different from JavaScript. In JsLIGO,%
is a modulus operator and in JavaScript it's a remainder operator. In the case of positive numbers everything is the same, but not with negative numbers.
For cases when you need both the quotient and the remainder, LIGO provides the
ediv
operation. ediv x y
returns Some (quotient, remainder)
, unless y
is zero, in which case it returns None
From int
to nat
and back
You can cast an int
to a nat
and vice versa. Here is how:
Checking a nat
You can check if a value is a nat
by using a predefined cast
function which accepts an int
and returns an optional nat
: if the
result is not None
, then the provided integer was indeed a natural
number, and not otherwise.
Increment operator
Increment opeator increments (adds one to) the value of the binder.
In the prefix position (++p
) the operator increments the value and returns
the latest incremented value.
In the postfix position (p++
) the operator increments the value but
returns the old value before the increment.
Decrement operator
Decrement opeator decrements (subtracts one from) the value of the binder.
In the prefix position (--p
) the operator decrements the value and returns
the latest decremented value.
In the postfix position (p--
) the operator decrements the value but
returns the old value before the decrement.