Tcl expr operands
Tcl operands are treated as integers, where feasible. They may be specified as decimal, binary (first two characters must be 0b), hexadecimal (first two characters must be 0x), or octal (first two characters must be 0o). Care should be taken when passing integers with a leading 0, for example 08, as the interpreter would evaluate 08 as an illegal octal value. If no integer formats are included, the command will evaluate the operand as a floating-point numeric value. For scientific notations, the character e (or E) is inserted as appropriate. If no numeric interpretation is feasible, the value will be evaluated as a string. In this case, the value must be enclosed within double quotes or braces. Please note that not all operands are accepted by all operators. To avoid inadvertent variable substitution, it is always best to enclose the operands within braces. For example, take a look at the following:
expr 1+1*3
will return a value of4
.expr (1+1)*3
will return a value of6
.
Operands may be presented in any of the following:
Operand |
Explanation |
---|---|
Numeric |
Integer and floating-point values may be passed directly to the command. |
Boolean |
All standard Boolean values (true, false, yes, no, 0, or 1) are supported. |
Tcl variable |
All referenced variables (in Tcl, a variable is referenced using the |
Strings (in double quotes) |
Strings contained within double quotes may be passed with no need to include backslash, variable, or command substitution, as these are handled automatically (see the chapter on String Expressions and Handling for clarification on these terms and their usage). |
Strings (in braces) |
Strings contained within braces will be used with no substitution. |
Tcl commands |
Tcl commands must be enclosed within square braces. The command will be executed and the mathematical function is performed on the return value. |
Named functions |
Functions, such as sine, cosine, and so on. |
Tcl supports a subset of the C programming language math operators and treats them in the same manner and precedence. If a named function (such as sine) is encountered, expr
automatically makes a call to the mathfunc
namespace to minimize the syntax required to obtain the value.
Tcl expr
operators may be specified as noted in the following table, in the descending order of precedence:
Operator |
Explanation |
---|---|
- + ~ ! |
Unary minus, unary plus, bitwise NOT and logical NOT. Cannot be applied to string operands. Bit-wise NOT may be applied to only integers. |
** |
Exponentiation Numeric operands only. |
*/ % |
Multiply, divide, and remainder. Numeric operands only. |
+ - |
Add and subtract. Numeric operands only. |
<< >> |
Left shift and right shift. Integer operands only. A right shift always propagates the sign bit. |
< > <= >= |
Boolean Less, Boolean Greater, Boolean Less Than or Equal To, Boolean Greater Than or Equal To (A value of 1 is returned if the condition is true, otherwise a 0 is returned). If utilized for strings, string comparison will be applied. |
== != |
Boolean Equal and Boolean Not Equal (A value of 1 is returned if the condition is true, otherwise a 0 is returned). |
|
Boolean String Equal and Boolean String Not Equal (A value of 1 is returned if the condition is true, otherwise a 0 is returned). Any operand provided will be interpreted as a string. |
|
List Containment and Negated List Containment (A value of 1 is returned if the condition is true, otherwise a 0 is returned). The first operand is treated as a string value, the second as a list. |
& |
Bitwise AND Integers only. |
^ |
Bitwise Exclusive OR Integers only. |
| |
Bitwise OR Integers only. |
&& |
Logical Boolean and numeric (integer and floating-point) operands only. |
x?y:z |
The |