ligo/gitlab-pages/docs/language-basics/math-numbers-tez.md

334 lines
7.1 KiB
Markdown
Raw Normal View History

---
id: math-numbers-tez
title: Math, Numbers & Tez
---
LIGO offers three built-in numerical types: `int`, `nat` and
`tez`. Values of type `int` are integers; values of type `nat` are
natural numbers (integral numbers greater than or equal to zero);
values of type `tez` are units of measure of Tezos tokens.
* Integer literals are the same found in mainstream programming
languages, for example, `10`, `-6` and `0`, but there is only one
canonical zero: `0` (so, for instance, `-0` and `00` are invalid).
* Natural numbers are written as digits follwed 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.
* Tezos tokens can be specified using literals of three kinds:
* units of millionth of `tez`, using the suffix `mutez` after a
natural literal, like `10000mutez` or `0mutez`;
* units of `tez`, using the suffix `tz` or `tez`, like `3tz` or
`3tez`;
* decimal amounts of `tz` or `tez`, like `12.3tz` or `12.4tez`.
Note that large integral values can be expressed using underscores to
separate groups of digits, like `1_000mutez` or `0.000_004tez`.
## Addition
2020-02-05 19:28:40 +04:00
Addition in LIGO is accomplished by means of the `+` infix
2020-02-06 14:47:41 +04:00
operator. Some type constraints apply, for example you cannot add a
2020-02-05 19:28:40 +04:00
value of type `tez` to a value of type `nat`.
2020-02-05 19:28:40 +04:00
In the following example you can find a series of arithmetic
operations, including various numerical types. However, some bits
2020-02-06 14:47:41 +04:00
remain in comments as they would otherwise not compile, for example,
adding a value of type `int` to a value of type `tez` is invalid. Note
that adding an integer to a natural number produces an integer.
<!--DOCUSAURUS_CODE_TABS-->
2020-02-10 22:07:20 +04:00
<!--PascaLIGO-->
2019-12-26 16:29:22 +04:00
```pascaligo group=a
2020-02-05 19:28:40 +04:00
// int + int yields int
const a : int = 5 + 10
// nat + int yields int
const b : int = 5n + 10
// tez + tez yields tez
const c : tez = 5mutez + 0.000_010tez
2020-02-05 19:28:40 +04:00
//tez + int or tez + nat is invalid
// const d : tez = 5mutez + 10n
// two nats yield a nat
const e : nat = 5n + 10n
// nat + int yields an int: invalid
// const f : nat = 5n + 10;
const g : int = 1_000_000
```
> Pro tip: you can use underscores for readability when defining large
> numbers:
>
>```pascaligo
2020-02-05 19:28:40 +04:00
> const sum : tez = 100_000mutez
>```
<!--CameLIGO-->
2019-12-26 16:29:22 +04:00
```cameligo group=a
2020-02-05 19:28:40 +04:00
// int + int yields int
let a : int = 5 + 10
// nat + int yields int
let b : int = 5n + 10
// tez + tez yields tez
let c : tez = 5mutez + 0.000_010tez
2020-02-05 19:28:40 +04:00
// tez + int or tez + nat is invalid
2020-02-12 01:29:12 +04:00
// let d : tez = 5mutez + 10n
2020-02-05 19:28:40 +04:00
// two nats yield a nat
let e : nat = 5n + 10n
// nat + int yields an int: invalid
2020-02-12 01:29:12 +04:00
// let f : nat = 5n + 10
2020-02-05 19:28:40 +04:00
let g : int = 1_000_000
```
> Pro tip: you can use underscores for readability when defining large
> numbers:
>
>```cameligo
2020-02-05 19:28:40 +04:00
>let sum : tez = 100_000mutez
>```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 16:29:22 +04:00
```reasonligo group=a
2020-02-05 19:28:40 +04:00
// int + int yields int
let a : int = 5 + 10;
// nat + int yields int
let b : int = 5n + 10;
// tez + tez yields tez
let c : tez = 5mutez + 0.000_010tez;
2020-02-05 19:28:40 +04:00
// tez + int or tez + nat is invalid:
// let d : tez = 5mutez + 10n;
// two nats yield a nat
let e : nat = 5n + 10n;
// nat + int yields an int: invalid
2020-02-10 22:07:20 +04:00
// let f : nat = 5n + 10;
2020-02-05 19:28:40 +04:00
let g : int = 1_000_000;
```
> Pro tip: you can use underscores for readability when defining large
> numbers:
2019-12-10 17:47:31 +04:00
>```reasonligo
2020-02-05 19:28:40 +04:00
>let sum : tex = 100_000mutez;
2019-12-10 17:47:31 +04:00
>```
<!--END_DOCUSAURUS_CODE_TABS-->
## Subtraction
2020-02-06 14:47:41 +04:00
Subtraction looks as follows.
> ⚠️ Even when subtracting two `nats`, the result is an `int`
<!--DOCUSAURUS_CODE_TABS-->
2020-02-10 22:07:20 +04:00
<!--PascaLIGO-->
2019-12-26 16:29:22 +04:00
```pascaligo group=b
2020-02-05 19:28:40 +04:00
const a : int = 5 - 10
// Subtraction of two nats yields an int
const b : int = 5n - 2n
// Therefore the following is invalid
// const c : nat = 5n - 2n
const d : tez = 5mutez - 1mutez
```
<!--CameLIGO-->
2019-12-26 16:29:22 +04:00
```cameligo group=b
2020-02-05 19:28:40 +04:00
let a : int = 5 - 10
// Subtraction of two nats yields an int
let b : int = 5n - 2n
// Therefore the following is invalid
2020-02-12 01:29:12 +04:00
// let c : nat = 5n - 2n
2020-02-05 19:28:40 +04:00
let d : tez = 5mutez - 1mutez
```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 16:29:22 +04:00
```reasonligo group=b
2020-02-05 19:28:40 +04:00
let a : int = 5 - 10;
// Subtraction of two nats yields an int
let b : int = 5n - 2n;
// Therefore the following is invalid
// let c : nat = 5n - 2n;
let d : tez = 5mutez - 1mutez;
2019-12-10 17:47:31 +04:00
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Multiplication
You can multiply values of the same type, such as:
<!--DOCUSAURUS_CODE_TABS-->
2020-02-10 22:07:20 +04:00
<!--PascaLIGO-->
2019-12-26 16:29:22 +04:00
```pascaligo group=c
2020-02-05 19:28:40 +04:00
const a : int = 5 * 5
const b : nat = 5n * 5n
2020-02-12 01:29:12 +04:00
// You can also multiply `nat` and `tez`
const c : tez = 5n * 5mutez
```
<!--CameLIGO-->
2019-12-26 16:29:22 +04:00
```cameligo group=c
2020-02-05 19:28:40 +04:00
let a : int = 5 * 5
let b : nat = 5n * 5n
2020-02-12 01:29:12 +04:00
// You can also multiply `nat` and `tez`
2020-02-05 19:28:40 +04:00
let c : tez = 5n * 5mutez
```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 16:29:22 +04:00
```reasonligo group=c
2020-02-05 19:28:40 +04:00
let a : int = 5 * 5;
let b : nat = 5n * 5n;
2020-02-12 01:29:12 +04:00
// You can also multiply `nat` and `tez`
2020-02-05 19:28:40 +04:00
let c : tez = 5n * 5mutez;
2019-12-10 17:47:31 +04:00
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Euclidean Division
2020-02-05 19:28:40 +04:00
In LIGO you can divide `int`, `nat`, and `tez`. Here is how:
> ⚠️ Division of two `tez` values results into a `nat`
<!--DOCUSAURUS_CODE_TABS-->
2020-02-10 22:07:20 +04:00
<!--PascaLIGO-->
2019-12-26 16:29:22 +04:00
```pascaligo group=d
2020-02-05 19:28:40 +04:00
const a : int = 10 / 3
const b : nat = 10n / 3n
const c : nat = 10mutez / 3mutez
```
<!--CameLIGO-->
2019-12-26 16:29:22 +04:00
```cameligo group=d
2020-02-05 19:28:40 +04:00
let a : int = 10 / 3
let b : nat = 10n / 3n
let c : nat = 10mutez / 3mutez
```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 16:29:22 +04:00
```reasonligo group=d
2020-02-05 19:28:40 +04:00
let a : int = 10 / 3;
let b : nat = 10n / 3n;
let c : nat = 10mutez / 3mutez;
2019-12-10 17:47:31 +04:00
```
<!--END_DOCUSAURUS_CODE_TABS-->
LIGO also allows you to compute the remainder of the Euclidean
division. In LIGO, it is a natural number.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo group=d
const a : int = 120
const b : int = 9
const rem1 : nat = a mod b // 3
const c : nat = 120n
const rem2 : nat = c mod b // 3
const d : nat = 9n
const rem3 : nat = c mod d // 3
const rem4 : nat = a mod d // 3
```
<!--CameLIGO-->
```cameligo group=d
let a : int = 120
let b : int = 9
let rem1 : nat = a mod b // 3
let c : nat = 120n
let rem2 : nat = c mod b // 3
let d : nat = 9n
let rem3 : nat = c mod d // 3
let rem4 : nat = a mod d // 3
```
<!--ReasonLIGO-->
```reasonligo group=d
let a : int = 120;
let b : int = 9;
let rem1 : nat = a mod b; // 3
let c : nat = 120n;
let rem2 : nat = c mod b; // 3
let d : nat = 9n;
let rem3 : nat = c mod d; // 3
let rem4 : nat = a mod d; // 3
```
## From `int` to `nat` and back
2020-02-05 19:28:40 +04:00
You can *cast* an `int` to a `nat` and vice versa. Here is how:
<!--DOCUSAURUS_CODE_TABS-->
2020-02-05 19:28:40 +04:00
<!--PascaLIGO-->
2019-12-26 16:29:22 +04:00
```pascaligo group=e
2020-02-05 19:28:40 +04:00
const a : int = int (1n)
const b : nat = abs (1)
```
<!--CameLIGO-->
```cameligo group=e
let a : int = int (1n)
let b : nat = abs (1)
```
<!--ReasonLIGO-->
```reasonligo group=e
let a : int = int (1n);
let b : nat = abs (1);
```
2020-01-29 20:15:03 +04:00
<!--END_DOCUSAURUS_CODE_TABS-->
2020-02-10 22:07:20 +04:00
## Checking a `nat`
2020-01-29 20:15:03 +04:00
2020-02-10 22:07:20 +04:00
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
2020-02-05 19:28:40 +04:00
number, and not otherwise.
2020-01-29 20:15:03 +04:00
<!--DOCUSAURUS_CODE_TABS-->
2020-02-05 19:28:40 +04:00
<!--PascaLIGO-->
```pascaligo group=e
const is_a_nat : option (nat) = is_nat (1)
```
<!--CameLIGO-->
```cameligo group=e
let is_a_nat : nat option = Michelson.is_nat (1)
2020-01-29 20:15:03 +04:00
```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 16:29:22 +04:00
```reasonligo group=e
2020-02-05 19:28:40 +04:00
let is_a_nat : option (nat) = Michelson.is_nat (1);
2019-12-11 13:34:08 +04:00
```
2020-02-05 19:28:40 +04:00
<!--END_DOCUSAURUS_CODE_TABS-->