Numbers
In LIGO, there are two types of numbers: integers and natural numbers.
Integer literals are the same found in mainstream programming languages, for example,
10
,-6
and0
, but there is only one canonical zero:0
(so, for instance,-0
and00
are invalid).Natural numbers are written as digits followed by the suffix
n
, like so:12n
,0n
, and the same restriction on zero as integers applies:0n
is the only way to specify the natural zero.
Contrary to integral numbers in other programming languages, numbers in LIGO have arbitrary-precision, that is, they do not overflow or underflow.
Digits of large numbers can be separated by an underscore, to increase readability.
As a form of documentation, a type can be ascribed to each constant:
Casting
In mathematics, natural numbers are a strict subset of integers, and can be used in any context where an integer is expected. In LIGO, this property does not hold true in general. Instead, a given binary arithmetic operation, say, is defined four times, so it can apply to any combination of natural numbers and integers: this is called overloading, and some programming languages extend it to user-defined functions (e.g. members in C++) -- but not LIGO.
So there are no implicit type casts in LIGO, but we can explicitly
cast natural numbers to integers (this is safe in all contexts where
an integer is valid) by calling the predefined function int
. The
inverse cast, from int
to nat
is called in mathematics the
absolute value, or abs
in LIGO.
Adding
Addition in LIGO is accomplished by means of the +
binary operator,
which is overloaded to apply to any combination of natural numbers and
integers, as shown in the following examples. Note that adding an
integer to a natural number produces an integer, because the compiler
cannot determine, in general, whether the result would be always a
natural number for all inputs.
Subtracting
Subtraction in LIGO is accomplished by means of the -
binary
operator which is overloaded to apply to any combination of natural
numbers and integers, as shown in the following examples. The rule
when subtracting two natural numbers is that the result is an integer
because, in general, the compiler cannot determine whether the value
of an expression is positive or zero for all inputs.
Negating
The arithmetic negation of a number is the same as subtracting that number from zero, so the negation of a natural numbers yields an integer:
Multiplying
Multiplication in LIGO is accomplished by means of the *
binary
operator which is overloaded to apply to any combination of natural
numbers and integers, as shown in the following examples. The type
rules for multiplication are the same as for the addition:
Dividing
Because LIGO features neither floating-point nor fixed-point
arithmetic, division in LIGO is Euclidean. The predefined binary
operator /
returns the quotient and is overloaded like the
multiplication. Of course, division by zero triggers an exception that
interrups the execution, so the programmer must make sure this case
cannot happen because the compiler cannot determine, in general, if a
variable will have a given value (or not) for all inputs.
The binary operator %
returns the positive modulo of the
Euclidean division, that is, the following holds:
(n*(a/n)+(a%n) == a) && (0n <= a % n) && (a % n < abs(n))
It is overloaded as the Euclidean division /
to allow for all four
combinations of natural numbers and integers.
It is possible to obtain both the quotient and remainder together, by means of the predefined function
ediv
: See Euclidean division.