EXPRESSIONS

From TNT

Jump to: navigation, search

General syntax is similar to that in C. Precedence is checked from left to right (the parser works by calculating the values as it reads the expressions). Operators: arithmetic (+ - * /), comparisons ( == > < >= <= != ), logical ( && || ! ), and bitwise operations (& | ^). When a number is expected, within the context of a macro command, enclosing a series of numbers (0-29) in curly braces is equivalent to typing the integral number that corresponds to that bit set (that is, { 0 3 4 } = 20+23+24 = 1 + 8 + 16 = 25). Bitwise representation is used (as in other parsimony programs) to store sets of states at internal nodes or polymorphic terminal taxa; the expressions that access state-sets (such as states, see under "help+states;") return sets of states as sets of bits. The legal operations and comparisons are:


  a == b if a equals b, value is 1 (0 otherwise)
  a > b if a is greater than b, value is 1
        (same for <)
  a>= b if a is greater than or equal to b, value is 1
        (same for <=)
  a != b if a is different from b, value is 1
  a && b if conditions a and b fullfilled, value is 1
  a || b if conditions a or b fullfilled, value is 1
 !a if condition a is not fullfilled, value is 1
  a & b if a bit is 1 in a and in b, it is 1 in the result
  a | b if a bit is 1 in a or b, it is 1 in the result
  a ^ b if a bit is 1 in a or b (but not both), then it is
      1 in the result

A "condition" may consist of just the numerical value of a number. For example, the variable success in the example above, might have been checked as

if ( 'success' == 1 ) ...

but, given that success itself can take values 0 (zero, "false"), or different from 0 ("true"), this is unnecessary, and identical to the more economical expression used in the example:


if ( 'success' ) ...

Note that a negative number is different from 0, and therefore is also evaluated as "true".

Also, the value returned by a logical comparison is either 0 or 1, which sometimes facilitates writing more compact instructions. For example, placing in a variable numlongtrees the number of trees that are longer than (say) 1000 steps might be done as:


   loop 0 ntrees
      if ( length[0] > 1000 )
        set numlongtrees 'numlongtrees'+1 ;
      end
    stop
    QUOTE There are 'numlongtrees' above 1000 steps ;


But the same result would be achieved by the more compact:


    loop 0 ntrees
       set numlongtrees += ( length[0] > 1000 ) ;
    stop
    QUOTE There are 'numlongtrees' above 1000 steps ;
Personal tools