

[1]  As a numeric value, either integer or floatingpoint. 
[2]  As a boolean value, using any form understood by string is boolean. 
[3]  As a Tcl variable, using standard $ notation. The variable’s value will be used as the operand. 
[4]  As a string enclosed in doublequotes. The expression parser will perform backslash, variable, and command substitutions on the information between the quotes, and use the resulting value as the operand 
[5]  As a string enclosed in braces. The characters between the open brace and matching close brace will be used as the operand without any substitutions. 
[6]  As a Tcl command enclosed in brackets. The command will be executed and its result will be used as the operand. 
[7]  As a mathematical function whose arguments have any of the above forms for operands, such as sin($x). See MATH FUNCTIONS below for a discussion of how mathematical functions are handled. 
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:
expr 3.1 + $a6.1 expr 2 + "$a.$b"5.6 expr 4*[llength "6 2"]8 expr {{word one} < "word $a"}0
The valid operators (most of which are also available as commands in the tcl::mathop namespace; see the mathop(n) manual page for details) are listed below, grouped in decreasing order of precedence:
See the C manual for more details on the results produced by each operator. The exponentiation operator promotes types like the multiply and divide operators, and produces a result that is the same as the output of the pow function (after any type conversions.)
 + ~ ! Unary minus, unary plus, bitwise NOT, logical NOT. None of these operators may be applied to string operands, and bitwise NOT may be applied only to integers. ** Exponentiation. Valid for any numeric operands. ’0u1v’
* / % Multiply, divide, remainder. None of these operators may be applied to string operands, and remainder may be applied only to integers. The remainder will always have the same sign as the divisor and an absolute value smaller than the divisor. +  Add and subtract. Valid for any numeric operands. << >> Left and right shift. Valid for integer operands only. A right shift always propagates the sign bit. < > <= >= 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. eq ne Boolean string equal and string not equal. Each operator produces a zero/one result. The operand types are interpreted only as strings. in ni List containment and negated list containment. Each operator produces a zero/one result and treats its first argument as a string and its second argument as a Tcl list. The in operator indicates whether the first argument is a member of the second argument list; the ni operator inverts the sense of the result. ’0u1v’
& Bitwise AND. Valid for integer operands only. ^ Bitwise exclusive OR. Valid for integer operands only.  Bitwise OR. Valid for integer operands only. && Logical AND. Produces a 1 result if both operands are nonzero, 0 otherwise. Valid for boolean and numeric (integers or floatingpoint) operands only.  Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. Valid for boolean and numeric (integers or floatingpoint) operands only. x?y:z Ifthenelse, as in C. If x evaluates to nonzero, then the result is the value of y. Otherwise the result is the value of z. The x operand must have a boolean or numeric value. ’0u1v’All of the binary operators group lefttoright within the same precedence level. For example, the command
returns 0.expr {4*2 < 7}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
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.expr {$v ? [a] : [b]}
When the expression parser encounters a mathematical function such as sin($x), it replaces it with a call to an ordinary Tcl function in the tcl::mathfunc namespace. The processing of an expression such as:
is the same in every way as the processing of:expr {sin($x+$y)}which in turn is the same as the processing of:expr {[tcl::mathfunc::sin [expr {$x+$y}]]}tcl::mathfunc::sin [expr {$x+$y}]The executor will search for tcl::mathfunc::sin using the usual rules for resolving functions in namespaces. Either ::tcl::mathfunc::sin or [namespace current]::tcl::mathfunc::sin will satisfy the request, and others may as well (depending on the current namespace path setting).
See the mathfunc(n) manual page for the math functions that are available by default.
’0u1v’
All internal computations involving integers are done calling on the LibTomMath multiple precision integer library as required so that all integer calculations are performed exactly. Note that in Tcl releases prior to 8.5, integer calculations were performed with one of the C types long int or Tcl_WideInt, causing implicit range truncation in those calculations where values overflowed the range of those types. Any code that relied on these implicit truncations will need to explicitly add int() or wide() function calls to expressions at the points where such truncation is required to take place.
’0u1v’
All internal computations involving floatingpoint are done with the C type double. When converting a string to floatingpoint, exponent overflow is detected and results in the double value of Inf or Inf as appropriate. Floatingpoint overflow and underflow are detected to the degree supported by the hardware, which is generally pretty reliable.
Conversion among internal representations for integer, floatingpoint, and string operands is done automatically as needed. For arithmetic computations, integers are used until some floatingpoint number is introduced, after which floatingpoint is used. For example,
returns 1, whileexpr {5 / 4}both return 1.25. Floatingpoint values are always returned with a “.” or an “e” so that they will not look like integer values. For example,expr {5 / 4.0} expr {5 / ( [string length "abcd"] + 0.0 )}returns 4.0, not 4.expr {20.0/5.0}
String values may be used as operands of the comparison operators, although the expression evaluator tries to do comparisons as integer or floatingpoint when it can, except in the case of the eq and ne operators. If one of the operands of a comparison is a string and the other has a numeric value, a canonical string representation of the numeric operand value is generated to compare with the string operand. Canonical string representation for integer values is a decimal string format. Canonical string representation for floatingpoint values is that produced by the %g format specifier of Tcl’s format command. For example, the commands
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. Because of Tcl’s tendency to treat values as numbers whenever possible, it is not generally a good idea to use operators like == when you really want string comparison and the values of the operands could be arbitrary; it is better in these cases to use the eq or ne operators, or the string command instead.expr {"0x03" > "2"} expr {"0y" < "0x12"}
Enclose expressions in braces for the best speed and the smallest storage requirements. This allows the Tcl bytecode compiler to generate the best code.
As mentioned above, expressions are substituted twice: once by the Tcl parser and once by the expr command. For example, the commands
return 11, not a multiple of 4. This is because the Tcl parser will first substitute $a + 2 for the variable b, then the expr command will evaluate the expression $a + 2*4.set a 3 set b {$a + 2} expr $b*4Most expressions do not require a second round of substitutions. Either they are enclosed in braces or, if not, their variable and command substitutions yield numbers or strings that do not themselves require substitutions. However, because a few unbraced expressions need two rounds of substitutions, the bytecode compiler must emit additional instructions to handle this situation. The most expensive code is required for unbraced expressions that contain command substitutions. These expressions must be implemented by generating new code each time the expression is executed. When the expression is unbraced to allow the substitution of a function or operator, consider using the commands documented in the mathfunc(n) or mathop(n) manual pages directly instead.
’0u1v’
Define a procedure that computes an “interesting” mathematical function:proc tcl::mathfunc::calc {x y} { expr { ($x**2  $y**2) / exp($x**2 + $y**2) } }Convert polar coordinates into cartesian coordinates:
# convert from ($radius,$angle) set x [expr { $radius * cos($angle) }] set y [expr { $radius * sin($angle) }]Convert cartesian coordinates into polar coordinates:
# convert from ($x,$y) set radius [expr { hypot($y, $x) }] set angle [expr { atan2($y, $x) }]Print a message describing the relationship of two string values to each other:
puts "a and b are [expr {$a eq $b ? {equal} : {different}}]"Set a variable to whether an environment variable is both defined at all and also set to a true boolean value:
set isTrue [expr { [info exists ::env(SOME_ENV_VAR)] && [string is true strict $::env(SOME_ENV_VAR)] }]Generate a random integer in the range 0..99 inclusive:
set randNum [expr { int(100 * rand()) }]
array(n), for(n), if(n), mathfunc(n), mathop(n), namespace(n), proc(n), string(n), Tcl(n), while(n)
arithmetic, boolean, compare, expression, fuzzy comparison
Copyright (c) 1993 The Regents of the University of California. Copyright (c) 19942000 Sun Microsystems Incorporated. Copyright (c) 2005 by Kevin B. Kenny <kennykb@acm.org>. All rights reserved.
Tcl  EXPR (n)  8.5 
Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.