3 While calc is C-like, users of C will find some unexpected
4 surprises in calc syntax and usage. Persons familiar with C should
7 Persons familiar with shell scripting may want to review this file
8 as well, particularly notes dealing with command line evaluation
15 The comma is also used for continuation of obj and mat creation
16 expressions and for separation of expressions to be used for
17 arguments or values in function calls or initialization lists. The
18 precedence order of these different uses is: continuation,
19 separator, comma operator. For example, assuming the variables a,
20 b, c, d, e, and object type xx have been defined, the arguments
23 f(a, b, c, obj xx d, e)
25 are a, b, c, and e, with e having the value of a newly created xx
28 f((a, b), c, (obj xx d), e)
30 the arguments of f are b, c, d, e, with only d being a newly
33 In combination with other operators, the continuation use of the
34 comma has the same precedence as [] and ., the separator use the
35 same as the comma operator. For example, assuming xx.mul() has
38 f(a = b, obj xx c, d = {1,2} * obj xx e = {3,4})
40 passes two arguments: a (with value b) and the product d * e of two
41 initialized xx objects.
48 In C, ^ is the xor operator. The expression:
52 yields "a to the b power", NOT "a xor b".
54 Unlike in C, calc evaluates the expression:
58 also yields "a to the b power".
60 Here "a" and "b" can be a real value or a complex value:
64 2.5 ^ 2.718i 3.13145i ^ 0.30103i
66 In addition, "a" can be matrix. In this case "b" must be an integer:
68 mat a[2,2] = {1,2,3,4};
71 Note that 'a' == 0 and 'b' is real, then is must be >= 0 as well.
72 Also 0^0 and 0**0 return the value 1.
74 Be careful about the precedence of operators. Note that:
82 because the above expression in parsed as:
91 op= operators associate left to right
92 =====================================
94 Operator-with-assignments:
96 += -= *= /= %= //= &= |= <<= >>= ^= **=
98 associate from left to right instead of right to left as in C.
107 where only 'a' is required to be an lvalue. For the effect of:
111 when both 'a' and 'b' are lvalues, use:
116 || yields values other than 0 or 1
117 ==================================
123 will produce 0 or 1 depending on the logical evaluation
124 of the expression. In calc, this expression will produce
125 either 'a' or 'b' and is equivalent to the expression:
129 In other words, if 'a' is true, then 'a' is returned, otherwise
133 && yields values other than 0 or 1
134 ==================================
140 will produce 0 or 1 depending on the logical evaluation
141 of the expression. In calc, this expression will produce
142 either 'a' or 'b' and is equivalent to the expression:
146 In other words, if 'a' is true, then 'b' is returned, otherwise
150 / is fractional divide, // is integral divide
151 =============================================
157 performs integer division when 'x' and 'y' are integer types.
158 In calc, this expression yields a rational number.
164 to perform division with integer truncation and is the equivalent to:
169 | and & have higher precedence than ==, +, -, *, / and %
170 ========================================================
180 and calc it is interpreted as:
185 calc always evaluates terms from left to right
186 ==============================================
188 Calc has a definite order for evaluation of terms (addends in a
189 sum, factors in a product, arguments for a function or a matrix,
190 etc.). This order is always from left to right. but skipping of
191 terms may occur for ||, && and ? : .
193 Consider, for example:
197 In calc above expression is evaluated in the following order:
207 This order of evaluation is significant if evaluation of a
208 term changes a variable on which a later term depends. For example:
210 x++ * x++ + x++ * x++
212 in calc returns the value:
214 x * (x + 1) + (x + 2) * (x + 3)
216 and increments x as if by x += 4. Similarly, for functions f, g,
223 f(x, x + 1) + g(x + 2)
225 and increments x three times.
227 In an other example, this expression:
231 evalues to 128, not 16, because <<8 is performed before the /2.
234 &A[0] and A are different things in calc
235 ========================================
237 In calc, value of &A[0] is the address of the first element, whereas
238 A is the entire array.
241 *X may be used to to return the value of X
242 ==========================================
244 If the current value of a variable X is an octet, number or string,
245 *X may be used to to return the value of X; in effect X is an
246 address and *X is the value at X.
249 freeing a variable has the effect of assigning the null value to it
250 ===================================================================
252 The freeglobals(), freestatics(), freeredc() and free() free
253 builtins to not "undefine" the variables, but have the effect of
254 assigning the null value to them, and so frees the memory used for
255 elements of a list, matrix or object.
257 Along the same lines:
261 undefines all current user-defined functions. After executing
262 all the above freeing functions (and if necessary free(.) to free
263 the current "old value"), the only remaining numbers as displayed by
267 should be those associated with epsilon(), and if it has been
274 In addition to the C style /* comment lines */, lines that begin with
275 #! are treated as comments.
277 A single # is an calc operator, not a comment. However two or more
278 ##'s in a row is a comment. See "help pound" for more information.
280 #!/usr/local/src/cmd/calc/calc -q -f
282 /* a correct comment */
283 ## another correct comment
284 ### two or more together is also a comment
286 * another correct comment
288 print "2+2 =", 2+2; ## yet another comment
290 This next example is WRONG:
292 #!/usr/local/src/cmd/calc/calc -q -f
294 # This is not a calc calc comment because it has only a single #
295 # You must to start comments with ## or /*
296 print "This example has invalid comments"
298 See "help cscript" and "help usage" for more information.
301 The { must be on the same line as an if, for, while or do
302 =========================================================
304 When statement is of the form { ... }, the leading { MUST BE ON
305 THE SAME LINE as the if, for, while or do keyword.
307 This works as expected:
313 However this WILL NOT WORK AS EXPECTED:
320 because calc will parse the if being terminated by
321 an empty statement followed by a
328 In the same way, use these forms:
330 for (optionalexpr ; optionalexpr ; optionalexpr) {
342 where the initial { is on the SAME LINE as the if, while,
345 NOTE: See "help statement", "help todo", and "help bugs".
348 Shell evaluation of command line arguments
349 ==========================================
351 In most interactive shells:
355 will frequently produce a "Missing operator" error because the '*' is
356 evaluated as a "shell glob". To avoid this you must quote or escape
357 argument with characters that your interactive shell interprets.
359 For example, bash / ksh / sh shell users should use:
367 or some other form of shell meta-character escaping.
370 Calc reads standard input after processing command line args
371 ============================================================
375 seq 5 | while read i; do calc "($i+3)^2"; done
377 FYI: The command "seq 5" will write 1 through 5 on separate
378 lines on standard output, while read i sets $i to
379 the value of each line that is read from stdin.
389 The reason why the last 4 lines of output are 2 through 5 is
390 that after calc evaluates the first line and prints (1+3)^2
391 (i.e., 16), calc continues to read stdin and slurps up all
392 of the remaining data on the pipe.
394 To avoid this problem, use:
396 seq 5 | while read i; do calc "($i+3)^2" </dev/null; done
398 which produces the expected results:
407 ## Copyright (C) 1999-2007 Landon Curt Noll
409 ## Calc is open software; you can redistribute it and/or modify it under
410 ## the terms of the version 2.1 of the GNU Lesser General Public License
411 ## as published by the Free Software Foundation.
413 ## Calc is distributed in the hope that it will be useful, but WITHOUT
414 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
415 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
416 ## Public License for more details.
418 ## A copy of version 2.1 of the GNU Lesser General Public License is
419 ## distributed with calc under the filename COPYING-LGPL. You should have
420 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
421 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
423 ## @(#) $Revision: 30.4 $
424 ## @(#) $Id: unexpected,v 30.4 2008/05/10 13:18:09 chongo Exp $
425 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/unexpected,v $
427 ## Under source code control: 1997/03/21 13:15:18
428 ## File existed as early as: 1997
430 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
431 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/