Integers
An integer is a number without a fractional component, like 42
or -123
. Acton has multiple integer types, which are used to represent different ranges of numbers. Signed numbers, whose type start with i
followed by the number of bits, can represent both positive and negative numbers, while unsigned numbers, starting with u
can only represent positive numbers.
Type | Min | Max |
---|---|---|
i16 |
-32768 | 32767 |
i32 |
-2147483648 | 2147483647 |
i64 |
-9223372036854775808 | 9223372036854775807 |
u16 |
0 | 65535 |
u32 |
0 | 4294967295 |
u64 |
0 | 18446744073709551615 |
int |
arbitrary | arbitrary |
int
provides arbitrary precision, meaning it can represent any integer value, regardless of its size. This is useful when you need to work with very large numbers, but it comes with a significant performance cost compared to the fixed size integer. If you don't need arbitrary precision, it's better to use a fixed-size integer type.
You can convert between integer types by calling the instructor for a particular type, like i16(42)
. This is safe when converting to a larger integer type. When converting to a smaller integer type, the value must be within the range of the smaller type, otherwise, an error will be thrown. Ensure that your values are small enough to fit in the target type. For example, you can convert an i32
to an i16
if the value is between -32768
and 32767
.
Assign the value |
|
Convert to an |
|
Ensure the value is within the range of a |
|
Attempt to convert to a |
|
Convert to a |
|