Numbers
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.
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:
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.
A literal that does not fit its type is an error before the program runs:
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.
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:
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:
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:
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:
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:
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:
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.
Read the output line by line:
250 +% 10wraps to 4, and250 +| 10stops at 255.5 -% 10wraps down to 251, and5 -| 10stops at 0.- For the signed
i8,120 *% 2wraps to -16, and120 *| 2stops at 127. - The last line is
top +%= 10u8, which changestopin 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:
Say what you mean with as:
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:
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:
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:
But -1 is below zero, and a u8 cannot hold it. The compiler sees this
before the program runs:
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:
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:
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.
- Things written after a value:
.field,[index]and?. - A prefix operator:
-,-%,!and~. as.*,*%,*|,/and%.+,-,+%,-%,+|and-|.<<and>>, then&, then^, then|.- The comparisons
<,<=,>and>=, then==and!=. &&, then||.
So as binds tighter than any operator with two sides. You can write a cast
inside a sum without parentheses:
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
Intis a 32-bit signed integer. Usei8toi64,u8tou64andUsizewhen you need another size or sign. Odd widths up to 64 bits, such asu33, work too.println(int: ...)prints any integer type up to 64 bits.- A suffix such as
200u8gives a literal its type. Hexadecimal literals take one too, as in0xFFu8. - 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.asconverts between integer types, and it checks the range.- A prefix minus comes before
as:-128 as i8works,-1 as u8is an error before the program runs, and-(x as i64)casts first.
Next: Conditions and loops.