3 The operators are similar to C, but there are some differences in
4 the associativity and precedence rules for some operators. In
5 addition, there are several operators not in C, and some C
6 operators are missing. A more detailed discussion of situations
7 that may be unexpected for the C programmer may be found in
8 the 'unexpected' help file.
10 Below is a list giving the operators arranged in order of
11 precedence, from the least tightly binding to the most tightly
12 binding. Except where otherwise indicated, operators at the same
13 level of precedence associate from left to right.
15 Unlike C, calc has a definite order for evaluation of terms (addends
16 in a sum, factors in a product, arguments for a function or a
17 matrix, etc.). This order is always from left to right. but
18 skipping of terms may occur for ||, && and ? : . For example,
19 an expression of the form:
23 is evaluated in the following order:
33 This order of evaluation is significant if evaluation of a
34 term changes a variable on which a later term depends. For example:
40 x * (x + 1) + (x + 2) * (x + 3)
42 and increments x as if by x += 4. Similarly, for functions f, g,
49 f(x, x + 1) + g(x + 2)
51 and increments x three times.
53 In A || B, B is read only if A tests as false; in A && B, B is
54 read only if A tests as true. Thus if x is nonzero,
55 x++ || x++ returns x and increments x once; if x is zero,
56 it returns x + 1 and increments x twice.
60 a, b returns the value of b.
61 For situations in which a comma is used for another purpose
62 (function arguments, array indexing, and the print statement),
63 parenthesis must be used around the comma operator expression.
64 E.g., if A is a matrix, A[(a, b), c] evaluates a, b, and c, and
65 returns the value of A[b, c].
67 += -= *= /= %= //= &= |= <<= >>= ^= **=
68 Operator-with-assignments.
69 These associate from left to right, e.g. a += b *= c has the
70 effect of a = (a + b) * c, where only a is required to be an
71 lvalue. For the effect of b *= c; a += b; when both a and b
72 are lvalues, use a += (b *= c).
75 As in C, this, when repeated, this associates from right to left,
76 e.g. a = b = c has the effect of a = (b = c). Here both a and b
79 ? : Conditional value.
80 a ? b : c returns b if a tests as true (i.e. nonzero if
81 a is a number), c otherwise. Thus it is equivalent to:
82 if (a) return b; else return c;.
83 All that is required of the arguments in this function
84 is that the "is-it-true?" test is meaningful for a.
85 As in C, this operator associates from right to left,
86 i.e. a ? b : c ? d : e is evaluated as a ? b : (c ? d : e).
89 Unlike C, the result for a || b is one of the operands
90 a, b rather than one of the numbers 0 and 1.
91 a || b is equivalent to a ? a : b, i.e. if a tests as
92 true, a is returned, otherwise b. The effect in a
93 test like "if (a || b) ... " is the same as in C.
96 Unlike C, the result for a && b is one of the operands
97 a, b rather than one of the numbers 0 and 1.
98 a && b is equivalent to a ? b : a, i.e. if a tests as
99 true, b is returned, otherwise a. The effect in a
100 test like "if (a && b) ... " is the same as in C.
106 Binary plus and minus and unary plus and minus when applied to
107 a first or only term.
110 Multiply, divide, and modulo.
111 Please Note: The '/' operator is a fractional divide,
112 whereas the '//' is an integral divide. Thus think of '/'
113 as division of real numbers, and think of '//' as division
114 of integers (e.g., 8 / 3 is 8/3 whereas 8 // 3 is 2).
115 The '%' is integral or fractional modulus (e.g., 11%4 is 3,
116 and 10%pi() is ~.575222).
119 In a | b, both a and b are to be real integers;
120 the signs of a and b are ignored, i.e.
121 a | b = abs(a) | abs(b) and the result will
122 be a non-negative integer.
125 In a & b, both a and b are to be real integers;
126 the signs of a and b are ignored as for a | b.
130 The '^' and '**' are both exponentiation, e.g. 2^3
131 returns 8, 2^-3 returns .125. Note that in a^b, if
132 'a' == 0 and 'b' is real, then is must be >= 0 as well.
133 Also 0^0 and 0**0 return the value 1.
135 For the shift operators both arguments are to be
136 integers, or if the first is complex, it is to have
137 integral real and imaginary parts. Changing the
138 sign of the second argument reverses the shift, e.g.
139 a >> -b = a << b. The result has the same sign as
140 the first argument except that a nonzero value is
141 reduced to zero by a sufficiently long shift to the
142 right. These operators associate right to left,
143 e.g. a << b ^ c = a << (b ^ c).
146 Plus (+) and minus (-) have their usual meanings as unary
147 prefix operators at this level of precedence when applied to
148 other than a first or only term.
150 As a prefix operator, '!' is the logical NOT: !a returns 0 if
151 a tests as nonzero, and 1 if a tests as zero, i.e. it is
152 equivalent to a ? 0 : 1. Be careful about
153 using this as the first character of a top level command,
154 since it is also used for executing shell commands.
156 As a postfix operator ! gives the factorial function, i.e.
160 Pre or post incrementing or decrementing.
161 These are applicable only to variables.
164 Indexing, double-bracket indexing, element references,
165 and function calls. Indexing can only be applied to matrices,
166 element references can only be applied to objects, but
167 double-bracket indexing can be applied to matrices, objects,
170 variables constants . ( )
171 These are variable names and constants, the special '.' symbol,
172 or a parenthesized expression. Variable names begin with a
173 letter, but then can contain letters, digits, or underscores.
174 Constants are numbers in various formats, or strings inside
175 either single or double quote marks.
178 The most significant difference from the order of precedence in
179 C is that | and & have higher precedence than ==, +, -, *, / and %.
180 For example, in C a == b | c * d is interpreted as:
189 Most of the operators will accept any real or complex numbers
190 as arguments. The exceptions are:
193 Second argument must be nonzero.
196 The exponent must be an integer. When raising zero
197 to a power, the exponent must be non-negative.
200 Both both arguments must be integers.
203 The shift amount must be an integer. The value being
204 shifted must be an integer or a complex number with
205 integral real and imaginary parts.
208 See the 'unexpected' help file for a list of unexpected
209 surprises in calc syntax/usage. Persons familiar with C should
210 read the 'unexpected' help file to avoid confusion.
212 ## Copyright (C) 1999 Landon Curt Noll
214 ## Calc is open software; you can redistribute it and/or modify it under
215 ## the terms of the version 2.1 of the GNU Lesser General Public License
216 ## as published by the Free Software Foundation.
218 ## Calc is distributed in the hope that it will be useful, but WITHOUT
219 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
220 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
221 ## Public License for more details.
223 ## A copy of version 2.1 of the GNU Lesser General Public License is
224 ## distributed with calc under the filename COPYING-LGPL. You should have
225 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
226 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
228 ## @(#) $Revision: 30.2 $
229 ## @(#) $Id: operator,v 30.2 2007/07/11 23:00:39 chongo Exp $
230 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/operator,v $
232 ## Under source code control: 1991/07/21 04:37:23
233 ## File existed as early as: 1991
235 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
236 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/