4.3. Operator Precedence And Associativity

Strange but true, there is no listing of the precedence and associativity of neither class of Scilab's operators anywhere in the documentation. So, we discuss the operator precedence and associativity in detail.

4.3.1. Numeric Operators

Table 4-6 shows the list of all numeric operators up to digraphs,[1] sorted in descending order of their precedence. An equal precedence value (column 1) means the operators are evaluated following the given associativity (column 3).

The table has been generated with a Scilab script, i.e., we had the interpreter determine its own precedence rules. These scripts are listed in Chapter 10.

Table 4-6. Arithmetic Operators

precedence operator associativity comment
21 + right unary
20 ^ right  
20 .^ right  
19 - right unary
8 * non  
8 / left  
8 .* non  
8 ./ left  
4 \ left  
4 .\ left  
1 + non binary
1 - left binary
Warning

One line begs for an additional warning, and that is the unary minus ranking at level 19. It looses against the power operator, ^. Therefore, -1^2 gives -1, and not 1. In other words Scilab sees -1^2 as -(1^2).

The association rules follow those of standard algebra. Thus, nobody should be surprised that a^b^c is interpreted as a^(b^c).

4.3.2. Relational Operators

Scilab implements the usual gang of relational operators with some syntactic sugar of having two "unequality"-operators <>, and ~=. The relational operators' precedences rank in between the numeric and the logical operators like they do in many other modern programming languages. This allows for a minimal use of parentheses in larger expressions like


if 2.0*n > l+1.0  |  n/3.0 <= k then
    ...
end

which evaluates exactly the same way as


if ((2.0 * n) > (l + 1.0)) | ((n / 3.0) <= k) then
    ...
end

just with much less line-noise.

4.3.3. Logical Operators

There are three logical operators: &, |, and ~, meaning "and", "or", and "not". The twiddle, ~ has the unique syntactic property that any number of consecutive twiddles are allowed and evaluated. But unless you want to enter the obfuscated Scilab contest, sticking with one probably is best as e.g. 15 ~ are as good as one, and therefore


~~~~~~~~~~~~~~~%t

returns F.

Table 4-7 shows the complete list of Scilab's logical, also known as boolean, operators sorted according to decreasing precedence.

Table 4-7. Boolean Operators

operator associativity comment
~ right unary
& non  
| non  

Notes

[1]

The trigraph operators .*., ./., and .\. are left out.