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 upNbits. 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 the1is 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).
i := 123456789012345678901234567890
Underscores may also be used to visually break up the integer for readability:
a_million := 1_000_000
Hexadecimal, octal, and binary integer literals are also supported:
hex := 0x123F
octal := 0o644
binary := 0b10101
For fixed-sized integers, use the type's name as a constructor:
my_int64 := Int64(12345)
my_int32 := Int32(12345)
my_int16 := Int32(12345)
my_int8 := Int32(123)
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:
quotient := numerator / denominator
remainder := numerator mod denominator
# Modulus always gives a non-negative result:
assert remainder >= 0
# The numerator can be reconstructed sensibly:
assert numerator == denominator * quotient + remainder
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
assert quotient == -1
remainder := -1 mod 5
assert remainder == 4
assert -1 == 5 * -1 + 4
quotient := 16 / -5
assert quotient == -3
remainder := -1 mod 5
assert remainder == 1
assert 16 == -5 * -3 + 1
API
1 # Integers3 Tomo has five types of integers:6 integer value for small numbers, and falls back to a bigint implementation7 when values are too large to fit in 29-bits. The bigint implementation uses8 the GNU MP library. These integers are fast for small numbers and guaranteed9 to always be correct and never overflow.11 These integers must be explicitly constructed using their type name (e.g.13 overflow occurs, a runtime error will be raised.14 - In cases where it is possible to infer that an integer literal should be15 used as a fixed-size integer, the literal will be converted at compile time16 to the appropriate fixed-size integer type and checked to ensure that it17 can fit in the needed size. For example, if you declare a variable as19 integer literal.21 Runtime conversion between integer types (casting) can be done explicitly by26 value will be a truncated form of the input value.34 ## Integer Literals36 The simplest form of integer literal is a string of digits, which is inferred40 i := 12345678901234567890123456789041 ```43 Underscores may also be used to visually break up the integer for readability:46 a_million := 1_000_00047 ```49 Hexadecimal, octal, and binary integer literals are also supported:52 hex := 0x123F53 octal := 0o64454 binary := 0b1010155 ```57 For fixed-sized integers, use the type's name as a constructor:60 my_int64 := Int64(12345)61 my_int32 := Int32(12345)62 my_int16 := Int32(12345)63 my_int8 := Int32(123)64 ```66 A compiler error will be raised if you attempt to construct a value that cannot69 ## A Note on Division71 Unlike some other languages (including C), Tomo uses a mathematically72 consistent definition of division called [Euclidean73 Division](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf)74 that upholds the following invariants for all inputs:77 quotient := numerator / denominator78 remainder := numerator mod denominator80 # Modulus always gives a non-negative result:81 assert remainder >= 083 # The numerator can be reconstructed sensibly:84 assert numerator == denominator * quotient + remainder85 ```87 Importantly, these invariants hold for both positive and negative numerators88 and denominators. When the numerator and denominator are both positive, you89 will not notice any difference from how integer division and modulus work in90 other programming languages. However, the behavior is a bit different when95 quotient := -1 / 596 assert quotient == -198 remainder := -1 mod 599 assert remainder == 4101 assert -1 == 5 * -1 + 4102 ```105 quotient := 16 / -5106 assert quotient == -3108 remainder := -1 mod 5109 assert remainder == 1111 assert 16 == -5 * -3 + 1112 ```114 # API116 [API documentation](../api/integers.md)