2019-11-08 03:19:27 +04:00
---
id: math-numbers-tez
title: Math, Numbers & Tez
---
LIGO offers three built-in numerical types: `int` , `nat` and `tez` .
## Addition
Addition in ligo is acomplished by using the `+` operator. Some type constraints apply; for example you can't add `tez + nat` .
In the following example you can find a series of arithmetic operations, including various numerical types. However, some bits of the example won't compile because adding an `int` to a `nat` produces an `int` , not a `nat` . Similiar rules apply for `tez` :
<!-- DOCUSAURUS_CODE_TABS -->
<!-- Pascaligo -->
2019-12-26 16:29:22 +04:00
```pascaligo group=a
2019-11-08 03:19:27 +04:00
// int + int produces int
const a: int = 5 + 10;
// nat + int produces int
const b: int = 5n + 10;
// tez + tez produces tez
const c: tez = 5mutez + 10mutez;
// you can't add tez + int or tez + nat, this won't compile
// const d: tez = 5mutez + 10n;
// two nats produce a nat
const e: nat = 5n + 10n;
// nat + int produces an int, this won't compile
// const f: nat = 5n + 10;
const g: int = 1_000_000;
```
> Pro tip: you can use underscores for readability when defining large numbers
>
>```pascaligo
>const g: int = 1_000_000;
>```
2019-12-12 17:35:07 +04:00
<!-- CameLIGO -->
2019-11-08 03:19:27 +04:00
2019-12-26 16:29:22 +04:00
```cameligo group=a
2019-11-08 03:19:27 +04:00
// int + int produces int
let a: int = 5 + 10
// nat + int produces int
let b: int = 5n + 10
// tez + tez produces tez
let c: tez = 5mutez + 10mutez
// you can't add tez + int or tez + nat, this won't compile
// const d: tez = 5mutez + 10n
// two nats produce a nat
let e: nat = 5n + 10n
// nat + int produces an int, this won't compile
// const f: nat = 5n + 10
let g: int = 1_000_000
```
> Pro tip: you can use underscores for readability when defining large numbers
>
>```cameligo
>let g: int = 1_000_000;
>```
2019-12-11 13:34:08 +04:00
<!-- ReasonLIGO -->
2019-12-10 17:47:31 +04:00
2019-12-26 16:29:22 +04:00
```reasonligo group=a
(* int + int produces int *)
2019-12-10 17:47:31 +04:00
let a: int = 5 + 10;
2019-12-26 16:29:22 +04:00
(* nat + int produces int *)
2019-12-10 17:47:31 +04:00
let b: int = 5n + 10;
2019-12-26 16:29:22 +04:00
(* tez + tez produces tez *)
2019-12-10 17:47:31 +04:00
let c: tez = 5mutez + 10mutez;
2019-12-26 16:29:22 +04:00
(* you can't add tez + int or tez + nat, this won't compile:
let d: tez = 5mutez + 10n; *)
(* two nats produce a nat *)
2019-12-10 17:47:31 +04:00
let e: nat = 5n + 10n;
2019-12-26 16:29:22 +04:00
(* nat + int produces an int, this won't compile:
let f: nat = 5n + 10; *)
2019-12-10 17:47:31 +04:00
let g: int = 1_000_000;
```
> Pro tip: you can use underscores for readability when defining large numbers
>
>```reasonligo
>let g: int = 1_000_000;
>```
2019-11-08 03:19:27 +04:00
<!-- END_DOCUSAURUS_CODE_TABS -->
## Subtraction
The simpliest substraction looks like this:
> ⚠️ Even when subtracting two `nats`, the result is an `int`
<!-- DOCUSAURUS_CODE_TABS -->
<!-- Pascaligo -->
2019-12-26 16:29:22 +04:00
```pascaligo group=b
2019-11-08 03:19:27 +04:00
const a: int = 5 - 10;
// substraction of two nats, yields an int
const b: int = 5n - 2n;
// won't compile, result is an int, not a nat
// const c: nat = 5n - 2n;
2019-12-26 16:29:22 +04:00
const d: tez = 5mutez - 1mutez;
2019-11-08 03:19:27 +04:00
```
2019-12-12 17:35:07 +04:00
<!-- CameLIGO -->
2019-12-26 16:29:22 +04:00
```cameligo group=b
2019-11-08 03:19:27 +04:00
let a: int = 5 - 10
// substraction of two nats, yields an int
let b: int = 5n - 2n
// won't compile, result is an int, not a nat
// const c: nat = 5n - 2n
2019-12-26 16:29:22 +04:00
let d: tez = 5mutez - 1mutez
2019-11-08 03:19:27 +04:00
```
2019-12-11 13:34:08 +04:00
<!-- ReasonLIGO -->
2019-12-26 16:29:22 +04:00
```reasonligo group=b
2019-12-10 17:47:31 +04:00
let a: int = 5 - 10;
2019-12-26 16:29:22 +04:00
(* substraction of two nats, yields an int *)
2019-12-10 17:47:31 +04:00
let b: int = 5n - 2n;
2019-12-26 16:29:22 +04:00
(* won't compile, result is an int, not a nat *)
(* let c: nat = 5n - 2n; *)
let d: tez = 5mutez - 1mutez;
2019-12-10 17:47:31 +04:00
```
2019-11-08 03:19:27 +04:00
<!-- END_DOCUSAURUS_CODE_TABS -->
## Multiplication
You can multiply values of the same type, such as:
<!-- DOCUSAURUS_CODE_TABS -->
<!-- Pascaligo -->
2019-12-26 16:29:22 +04:00
```pascaligo group=c
2019-11-08 03:19:27 +04:00
const a: int = 5 * 5;
const b: nat = 5n * 5n;
// you can also multiply `nat` and `tez`
const c: tez = 5n * 5mutez;
```
2019-12-12 17:35:07 +04:00
<!-- CameLIGO -->
2019-12-26 16:29:22 +04:00
```cameligo group=c
2019-11-08 03:19:27 +04:00
let a: int = 5 * 5
let b: nat = 5n * 5n
// you can also multiply `nat` and `tez`
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
2019-12-10 17:47:31 +04:00
let a: int = 5 * 5;
let b: nat = 5n * 5n;
2019-12-26 16:29:22 +04:00
(* you can also multiply `nat` and `tez` *)
2019-12-10 17:47:31 +04:00
let c: tez = 5n * 5mutez;
```
2019-11-08 03:19:27 +04:00
<!-- END_DOCUSAURUS_CODE_TABS -->
## Division
In LIGO you can divide `int` , `nat` , and `tez` . Here's how:
> ⚠️ Division of two `tez` values results into a `nat`
<!-- DOCUSAURUS_CODE_TABS -->
<!-- Pascaligo -->
2019-12-26 16:29:22 +04:00
```pascaligo group=d
2019-11-08 03:19:27 +04:00
const a: int = 10 / 3;
const b: nat = 10n / 3n;
const c: nat = 10mutez / 3mutez;
```
2019-12-12 17:35:07 +04:00
<!-- CameLIGO -->
2019-12-26 16:29:22 +04:00
```cameligo group=d
2019-11-08 03:19:27 +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
2019-12-10 17:47:31 +04:00
let a: int = 10 / 3;
let b: nat = 10n / 3n;
let c: nat = 10mutez / 3mutez;
```
2019-11-08 03:19:27 +04:00
<!-- END_DOCUSAURUS_CODE_TABS -->
## From `int` to `nat` and back
You can *cast* an `int` to a `nat` and vice versa, here's how:
<!-- DOCUSAURUS_CODE_TABS -->
<!-- Pascaligo -->
2019-12-26 16:29:22 +04:00
```pascaligo group=e
2019-11-08 03:19:27 +04:00
const a: int = int(1n);
const b: nat = abs(1);
```
2019-12-11 13:34:08 +04:00
<!-- ReasonLIGO -->
2019-12-26 16:29:22 +04:00
```reasonligo group=e
2019-12-11 13:34:08 +04:00
let a: int = int(1n);
let b: nat = abs(1);
```
2019-11-08 03:19:27 +04:00
<!-- END_DOCUSAURUS_CODE_TABS -->