Numbers#
Types#
Int#
Int is a 64-bit signed integer.
Overflow wraps silently, two’s complement.
(Spec: [LEXP.7.13.1])
Float#
Float is IEEE 754 binary64, rounding to nearest, ties to even, with subnormals kept.
An Int operand promotes to Float under an operator. A division by zero with a Float operand follows Division by zero.
(Spec: [LDET.3.6.1], [LEXP.7.3])
Bool#
Bool is the type of true and false.
The arithmetic operators take Int or Float, so a Bool is no operand of theirs. Numeric and Bool convert by no cast. and, or and not are Kleene.
(Spec: [LEXP.7.1a])
Committed slot types#
A String, Int, Float or Bool slot’s type is committed at schema time.
A Float slot in a string comparison is refused with DS_VALIDATE_TYPE_MISMATCH, not a runtime fallback.
(Spec: [LEXP.7.12.5])
Operator result types#
Promotion#
All operands Int give Int. Any Float operand gives Float, and each Int operand promotes.
r.n + 1
r.level + r.n
Promotion at a slot#
Int to Float promotion applies where an operator combines operands and not where a value is assigned to a declared slot.
An emit or publish kwarg of type Int into a Float slot is refused with DS_VALIDATE_TYPE_MISMATCH. Write the cast.
(Spec: [LEXP.7.3b], Source: compiler/mercury/src/dsc.validate.m)
Arithmetic operands#
+, -, *, /, // and % take Int or Float.
A String operand is refused with DS_VALIDATE_BAD_OPERAND_TYPE.
Refused with DS_VALIDATE_BAD_OPERAND_TYPE
r.label + r.label
Exponent#
a ** b
** raises a to the power b.
Int ** Intwith a compile-time-known non-negative exponent, a literal or aconstthat folds to at least 0, isInt, by binary exponentiation underIntwrap.Int ** Intwith a negative exponent, or one whose sign is not known at compile time, isFloat, by correctly roundedpow.2 ** -3 == 0.125.0to a negative power is+Inf, and±Inffor an odd exponent.A non-integer exponent or a
Floatoperand givesFloat, by the samepow.
** is right-associative and each power is typed on its own: a ** b ** 2 with a runtime b is Float.
(Spec: [LEXP.7.3a], [LEXP.7.3a.1])
True division#
a / b
/ divides, and its result is Float, on two Int values included.
INT_LIT / INT_LIT is a Rational literal, not the / operator. Any non-literal operand makes it the operator.
(Spec: [LEXP.7.4])
Floor division#
a // b
// divides and rounds toward negative infinity; its result type follows Promotion.
7 // 2 == 3, -7 // 2 == -4, -7.5 // 2 == -4.0.
With a Float operand and a nonzero divisor, r being step 1’s exact remainder under Modulo:
q = (a - r) / b, the subtraction and the division each rounded once.If Modulo’s step 2 adjusted
r,qbecomesq - 1, rounded once.If
qis 0, the result is a zero with the sign ofa / b.Otherwise, with
fthe floor ofq, the result isf + 1ifq - fexceeds 0.5, elsef. A NaNqgives NaN.
(Spec: [LEXP.7.5])
Modulo#
a % b
% is the remainder; Int % Int is Int, and any Float operand gives Float.
A nonzero result takes the divisor’s sign: 5.5 % 2 == 1.5, -7 % 3 == 2, -7.5 % 2 == 0.5.
With a Float operand and a nonzero divisor:
r = a - b * trunc(a / b), computed exactly, witha’s sign. It is NaN whenais infinite or either operand is NaN, andawhenbis infinite andafinite.If
ris nonzero and its sign differs fromb’s,rbecomesr + b, rounded once.If
ris 0, the result is a zero withb’s sign; otherwise it isr.
(Spec: [LEXP.7.6])
Division by zero#
A /, // or % whose operands are all Int is a runtime error when the divisor is zero, / included.
With a Float operand the operation is IEEE 754’s: / and // give ±Inf for a nonzero dividend and NaN for a zero one, and % gives NaN.
A false guard decides an and on either side of it: x != 0 and y / x > 1 and y / x > 1 and x != 0 are both false when x is 0.
(Spec: [LEXP.7.15.1], [LEXP.7.15.2], [LEXP.7.16.1])
Unary minus#
-a
Unary - is arithmetic negation of an Int or a Float.
A unit-typed Int or Float is an Int or a Float here. An operand of any other kind, a Duration among them, is refused with DS_VALIDATE_BAD_OPERAND_TYPE.
(Spec: [LEXP.7.10])
Unary plus#
+a
Unary + on a numeric operand is a no-op.
(Spec: [LEXP.7.11])
Bitwise operators#
a & b, a | b, a ^ b, ~a, a << n, a >> n
The bitwise operators take integer operands.
A Float operand whose type is known statically is refused with DS_VALIDATE_BITWISE_TYPE.
Refused with DS_VALIDATE_BITWISE_TYPE
r.level & 1
(Spec: [LEXP.7.7], [LEXP.7.9], Source: compiler/mercury/src/dsc.typecheck.m)
Shifts#
a << n, a >> n
>> is an arithmetic shift, the floor of a / 2^n; << is a * 2^n under Int wrap.
The count is not reduced modulo 64. A count of 64 or more gives 0 or -1 for >>, by the sign of a, and 0 for <<. A negative literal count is refused with DS_VALIDATE_NEGATIVE_SHIFT. A count negative at run time is a ComputationError of kind DOMAIN.
Refused with DS_VALIDATE_NEGATIVE_SHIFT
r.n << -1
(Spec: [LEXP.7.8], [LEXP.7.14.1], Source: compiler/mercury/src/dsc.typecheck.m)