Integers
Tomo has five types of integers:
Int
: the default integer type, which uses an efficient tagged 29-bit integer value for small numbers, and falls back to a bigint implementation when values are too large to fit in 29-bits. The bigint implementation uses the GNU MP library. These integers are fast for small numbers and guaranteed to always be correct and never overflow.Int8
/Int16
/Int32
/Int64
: Fixed-size integers that take upN
bits. These integers must be explicitly constructed using their type name (e.g.Int64(5)
) and are subject to overflowing on arithmetic operations. If an overflow occurs, a runtime error will be raised.- In cases where it is possible to infer that an integer literal should be
used as a fixed-size integer, the literal will be converted at compile time to
the appropriate fixed-size integer type and checked to ensure that it can fit
in the needed size. For example, if you declare a variable as
x := Int64(0)
and later dox + 1
, it’s inferred that the1
is a 64-bit integer literal.
Runtime conversion between integer types (casting) can be done explicitly
by calling the target type as a function: Int32(x)
. For
fixed-width types, the conversion function also accepts a second parameter,
truncate
. If truncate
is no
(the
default), conversion will create a runtime error if the value is too large to
fit in the target type. If truncate
is yes
, then the
resulting value will be a truncated form of the input value.
Integers support the standard math operations (x+y
,
x-y
, x*y
, x/y
) as well as
powers/exponentiation (x^y
), modulus (x mod y
and
x mod1 y
), and bitwise operations: x and y
,
x or y
, x xor y
, x << y
,
x >> y
, x >>> y
(unsigned right
shift), and x <<< y
(unsighted left shift). The
operators and
, or
, and xor
are
bitwise, not logical operators.
Integer Literals
The simplest form of integer literal is a string of digits, which is
inferred to have type Int
(unbounded size).
>>> 123456789012345678901234567890
= 123456789012345678901234567890 : Int
Underscores may also be used to visually break up the integer for readability:
:= 1_000_000 a_million
Hexadecimal, octal, and binary integer literals are also supported:
:= 0x123F
hex := 0o644
octal := 0b10101 binary
For fixed-sized integers, use the type’s name as a constructor:
:= Int64(12345)
my_int64 := Int32(12345)
my_int32 := Int32(12345)
my_int16 := Int32(123) my_int8
A compiler error will be raised if you attempt to construct a value that
cannot fit in the specified integer size (e.g. Int8(99999)
).
A Note on Division
Unlike some other languages (including C), Tomo uses a mathematically consistent definition of division called Euclidean Division that upholds the following invariants for all inputs:
:= numerator / denominator
quotient := numerator mod denominator
remainder
# Modulus always gives a non-negative result:
>> remainder >= 0
= yes
# The numerator can be reconstructed sensibly:
>> numerator == denominator * quotient + remainder
= yes
Importantly, these invariants hold for both positive and negative numerators and denominators. When the numerator and denominator are both positive, you will not notice any difference from how integer division and modulus work in other programming languages. However, the behavior is a bit different when negative numbers are involved. Integer division rounds down instead of rounding towards zero, and modulus never gives negative results:
>> quotient := -1 / 5
= -1
>> remainder := -1 mod 5
= 4
>> -1 == 5 * -1 + 4
= yes
>> quotient := 16 / -5
= -3
>> remainder := -1 mod 5
= 1
>> 16 == -5 * -3 + 1
= yes