Skip to content

Integers

This content is not available in your language yet.

Arithmetic in smart contracts on TON is always done with integers and never with floating-point numbers since the floats are unpredictable. Therefore, the big accent goes on integers and their handling.

The only primitive number type in Tact is Int, for 257257-bit signed integers.
It’s capable of storing integers between 2256-2^{256} and 22561.2^{256} - 1.

Notation

Tact supports various ways of writing primitive values of Int as integer literals.

Most of the notations allow adding underscores (_) in-between digits, except for:

  • Representations in strings, as seen in nano-tons case.
  • Decimal numbers written with a leading zero 0.0. Their use is generally discouraged, see below.

Additionally, several underscores in a row as in 4__24\_\_2, or trailing underscores as in 42_42\_ are not allowed.

Decimal

Most common and most used way of representing numbers, using the decimal numeral system: 123456789.123456789.
You can use underscores (_) to improve readability: 123_456_789123\_456\_789 is equal to 123456789.123456789.

Hexadecimal

Represent numbers using hexadecimal numeral system, denoted by the 0x\mathrm{0x} (or 0X\mathrm{0X}) prefix: 0xFFFFFFFFF.\mathrm{0xFFFFFFFFF}.
Use underscores (_) to improve readability: 0xFFF_FFF_FFF\mathrm{0xFFF\_FFF\_FFF} is equal to 0xFFFFFFFFF.\mathrm{0xFFFFFFFFF}.

Octal

Represent numbers using octal numeral system, denoted by the 0o\mathrm{0o} (or 0O\mathrm{0O}) prefix: 0o777777777.\mathrm{0o777777777.}
Use underscores (_) to improve readability: 0o777_777_777\mathrm{0o777\_777\_777} is equal to 0o777777777.\mathrm{0o777777777}.

Binary

Represent numbers using binary numeral system, denoted by the 0b\mathrm{0b} (or 0B\mathrm{0B}) prefix: 0b111111111.\mathrm{0b111111111.}
Use underscores (_) to improve readability: 0b111_111_111\mathrm{0b111\_111\_111} is equal to 0b111111111.\mathrm{0b111111111}.

NanoToncoin

Arithmetic with dollars requires two decimal places after the dot — those are used for the cents value. But how would we represent the number $1.251.25 if we’re only able to work with integers? The solution is to work with cents directly. This way, $1.251.25 becomes 125125 cents. We simply memorize that the two rightmost digits represent the numbers after the decimal point.

Similarly, working with Toncoin, the main currency of TON Blockchain, requires nine decimal places instead of the two. One can say that nanoToncoin is the 1109th\frac{1}{10^{9}}\mathrm{th} of the Toncoin.

Therefore, the amount of 1.251.25 Toncoin, which can be represented in Tact as ton("1.25"), is actually the number 12500000001250000000. We refer to such numbers as nanoToncoin(s) (or nano-ton(s)) rather than cents.

Serialization

When encoding Int values to persistent state (fields of contracts and traits), it’s usually better to use smaller representations than 257257-bits to reduce storage costs. Usage of such representations is also called “serialization” due to them representing the native TL-B types which TON Blockchain operates on.

The persistent state size is specified in every declaration of a state variable after the as keyword:

contract SerializationExample {
// persistent state variables
oneByte: Int as int8 = 0; // ranges from -128 to 127 (takes 8 bit = 1 byte)
twoBytes: Int as int16; // ranges from -32,768 to 32,767 (takes 16 bit = 2 bytes)
init() {
// needs to be initialized in the init() because it doesn't have the default value
self.twoBytes = 55*55;
}
}

Integer serialization is also available for the fields of Structs and Messages, as well as in key/value types of maps:

struct StSerialization {
martin: Int as int8;
}
message MsgSerialization {
seamus: Int as int8;
mcFly: map<Int as int8, Int as int8>;
}

Motivation is very simple:

  • Storing 10001000 257257-bit integers in state costs about 0.1840.184 TON per year.
  • Storing 10001000 3232-bit integers only costs 0.0230.023 TON per year by comparison.

Serialization types

NameTL-BInclusive rangeSpace taken
uint8uint800 to 2812^{8} - 188 bits = 11 byte
uint16uint1600 to 21612^{16} - 11616 bits = 22 bytes
uint32uint3200 to 23212^{32} - 13232 bits = 44 bytes
uint64uint6400 to 26412^{64} - 16464 bits = 88 bytes
uint128uint12800 to 212812^{128} - 1128128 bits = 1616 bytes
uint256uint25600 to 225612^{256} - 1256256 bits = 3232 bytes
int8int827-2^{7} to 2712^{7} - 188 bits = 11 byte
int16int16215-2^{15} to 21512^{15} - 11616 bits = 22 bytes
int32int32231-2^{31} to 23112^{31} - 13232 bits = 44 bytes
int64int64263-2^{63} to 26312^{63} - 16464 bits = 88 bytes
int128int1282127-2^{127} to 212712^{127} - 1128128 bits = 1616 bytes
int256int2562255-2^{255} to 225512^{255} - 1256256 bits = 3232 bytes
int257int2572256-2^{256} to 225612^{256} - 1257257 bits = 3232 bytes + 11 bit
coinsVarUInteger 1600 to 212012^{120} - 1between 44 and 124124 bits, see below

Variable coins type

In Tact, coins is an alias to VarUInteger 16 in TL-B representation, i.e. it takes a variable bit length depending on the optimal number of bytes needed to store the given integer and is commonly used for storing nanoToncoin amounts.

This serialization format consists of two TL-B fields:

  • len, a 44-bit unsigned big-endian integer storing the byte length of the value provided
  • value, a 8len8 * len-bit unsigned big-endian representation of the value provided

That is, integers serialized as coins occupy between 44 and 124124 bits (44 bits for len and 00 to 1515 bytes for value) and have values in the inclusive range from 00 to 212012^{120} - 1.

Examples:

struct Scrooge {
// len: 0000, 4 bits (always)
// value: none!
// in total: 4 bits
a: Int as coins = 0; // 0000
// len: 0001, 4 bits
// value: 00000001, 8 bits
// in total: 12 bits
b: Int as coins = 1; // 0001 00000001
// len: 0010, 4 bits
// value: 00000001 00000010, 16 bits
// in total: 20 bits
c: Int as coins = 258; // 0010 00000001 00000010
// len: 1111, 4 bits
// value: hundred twenty 1's in binary
// in total: 124 bits
d: Int as coins = pow(2, 120) - 1; // hundred twenty 1's in binary
}

Operations

All runtime calculations with numbers are done at 257-bits, so overflows are quite rare. Nevertheless, if any math operation overflows, an exception will be thrown, and the transaction will fail. You could say that Tact’s math is safe by default.

Note, that there is no problem with mixing variables of different state sizes in the same calculation. At runtime they are all the same type no matter what — 257257-bit signed, so overflows won’t happen then.

However, this can still lead to errors in the compute phase of the transaction. Consider the following example:

import "@stdlib/deploy";
contract ComputeErrorsOhNo with Deployable {
oneByte: Int as uint8; // persistent state variable, max value is 255
init() {
self.oneByte = 255; // initial value is 255, everything fits
}
receive("lets break it") {
let tmp: Int = self.oneByte * 256; // no runtime overflow
self.oneByte = tmp; // whoops, tmp value is out of the expected range of oneByte
}
}

Here, oneByte is serialized as a uint8, which occupies only one byte and ranges from 00 to 2812^{8} - 1, which is 255255. And when used in runtime calculations no overflow happens and everything is calculated as a 257257-bit signed integers. But the very moment we decide to store the value of tmp back into oneByte we get an error with the exit code 5, which states the following: Integer out of the expected range.