modified: diffout.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / help / unexpected
blob4f91b63a8360d091534076f4e72095a35e96a60e
1 Unexpected
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
5     review this file.
7     Persons familiar with shell scripting may want to review this file
8     as well, particularly notes dealing with command line evaluation
9     and execution.
12     The Comma
13     =========
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
21     passed to f in:
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
26     object.  In:
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
31     created xx object.
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
36     been defined:
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.
44     ^ is not xor
45     ** is exponentiation
46     ====================
48     In C, ^ is the xor operator. The expression:
50             a ^ b
52     yields "a to the b power", NOT "a xor b".
54     Unlike in C, calc evaluates the expression:
56             a ** b
58     also yields "a to the b power".
60     Here "a" and "b" can be a real value or a complex value:
62         2^3                     3i^4
63         2.5 ^ 3.5               0.5i ^ 0.25
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};
69         a^3
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:
76         -1 ^ 0.5 == -1
78     whereas:
80         (-1) ^ 0.5 == 1i
82     because the above expression in parsed as:
84         -(1 ^ 0.5) == -1
86     whereas:
88         (-1) ^ 0.5 == 1i
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.
99     For example:
101             a += b *= c
103     has the effect of:
105             a = (a + b) * c
107     where only 'a' is required to be an lvalue.  For the effect of:
109             b *= c; a += b
111     when both 'a' and 'b' are lvalues, use:
113             a += (b *= c)
116     || yields values other than 0 or 1
117     ==================================
119     In C:
121             a || b
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:
127             a ? a : b
129     In other words, if 'a' is true, then 'a' is returned, otherwise
130     'b' is returned.
133     && yields values other than 0 or 1
134     ==================================
136     In C:
138             a && b
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:
144             a ? b : a
146     In other words, if 'a' is true, then 'b' is returned, otherwise
147     'a' is returned.
150     / is fractional divide, // is integral divide
151     =============================================
153     In C:
155             x/y
157     performs integer division when 'x' and 'y' are integer types.
158     In calc, this expression yields a rational number.
160     Calc uses:
162             x//y
164     to perform division with integer truncation and is the equivalent to:
166             int(x/y)
169     | and & have higher precedence than ==, +, -, *, / and %
170     ========================================================
172     Is C:
174             a == b | c * d
176     is interpreted as:
178             (a == b) | (c * d)
180     and calc it is interpreted as:
182             a == ((b | c) * d)
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:
195             A * B + C * D
197     In calc above expression is evaluated in the following order:
199             A
200             B
201             A * B
202             C
203             D
204             C * D
205             A * B + C * D
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,
217     the expression:
219             f(x++, x++) + g(x++)
221     evaluates to:
223             f(x, x + 1) + g(x + 2)
225     and increments x three times.
227     In an other example, this expression:
229         1<<8/2
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:
259             undefine *
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
265             show numbers
267     should be those associated with epsilon(), and if it has been
268     called, qpi().
271     #! is also a comment
272     ====================
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
285         /*
286          * another correct comment
287          */
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:
309         if (expr) {
310             ...
311         }
313     However this WILL NOT WORK AS EXPECTED:
315         if (expr)
316         {
317             ...
318         }
320     because calc will parse the if being terminated by
321     an empty statement followed by a
323         if (expr) ;
324         {
325             ...
326         }
328     In the same way, use these forms:
330         for (optionalexpr ; optionalexpr ; optionalexpr) {
331                 ...
332         }
334         while (expr) {
335                 ...
336         }
338         do {
339                 ...
340         while (expr);
342     where the initial { is on the SAME LINE as the if, while,
343     for or do keyword.
345     NOTE: See "help statement", "help todo", and "help bugs".
348     Shell evaluation of command line arguments
349     ==========================================
351     In most interactive shells:
353         calc 2 * 3
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:
361         calc '2 * 3'
363     or:
365         calc 2 \* 3
367     or some other form of shell meta-character escaping.
370     Calc reads standard input after processing command line args
371     ============================================================
373     The shell command:
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.
381     will produce:
383         16
384         2
385         3
386         4
387         5
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:
400         16
401         25
402         36
403         49
404         64
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/