_________________________________________________________________
NAME
expr - Evalue an expression
SYNOPSIS
expr arg ?arg arg ...?
_________________________________________________________________
DESCRIPTION
Concatenates arg's (adding separator spaces between them), evaluates the result as a Tcl expression, and returns the value. The operators permitted in Tcl expressions are a subset of the operators permitted in C expressions, and they have the same meaning and precedence as the corresponding C operators. Expressions almost always yield numeric results (integer or floating-point values). For example, the expression
expr 8.2 + 6
evaluates to 14.2. Tcl expressions differ from C expressions in the way that operands are specified. Also, Tcl expressions support non-numeric operands and string comparisons.
OPERANDS
A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between the operands and operators and parentheses; it is ignored by the expression processor. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x). If an operand does not have one of the integer formats given above, then it is treated as a floatingpoint number if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSIcompliant C compiler (except that the ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in most installations). For example, all of the following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interpretation is possible, then an operand is left as a string (and only a limited set of operators may be applied to it).
Operands may be specified in any of the following ways:
For some examples of simple expressions, suppose the variable a has the value 3 and the variable b has the value 6. Then the command on the left side of each of the lines below will produce the value on the right side of the line:
+ - Add and subtract. Valid for any numeric operands.
<< >> Left and right shift. Valid for integer operands only.
< > <= >= Boolean less, greater, less than or equal, and greater than or equal. Each operator produces 1 if the condition is true, 0 otherwise. These operators may be applied to strings as well as numeric operands, in which case string comparison is used.
== != Boolean equal and not equal. Each operator produces a zero/one result. Valid for all operand types.
expr 4*2 < 7
returns 0.
The &&, ||, and ?: operators have ``lazy evaluation'', just as in C, which means that operands are not evaluated if they are not needed to determine the outcome. For example, in the command
expr {$v ? [a] : [b]}
only one of [a] or [b] will actually be evaluated, depending on the value of $v. Note, however, that this is only true if the entire expression is enclosed in braces; otherwise the Tcl parser will evaluate both [a] and [b] before invoking the expr command.
MATH FUNCTIONS
Tcl supports the following mathematical functions in expressions:
acos cos hypot sinh asin cosh log sqrt atan exp log10 tan atan2 floor pow tanh ceil fmod sin Each of these functions invokes the math library function of the same name; see the manual entries for the library functions for details on what they do. Tcl also implements the following functions for conversion between integers and floating-point numbers:
abs(arg)
Returns the absolute value of arg. Arg may be either integer or floating-point, and the result is returned in the same form.
double(arg)
If arg is a floating value, returns arg, otherwise converts arg to floating and returns the converted value.
int(arg)
If arg is an integer value, returns arg, otherwise converts arg to integer by truncation and returns the converted value.
round(arg)
If arg is an integer value, returns arg, otherwise converts arg to integer by rounding and returns the converted value.
In addition to these predifined functions, applications may define additional functions using Tcl_CreateMathFunc().
TYPES, OVERFLOW, AND PRECISION
All internal computations involving integers are done with the C type long, and all internal computations involving floating-point are done with the C type double. When converting a string to floating-point, exponent overflow is detected and results in a Tcl error. For conversion to integer from string, detection of overflow depends on the behavior of some routines in the local C library, so it should be regarded as unreliable. In any case, integer overflow and underflow are generally not detected reliably for intermediate results. Floating-point overflow and underflow are detected to the degree supported by the hardware, which is generally pretty reliable.
Conversion among internal representations for integer, floating-point, and string operands is done automatically as needed. For arithmetic computations, integers are used until some floating-point number is introduced, after which floating-point is used. For example,
expr 5 / 4
returns 1, while
expr 5 / 4.0
expr 5 / ( [string length "abcd"] + 0.0 ) both return 1.25. Floating-point values are always | returned with a ``.'' or an ``e'' so that they will not | look like integer values. For example, |
expr {"0x03" > "2"}
expr {"0y" < "0x12"}
both return 1. The first comparison is done using integer comparison, and the second is done using string comparison after the second operand is converted to the string ``18''.
KEYWORDS
arithmetic, boolean, compare, expression