In this chapter we shed some light onto widely unknown features. Parts like the operator precedence unconsciously are exploited in every-day programming by all of us. Others like the use of function variables are truly unknown, at least to the average Scilab user. So, read on and become a Yedi^H^H^H^HScilab master.
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.
Table 4-1 displays a 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 is generated with a Scilab script, i.e. we had the interpreter determine its own precedence rules, which is neat. These scripts are listed in Chapter 9.
Table 4-1. 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 |
One line asks for an additional warning, and that's the unary minus 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).
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.
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 none, and therefore
~~~~~~~~~~~~~~~%t
returns F.
Table 4-2 shows the complete list of Scilab's logical (also known as boolean) operators sorted according to decreasing precedence.
For the logical operators have boolean expressions as their arguments, it is time now to discuss the implicit promotion of numeric types to boolean type, something very familiar to C, Perl, and Python programmers. You have guessed right, the rule is: "Zero is false, everything else is true." Here are some examples of that rule at work:
-->%t & 0 ans = F -->%t & 0.1 ans = T -->6.34 | %f ans = T -->6.34 | -0.3 ans = T
Scilab always evaluates boolean expressions completely. No operator is defined with short-circuit evaluation semantics.
-->deff('b = ret_false()', 'b = %f, disp(''ret_false'')'); -->ret_false() & ret_false() ret_false ret_false ans = F
[1] | The trigraph operators .*., ./., and .\. are left out. |