Description: Evaluate an expression

Syntax:
**expr** argument
number expression tag variable

Argument: logical expression, arithmetic expression including numbers, tags, variables and strings

Returns: The return value from the expression

See Also: Calculation Tags

Examples:

Addition

SETVAL tag2=[expr
[GETVAL tag1]+[GETVAL tag2] ]

set sum [expr $x + $x1 +$X2]

Multiplication

SETVAL anatag1=[expr
[GETVAL anatag2] * 10]

set GroupEnd [expr $GroupStart * 3]

Trigonometric Functions

set x [expr [GETVAL slope]/100]

set y [expr
atan($x)]

SETVAL radians=$y

SETVAL degrees=[expr $y*180/3.14159265]

Evaluate an expression. Concatenates arguments (adding separator spaces between them), evaluates the result as a Tcl expression, and returns the value. 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 support non-numeric operands and string comparisons.

A Tcl expression consists of a combination of operands, operators, and parentheses. A blank space may be used between the operands and operators and parentheses; it is ignored by the expression’s instructions. 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 floating-point number if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant 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:

As a numeric value, either integer or floating-point.

As a Tcl variable, using standard
**$** notation. The variable’s value will be used as the
operand.

As a string enclosed in double-quotes
**""**. The expression parser will perform backslash, variable,
and command substitutions on the information between the quotes,
and use the resulting value as the operand

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.

As a Tcl command enclosed in brackets
**[ ]**. The command will be executed and its result will be
used as the operand.

As a mathematical function whose
arguments have any of the above forms for operands, such as
**sin($x) .**

For a list of defined functions see section

Where substitutions occur above (e.g. inside quoted strings), they are performed by the expression’s instructions. However, the command parser may already have performed an additional layer of substitution before the expression processor was called. As discussed below, it is usually best to enclose expressions in braces to prevent the command parser from performing substitutions on the contents.

For some examples of simple expressions, the command on the left side of each of the lines below will produce the value on the right side of the line:

**set a
3 **
# evaluates to 3

**expr 3.1 +
$a**
# evaluates to 6.1

**expr 2 +
"$a.$b"**
# evaluates to 5.6

**expr 4*[llength "6
2"]** # evaluates
to 8

For a list of all expressions and more detail, please see Sections 12.6 OPERATORS and 12.7 MATH FUNCTIONS

See also 12.10.5 Perform math on Tags