Next: logistic() Up: MacAnova Help File Previous: log2()   Contents

logic

Usage:
a && b, a || b, !a, where a and b are LOGICAL or structures with LOGICAL
  components
LOGICAL constants are T (True) and F (False)



Keywords: variables, syntax, logical variables, missing values, operations
Elements of a LOGICAL variable have only three possible values -- True,
False, and MISSING.  A LOGICAL variable may be a vector, matrix, or
array.

Logical values are printed as 'T' (True) and 'F' (False), the same
symbols as you use to enter them.  For example, 'a <- vector(T,F,F,T)'
creates a LOGICAL vector of length 4.

When used as the value of a keyword phrase, as in 'quiet:T', T and F can
usually be interpreted as 'yes' and 'no', respectively.

You can also create LOGICAL data as the result of comparing REAL,
LOGICAL or CHARACTER variables using the following comparison operators:

There are 6 comparison operators used with REAL, LOGICAL and CHARACTER
data and structures of such data.
 Comparison
  Operator  Precedence  Meaning
    a == b      8       Equal or same
    a != b      8       Not equal or different
    a < b       8       Less than
    a <= b      8       Less than or equal
    a > b       8       Greater than
    a >= b      8       Greater than or equal

There are three purely operators used only with LOGICAL data.
   Logical
  Operator  Precedence  Meaning
   a || b       5       Logical Or (T||T,T||F,F||T are True, F||F False)
   a && b       6       Logical And (T&&T is True, T&&F,F&&T,F&&F False)
     !a         7       Logical Not (!T is False, !F is True)

The precedence level in the lists of operators above affects the order
of evaluation when there is more than one operator in an expression.  An
operator with higher precedence is evaluated before one with lower
precedence.  For example, MacAnova interprets T && F || T && T as
(T && F) || (T && T) because '&&' has higher precedence (6) than '||'
(6).

See topic 'precedence' for a complete discussion of operator association
and precedence.

                           Comparison Operators
Comparison operators are most useful with REAL and CHARACTER data.  When
they are used with LOGICAL data, True and False are interpreted as 1 and
0, respectively, in the same way as with arithmetic operators +, -, *,
and ^ or ** (see 'arithmetic').  In particular F == F and T == T are
True and F == T and T < F are False.

Comparison operators do not "associate".  For example, an expression
like '3 < x <= 5' is meaningless and is an error.  Instead, you can use
'3 < x && x <= 5'.

As you would expect, the precedence of comparison operators is lower
than all arithmetic operations (see 'arithmetic') so that, for example,
3*4 == 14-2 is interpreted as (3*4) == (14-2) and is True.

CHARACTER variables are compared using the ASCII collating sequence.
Most punctuation and all numerals are "less than" upper case letters
which in turn are "less than" lower case letters.  A space is "less
than" all printable characters.  Here is the explicit ordering starting
with space:
    !"#$%&'()*+,-./0123456789:;<=>?
   @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
   `abcdefghijklmnopqrstuvwxyz{|}~
On computers with extended character sets, the ordering is dependent on
the internal representation of the characters.

Here's what happens when you make a comparison involving a MISSING
value.
 Operators '<', '<=', '>', or '>=':  The result is MISSING.
 Operator '==' : Result is True only when if both operands are MISSING
 Operator '!=' : Result is True only when just one operand is MISSING.

Examples:
   2 == 1, 3 != 3, 3 < 1, T == F, and T > 1 all have the value False
   2 == 2, 3 != 2, 3 > -1, F == F, and 0 == F all have value True
   "A" > "a", "A" != "A", and "MacAnova 2" == "MacAnova 3" are all False

Because '<-' is the assignment operator, 'a<-5' will always be
interpreted as "assign 5 to a" even if what is wanted is a comparison of
a with -5.  To obtain the latter a space must follow '<' as in 'a < -5'.

On the Macintosh, you can use 'Option+<', 'Option+>' and 'Option+=' in
place of '<=', '>=', and '!=', respectively.

                        Purely logical operators
You can use operators '&&', '||' and '!' (logical AND, OR and NOT) to
combine several conditions to make a single condition or to specify the
opposite of a condition.  For example, (x > 1) && (x < 3) is True if and
only if the value of x is greater than 1 and less than 4 and (x < 0) ||
(y < 0) is True if and only if one or both of x and y is negative.
Also, !(x < 0) means the same as x >= 0.

If an operand of '||', '&&' or '!' is MISSING, so is the result .

The precedence of logical operators is lower than the precedence of
arithmetic expressions (see 'arithmetic') and comparison operations.
For example, 'x > 1 && x < 4' is interpreted as '(x > 1) && (x < 4)',
and is True if and only if the value of x is greater than 1 and less
than 4.  Because the precedence of '!' is lower than the precedence of
the comparison operators, expressions like '! a < b' are evaluated as
'!(a < b)'.  See also topic 'precedence'.

Examples:
   F && T, F || F, and !T all have the value False
   F || T and !F have the value True

                     Functions alltrue() and anytrue()
In contrast with the C programming language, both expressions that are
combined with '&&' and '||' are always evaluated regardless of the value
of the first expression.  For example, in
   (sqrt(2) < sqrt(3)) || (log(5) < log(6))
both log(5) and log(6) are evaluated although the final value of the
expression (True) could have been determined once it was found that
sqrt(2) < sqrt(3) was True.

Functions alltrue() and anytrue() provide the C behavior.  For example
  Cmd> anytrue(sqrt(2) < sqrt(3), log(5) > log(6)) # value is True

and

  Cmd> alltrue(sqrt(4) < sqrt(2), log(5) < log(6)) # value is False

evaluate only the first arguments.

           Comparison and logical operations with non-scalars
MacAnova allows comparison of or logical combination of arrays of
different sizes entirely analogously to the way they can be combined
arithmetically by '+', '-', '*', '/', '^', and '%%'.  For instance, '2 <
run(3)' is vector(F,F,T).  See topic 'arithmetic' for details.

When one of the operands is a structure, each of its components is
combined with the other argument, producing a structure with the same
shape as the structure argument.  If both arguments are structures, they
must have the same shape and the corresponding components are combined.
In either case, all the components of a structure must have the same
type and all components must be compatible.  See topic 'structures'.


Gary Oehlert 2003-01-15