Numbers

View source

Integer types, literals, arithmetic that stops on overflow, wrapping and saturating operators, and conversions with as, and the order of operators.

Actus works with whole numbers. This page shows the integer types, how to write number literals, what happens when a result does not fit, and how to change a number from one type to another.

Integer types

Every integer has a type. The type says how many bits the number uses and whether it can be negative.

Int is the default whole-number type. A number you write without a type, such as 5, is an Int unless the context says otherwise. Use Int for counters, exit statuses and small calculations. Use a fixed width such as u8 or u32 when the width is part of the job, for example for a byte in a file, a field of a message or a hardware register.

Other widths from 1 to 64 bits also exist, such as u16, u33 and i40, and they work like the common ones. Names up to u128 and i128 are accepted, but values wider than 64 bits are not supported yet: a literal that needs more than 64 bits does not compile, and println(int: ...) refuses such a type (see below). This page uses the common types.

actus
verb main() {
    erg small: i8 = -100i8;
    erg byte: u8 = 255u8;
    erg medium: i16 = 30000i16;
    erg plain: Int = 2147483647;
    erg index: Usize = 3;
    println(int: small);
    println(int: byte);
    println(int: medium);
    println(int: plain);
    println(int: index);
}
bash
actus run
text
-100
255
30000
2147483647
3
process exited with status 0

println(int: ...) takes an integer of any type up to 64 bits, so the program above prints a u8, an i16 and a Usize directly. A type that is wider than 64 bits has no way to be printed yet. The compiler refuses it and tells you to convert it first:

actus
verb main() {
    erg wide: i128 = 5i128;
    println(int: wide);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1139] at 3:18: `println(int:)` prints integers up to 64 bits; `i128` is wider, cast it to a 64-bit type first

You will find out more about as in Convert with as.

Number literals

A literal is a number written in the code. Add a suffix to give it a type directly: 200u8 is a u8, 5i64 is an i64. Without a suffix, a literal takes the type the context asks for, if the value fits. Hexadecimal literals start with 0x or 0X, and they can take a suffix too, as in 0xFFu8.

actus
verb main() {
    erg with_suffix = 200u8;
    erg from_context: u8 = 200;
    erg hex: Int = 0xFF;
    erg hex_byte = 0xFFu8;
    erg upper: Int = 0X1F;
    println(int: with_suffix);
    println(int: from_context);
    println(int: hex);
    println(int: hex_byte);
    println(int: upper);
}
bash
actus run
text
200
200
255
255
31
process exited with status 0

A literal that does not fit its type is an error before the program runs:

actus
verb main() {
    erg level: u8 = 300;
    println(int: level);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1068] at 2:21: literal `300` is outside the range of `u8`

Arithmetic

The usual operators work on integers: +, -, *, / and % for the remainder. A minus sign in front of a number negates it. Division of integers drops the fraction.

actus
verb main() {
    erg a = 7;
    erg b = 2;
    println(int: a + b);
    println(int: a - b);
    println(int: a * b);
    println(int: a / b);
    println(int: a % b);
    println(int: -a);
}
bash
actus run
text
9
5
14
3
1
-7
process exited with status 0

7 / 2 is 3 and 7 % 2 is 1.

Both sides need the same type

Both sides of an arithmetic operator must have the same integer type, and the result has that type. This holds for +, -, *, /, %, for the wrapping and saturating forms you will meet below, and for &, | and ^. Int, i32 and u32 are three different types, so mixing them is an error:

actus
verb main() {
    erg small: u8 = 10u8;
    erg wide: u32 = 1000u32;
    erg total = small + wide;
    println(int: total);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1140] at 4:25: operands of `+` must have the same integer type, found `u8` and `u32`; convert one side explicitly with `as`

The message points at the right operand and names the fix: convert one side with as (see Convert with as). A number you write without a suffix is the exception. It takes the type of the other side, as long as the value fits:

actus
verb main() {
    erg small: u8 = 10u8;
    erg wide: u32 = 1000u32;
    erg total: u32 = (small as u32) + wide;
    erg next: u8 = small + 1;
    erg first: u8 = 1 + small;
    println(int: total);
    println(int: next);
    println(int: first);
}
bash
actus run
text
1010
11
11
process exited with status 0

Here small as u32 makes both sides u32. The plain 1 becomes a u8 in small + 1 because small is a u8. A suffixed literal such as 4u32 and a cast keep their own type, so they do not adapt. Comparisons follow the same rule, as you will see below.

Overflow stops the program

A number type has a largest and a smallest value. If a calculation goes past them, Actus does not quietly wrap around and give you a wrong number. The program stops with an error:

actus
verb main() {
    erg count: Int = 2147483647;
    println(string: "adding 1 to the largest Int");
    count = count + 1;
    println(string: "done");
}
bash
actus run
text
adding 1 to the largest Int
error: integer overflow at src/main.act:4:13
process exited with status 101

The message names the problem and the place: file, line and column. The first line was printed, then the program stopped at the addition, so done never appears. The status 101 is the status Actus uses when a runtime check fails. You saw the same in First program.

This applies to +, - and *, to negation and to +=, -= and *=. It applies to unsigned types too. A u8 cannot go below 0:

actus
verb main() {
    erg have: u8 = 5u8;
    erg need: u8 = 10u8;
    println(string: "taking 10 from 5");
    erg left = have - need;
    println(int: left);
}
bash
actus run
text
taking 10 from 5
error: integer overflow at src/main.act:5:16
process exited with status 101

Division and remainder by zero stop the program in the same way. So does dividing the smallest value of a signed type by -1, because the true answer does not fit in the type:

actus
verb main() {
    erg total = 100;
    erg parts = 0;
    println(string: "dividing");
    println(int: total / parts);
}
bash
actus run
text
dividing
error: division by zero at src/main.act:5:18
process exited with status 101
actus
verb main() {
    erg smallest: Int = -2147483647 - 1;
    erg minus_one: Int = -1;
    println(int: smallest % minus_one);
    println(int: smallest / minus_one);
}
bash
actus run
text
0
error: integer overflow at src/main.act:5:18
process exited with status 101

The remainder of that pair is 0 and never stops the program. The division stops it.

When the compiler can already see the problem in the code, it refuses the program before it runs. The message also names the way out:

actus
verb main() {
    println(int: 2147483647 + 1);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1138] at 2:18: constant `+` overflows `Int`; use `+%` to wrap or `+|` to saturate

Wrap or saturate on purpose

Sometimes wrapping around is what you want, for example in a checksum or a counter that starts again at zero. Sometimes you want a value to stop at the largest or smallest number. You ask for these with different operators.

There are also +%=, -%= and *%= for changing a variable in place.

actus
verb main() {
    erg top: u8 = 250u8;
    erg low: u8 = 5u8;
    erg tiny: i8 = 120i8;
    println(int: top +% 10u8);
    println(int: top +| 10u8);
    println(int: low -% 10u8);
    println(int: low -| 10u8);
    println(int: tiny *% 2i8);
    println(int: tiny *| 2i8);
    top +%= 10u8;
    println(int: top);
}
bash
actus run
text
4
255
251
0
-16
127
4
process exited with status 0

Read the output line by line:

  • 250 +% 10 wraps to 4, and 250 +| 10 stops at 255.
  • 5 -% 10 wraps down to 251, and 5 -| 10 stops at 0.
  • For the signed i8, 120 *% 2 wraps to -16, and 120 *| 2 stops at 127.
  • The last line is top +%= 10u8, which changes top in place to 4.

Because the plain operators always check, you can read a plain + as "this must fit". When you see +% or +|, the author chose wrapping or clamping on purpose.

Convert with as

Actus does not turn one integer type into another by itself. Putting a u8 into an Int variable is an error:

actus
verb main() {
    erg small: u8 = 10u8;
    erg wide: Int = small;
    println(int: wide);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1027] at 3:5: type mismatch for `wide`: expected `Int`, found `u8`

Say what you mean with as:

actus
verb main() {
    erg small: u8 = 10u8;
    erg wide: Int = small as Int;
    erg big: i64 = wide as i64;
    erg product: i64 = big * 400000000i64;
    println(int: wide);
    println(int: product / 1000000000i64);
}
bash
actus run
text
10
4
process exited with status 0

Here the u8 becomes an Int, then an i64. The i64 holds 10 * 400000000, which is too large for an Int. The program then divides the result and prints it. Note that big * 400000000i64 has the same type on both sides.

Comparisons need two integers of the same type as well. This is the same rule as for arithmetic, and the same error code. Convert one side with as:

actus
verb main() {
    erg signed: i8 = -5i8;
    erg unsigned: u8 = 5u8;
    println(bool: signed < unsigned);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1140] at 4:28: operands of `<` must have the same integer type, found `i8` and `u8`; convert one side explicitly with `as`
actus
verb main() {
    erg signed: i8 = -5i8;
    erg unsigned: u8 = 5u8;
    println(bool: signed < (unsigned as i8));
    println(bool: signed == -5i8);
}
bash
actus run
text
true
true
process exited with status 0

Casts are checked

An as that would change the value is not allowed. The compiler checks the range of the target type. When the value is only known while the program runs, the check also happens then, and the program stops:

actus
verb main() {
    erg value: Int = 300;
    println(string: "casting 300 to u8");
    erg narrow: u8 = value as u8;
    println(int: narrow);
}
bash
actus run
text
casting 300 to u8
error: integer value out of range at src/main.act:4:22
process exited with status 101
actus
verb main() {
    erg value: Int = -1;
    println(string: "casting -1 to u32");
    erg unsigned: u32 = value as u32;
    println(int: unsigned);
}
bash
actus run
text
casting -1 to u32
error: integer value out of range at src/main.act:4:25
process exited with status 101

300 does not fit in a u8, and -1 does not fit in an unsigned type. Actus reports integer value out of range instead of cutting the number.

A minus sign and as

A minus sign in front of a value, and as, can meet in one expression, as in -1 as u8. The rule is simple: the minus sign is applied first, and as comes after it. So -1 as u8 means (-1) as u8, and -x as i64 means (-x) as i64. The same holds for the other prefix operators: -%, ! and ~.

For a number written in the code this has a useful result. -128 is one number, and it fits in an i8, so this works:

actus
verb main() {
    erg lowest: i8 = -128 as i8;
    println(int: lowest);
}
bash
actus run
text
-128
process exited with status 0

But -1 is below zero, and a u8 cannot hold it. The compiler sees this before the program runs:

actus
verb main() {
    erg wrong: u8 = -1 as u8;
    println(int: wrong);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1091] at 2:21: literal `-1` is outside the range of `u8` in a cast

With a variable the order matters too. -x as i64 negates x in its own type first, and only then makes an i64. For the smallest i8, that first step does not fit, so the program stops:

actus
verb main() {
    erg lowest: i8 = -128i8;
    println(string: "negating -128 as an i8");
    erg wrong: i64 = -lowest as i64;
    println(int: wrong);
}
bash
actus run
text
negating -128 as an i8
error: integer overflow at src/main.act:4:22
process exited with status 101

If you want the cast first and the minus sign afterwards, say so with parentheses: -(x as i64). Now the value is an i64 before it is negated, and there is room for 128:

actus
verb main() {
    erg lowest: i8 = -128 as i8;
    erg negated: i64 = -(lowest as i64);
    println(int: lowest);
    println(int: negated);
}
bash
actus run
text
-128
128
process exited with status 0

When you are not sure which comes first, add parentheses. They always win.

Which operator goes first

When one line has several operators, Actus works from the tightest to the loosest. Operators on the same level go from left to right.

  1. Things written after a value: .field, [index] and ?.
  2. A prefix operator: -, -%, ! and ~.
  3. as.
  4. *, *%, *|, / and %.
  5. +, -, +%, -%, +| and -|.
  6. << and >>, then &, then ^, then |.
  7. The comparisons <, <=, > and >=, then == and !=.
  8. &&, then ||.

So as binds tighter than any operator with two sides. You can write a cast inside a sum without parentheses:

actus
verb main() {
    erg small: u8 = 10u8;
    erg big: u32 = 1000u32;
    erg total: u32 = small as u32 + big;
    println(int: total);
}
bash
actus run
text
1010
process exited with status 0

Here small as u32 is worked out first and then added to big.

Decimal numbers

The type list of the language reference also names f32 and f64, for decimal numbers, with the note that they are for places where the backend and the contract support them. The programs in this guide use whole numbers only. In 0.3.0 you can store a float in a variable, compare two floats of the same width, and put a minus sign in front of one, as in -x. Nothing else works yet. For example, +, -, * and / on an f64 are rejected, and println has no float form. Do not build on them yet.

What you learned

  • Int is a 32-bit signed integer. Use i8 to i64, u8 to u64 and Usize when you need another size or sign. Odd widths up to 64 bits, such as u33, work too.
  • println(int: ...) prints any integer type up to 64 bits.
  • A suffix such as 200u8 gives a literal its type. Hexadecimal literals take one too, as in 0xFFu8.
  • Both sides of an arithmetic or comparison operator need the same integer type. An unsuffixed literal adapts. Anything else needs as.
  • +, - and * stop the program when the result does not fit. So do division by zero, unsigned results below zero, and the smallest value divided by -1.
  • +% wraps around and +| saturates, when you want that.
  • as converts between integer types, and it checks the range.
  • A prefix minus comes before as: -128 as i8 works, -1 as u8 is an error before the program runs, and -(x as i64) casts first.

Next: Conditions and loops.