All of the usual simple operations on numbers are available to MOO programs:

+ - * / %

These are, in order, addition, subtraction, multiplication, division, and remainder. In the following table, the expressions on the left have the corresponding values on the right:

5 + 2 => 7 5 - 2 => 3 5 * 2 => 10 5 / 2 => 2 5.0 / 2.0 => 2.5 5 % 2 => 1 5.0 % 2.0 => 1.0 5 % -2 => 1 -5 % 2 => -1 -5 % -2 => -1 -(5 + 2) => -7

Note that integer division in MOO throws away the remainder and that the result
of the remainder operator (``%'`) has the same sign as the left-hand
operand. Also, note that ``-'` can be used without a left-hand operand to
negate a numeric expression.

Fine point:Integers and floating-point numbers cannot be mixed in any particular use of these arithmetic operators; unlike some other programming languages, MOO does not automatically coerce integers into floating-point numbers. You can use the`tofloat()`

function to perform an explicit conversion.

The ``+'` operator can also be used to append two strings. The expression

"foo" + "bar"

has the value

"foobar"

Unless both operands to an arithmetic operator are numbers of the same kind
(or, for ``+'`, both strings), the error value `E_TYPE`

is raised. If
the right-hand operand for the division or remainder operators (``/'` or
``%'`) is zero, the error value `E_DIV`

is raised.

MOO also supports the exponentiation operation, also known as "raising to a
power," using the ``^'` operator:

3 ^ 4 => 81 3 ^ 4.5 error--> E_TYPE 3.5 ^ 4 => 150.0625 3.5 ^ 4.5 => 280.741230801382

Note that if the first operand is an integer, then the second operand must also be an integer. If the first operand is a floating-point number, then the second operand can be either kind of number. Although it is legal to raise an integer to a negative power, it is unlikely to be terribly useful.

Go to the first, previous, next, last section, table of contents.