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 may be specified in any of the following ways:

[1] As an numeric value, either integer or floating-point.

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

[3] 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

[4] 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.

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

[6] As a mathematical function whose arguments have any of the above forms for operands, such as ``**sin($x)**''. See below for a list of defined functions.

Where substitutions occur above (e.g. inside quoted strings), they are performed by the expression processor. However, an additional layer of substitution may already have been performed by the command parser 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, 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****+****$a**-
**6.1**

**expr****2****+****"$a.$b"**-
**5.6****expr****4*[llength****"6****2"]****8****expr****{{word****one}****<****"word****$a"}0**

**-****+****~****!**Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operands may be applied to string operands, and bitwise NOT may be applied only to integers.

**+** **-** 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.

**&**- Bit-wise AND. Valid for integer operands only.

**^**- Bit-wise exclusive OR. Valid for integer operands only.

**|**- Bit-wise OR. Valid for integer operands only.

**&&**- Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise. Valid for numeric operands only (integers or floating-point).

**||**- Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. Valid for numeric operands only (integers or floating-point).

*x?y:z*- If-then-else, as in C. If
*x*evaluates to non-zero, then the result is the value of*y*. Otherwise the result is the value of*z*. The*x*operand must have a numeric value.

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.

**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 predefined functions, applications may define additional functions using **Tcl_CreateMathFunc**().

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** **20.0/5.0**

returns ``4.0'', not ``4''. The global variable **tcl_precision** determines the the number of significant digits that are retained when floating values are converted to strings (except that trailing zeroes are omitted). If **tcl_precision** is unset then 6 digits of precision are used. To retain all of the significant bits of an IEEE floatingpoint number set **tcl_precision** to 17; if a value is converted to string with 17 digits of precision and then converted back to binary for some later calculation, the resulting binary value is guaranteed to be identical to the original one.

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 isn't generally a good idea to use operators like