Skip to content

multiplicative expression

Cameron Purdy edited this page Apr 7, 2020 · 1 revision

The *, /, and % binary operators are well-known, representing multiplication, division, and modulo. Ecstasy adds a new operator that performs division and remainder (not the modulo) in a single step, the "divrem" operator /%. The corresponding expressions use the common forms:

a * b
a / b
a % b
a /% b

The operators and their corresponding default method names are as follows:

Op Default method name Description
* mul multiplication
/ div division
% mod modulo
/% divrem division, remainder

Despite appearing to be primitive-type operators, Ecstasy has no primitive type system, and the input and output types of these operators are defined entirely by the types against which the operators execute. Specifically, the type of the expression a must have an unambiguously single best operator method (selected by the operator symbol and method name) that takes an argument of the type of expression b. The implicit type of the expression is the return type of the operator method.

The execution of the expression is an invocation of the selected operator method, against a target reference yielded by the expression a, passing one argument as yielded by the expression b; the result of the expression is the return value from the operator method.

The mathematical behavior defined for the % operator is modulo, not remainder. Specifically, the modulo is defined as a value in the range [0..divisor) for positive divisors, and (divisor..0] for negative divisors.

The mathematical behavior defined for the /% operator is division and remainder, not modulo. This matches the behavior of % in C and Java, for example[1].

The expression short-circuits if either expression a or b short-circuits.

The expression uses the default left-to-right definite assignment rules:

  • The VAS before a is the VAS before the expression.
  • The VAS before b is the VAS after a.
  • The VAS after the expression is the VAS after b.

These expressions group to the left, so for example, a * b * c is treated as (a * b) * c:

    MultiplicativeExpression:
        ElvisExpression
        MultiplicativeExpression * ElvisExpression
        MultiplicativeExpression / ElvisExpression
        MultiplicativeExpression % ElvisExpression
        MultiplicativeExpression /% ElvisExpression

[1]

C and Java refer to the % operator as the modulo operator, but actually produce the remainder.