mirror of
https://github.com/lcn2/calc.git
synced 2025-08-16 01:03:29 +03:00
Release calc version 2.10.2t30
This commit is contained in:
185
help/operator
Normal file
185
help/operator
Normal file
@@ -0,0 +1,185 @@
|
||||
operators
|
||||
|
||||
The operators are similar to C, but there are some differences
|
||||
in the associativity and precedence rules for some operators.
|
||||
In addition, there several operators not in C, and some C operators
|
||||
are missing. Below is a list giving the operators arranged in
|
||||
order of precedence, from the least tightly binding to the most
|
||||
tightly binding.
|
||||
|
||||
Except where otherwise indicated, operators at the same level of
|
||||
precedence associate from left to right.
|
||||
|
||||
Unlike C, calc has a definite order for evaluation of terms (addends
|
||||
in a sum, factors in a product, arguments for a function or a
|
||||
matrix, etc.). This order is always from left to right. but
|
||||
skipping of terms may occur for ||, && and ? : . For example,
|
||||
an expression of the form:
|
||||
|
||||
A * B + C * D
|
||||
|
||||
is evaluated in the following order:
|
||||
|
||||
A
|
||||
B
|
||||
A * B
|
||||
C
|
||||
D
|
||||
C * D
|
||||
A * B + C * D
|
||||
|
||||
This order of evaluation is significant if evaluation of a
|
||||
term changes a variable on which a later term depends. For example:
|
||||
|
||||
x++ * x++ + x++ * x++
|
||||
|
||||
returns the value of:
|
||||
|
||||
x * (x + 1) + (x + 2) * (x + 3)
|
||||
|
||||
and increments x as if by x += 4. Similarly, for functions f, g,
|
||||
the expression:
|
||||
|
||||
f(x++, x++) + g(x++)
|
||||
|
||||
evaluates to:
|
||||
|
||||
f(x, x + 1) + g(x + 2)
|
||||
|
||||
and increments x three times.
|
||||
|
||||
In A || B, B is read only if A tests as false; in A && B, B is
|
||||
read only if A tests as true. Thus if x is nonzero,
|
||||
x++ || x++ returns x and increments x once; if x is zero,
|
||||
it returns x + 1 and increments x twice.
|
||||
|
||||
|
||||
, Comma operator.
|
||||
For situations in which a comma is used for another purpose
|
||||
(function arguments, array indexing, and the print statement),
|
||||
parenthesis must be used around the comma operator.
|
||||
|
||||
= += -= *= /= %= //= &= |= <<= >>= ^= **=
|
||||
Assignments. As in C, these associate from right to left.
|
||||
|
||||
|
||||
? : Conditional value.
|
||||
a ? b : c returns b if a tests as true (i.e. nonzero if
|
||||
a is a number), c otherwise. Thus it is equivalent to:
|
||||
if (a) return b; else return c;.
|
||||
All that is required of the arguments in this function
|
||||
is that the "is-it-true?" test is meaningful for a.
|
||||
As in C, this operator associates from right to left,
|
||||
i.e. a ? b : c ? d : e is evaluated as a ? b : (c ? d : e).
|
||||
|
||||
|| Logical OR.
|
||||
Unlike C, the result for a || b is one of the operands
|
||||
a, b rather than one of the numbers 0 and 1.
|
||||
a || b is equivalent to a ? a : b, i.e. if a tests as
|
||||
true, a is returned, otherwise b. The effect in a
|
||||
test like "if (a || b) ... " is the same as in C.
|
||||
|
||||
&& Logical AND.
|
||||
Unlike C, the result for a && b is one of the operands
|
||||
a, b rather than one of the numbers 0 and 1.
|
||||
a && b is equivalent to a ? b : a, i.e. if a tests as
|
||||
true, b is returned, otherwise a. The effect in a
|
||||
test like "if (a && b) ... " is the same as in C.
|
||||
|
||||
== != <= >= < >
|
||||
Relations.
|
||||
|
||||
+ -
|
||||
Binary plus and minus.
|
||||
|
||||
* / // %
|
||||
Multiply, divide, and modulo.
|
||||
Please Note: The '/' operator is a fractional divide,
|
||||
whereas the '//' is an integral divide. Thus think of '/'
|
||||
as division of real numbers, and think of '//' as division
|
||||
of integers (e.g., 8 / 3 is 8/3 whereas 8 // 3 is 2).
|
||||
The '%' is integral or fractional modulus (e.g., 11%4 is 3,
|
||||
and 10%pi() is ~.575222).
|
||||
|
||||
| Bitwise OR.
|
||||
In a | b, both a and b are to be real integers;
|
||||
the signs of a and b are ignored, i.e.
|
||||
a | b = abs(a) | abs(b) and the result will
|
||||
be a non-negative integer.
|
||||
|
||||
& Bitwise AND.
|
||||
In a & b, both a and b are to be real integers;
|
||||
the signs of a and b are ignored as for a | b.
|
||||
|
||||
^ ** << >>
|
||||
Powers and shifts.
|
||||
The '^' and '**' are both exponentiation, e.g. 2^3
|
||||
returns 8, 2^-3 returns .125. In a ^ b, b has to be
|
||||
an integer and if a is zero, nonnegative. 0^0 returns
|
||||
the value 1.
|
||||
|
||||
For the shift operators both arguments are to be
|
||||
integers, or if the first is complex, it is to have
|
||||
integral real and imaginary parts. Changing the
|
||||
sign of the second argument reverses the shift, e.g.
|
||||
a >> -b = a << b. The result has the same sign as
|
||||
the first argument except that a nonzero value is
|
||||
reduced to zero by a sufficiently long shift to the
|
||||
right. These operators associate right to left,
|
||||
e.g. a << b ^ c = a << (b ^ c).
|
||||
|
||||
+ - !
|
||||
Unary operators.
|
||||
The '!' is the logical NOT operator: !a returns 0 if
|
||||
a is nonzero, and 1 if a is zero, i.e. it is
|
||||
equivalent to a ? 0 : 1. Be careful about
|
||||
using this as the first character of a top level command,
|
||||
since it is also used for executing shell commands.
|
||||
|
||||
++ --
|
||||
Pre or post incrementing or decrementing.
|
||||
These are applicable only to variables.
|
||||
|
||||
[ ] [[ ]] . ( )
|
||||
Indexing, double-bracket indexing, element references,
|
||||
and function calls. Indexing can only be applied to matrices,
|
||||
element references can only be applied to objects, but
|
||||
double-bracket indexing can be applied to matrices, objects,
|
||||
or lists.
|
||||
|
||||
variables constants . ( )
|
||||
These are variable names and constants, the special '.' symbol,
|
||||
or a parenthesized expression. Variable names begin with a
|
||||
letter, but then can contain letters, digits, or underscores.
|
||||
Constants are numbers in various formats, or strings inside
|
||||
either single or double quote marks.
|
||||
|
||||
|
||||
The most significant difference from the order of precedence in
|
||||
C is that | and & have higher precedence than ==, +, -, *, / and %.
|
||||
For example, in C a == b | c * d is interpreted as:
|
||||
|
||||
(a == b) | (c * d)
|
||||
|
||||
and calc it is:
|
||||
|
||||
a == ((b | c) * d)
|
||||
|
||||
|
||||
Most of the operators will accept any real or complex numbers
|
||||
as arguments. The exceptions are:
|
||||
|
||||
/ // %
|
||||
Second argument must be nonzero.
|
||||
|
||||
^
|
||||
The exponent must be an integer. When raising zero
|
||||
to a power, the exponent must be non-negative.
|
||||
|
||||
| &
|
||||
Both both arguments must be integers.
|
||||
|
||||
<< >>
|
||||
The shift amount must be an integer. The value being
|
||||
shifted must be an integer or a complex number with
|
||||
integral real and imaginary parts.
|
Reference in New Issue
Block a user