Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mathematical operations

The compatibility tables below represent the actions taken by the interpreter when the operation is applied to two values of the given types. anything means that the operation can be applied to any data type that wasn't explicitly listed above in the list (the list is evaluated from top to bottom, so the first matching type is used).

Folding operations

Addidion, subtraction, multiplication, division, and modulo in XMLang accept any number of children.

They are evaluated and the result is calculated by using the popular fold iterator method.

The first child's value is used as the initial value and stored in an accumulator. Then, each subsequent child's value is used to update the accumulator by applying the operation. The final value of the accumulator is returned as the result.

If no children are provided, the result is null.

If the two data types of an operation are incompatible, an error (Can't {operation} incompatible types: {type1} and {type2}) is thrown.

Example

<program>
    <print>
        <add>
            <int>1</int>
            <float>2.1</float> <!-- 3.1 -->
            <int>3</int> <!-- 6.1 -->
            <string>a</string> <!-- "6.1a" -->
        </add>
    </print> <!-- prints 6.1a -->
</program>

<add>

The order of the children does not matter, as addition is commutative.

Compatible types:

<sub>

The order of the children matters, as subtraction is not commutative.

Incompatible types:

Compatible types:

<mul>

The order of the children does not matter, as multiplication is commutative.

Incompabile types:

Compatible types:

<div>

The order of the children matters, as division is not commutative.

Division by zero will throw the error Division by zero is not allowed.

Incompatible types:

Compatible types:

<mod>

The order of the children matters, as modulo is not commutative.

Modulo by zero will throw the error Division by zero is not allowed.

Incompatible types:

Compatible types:

Unary operations

Arithmetic negation and absolute value in XMLang accept a single child.

If the data type of an operation is incompatible, an error (Can't {operation} incompatible type: {type}) is thrown.

<neg>

The <neg> element is used to compute the additive inverse of a value.

Incompatible types:

Compatible types:

<abs>

The <abs> element is used to calculate the absolute value of a number.

Incompatible types:

Compatible types: