home *** CD-ROM | disk | FTP | other *** search
-
-
-
- eeeexxxxpppprrrr((((3333TTTTccccllll)))) eeeexxxxpppprrrr((((3333TTTTccccllll))))
-
-
-
- NNNNAAAAMMMMEEEE
- expr - Evaluate an expression
-
- SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
- eeeexxxxpppprrrr _a_r_g ?_a_r_g _a_r_g ...?
-
-
- DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
- Concatenates _a_r_g'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
-
- eeeexxxxpppprrrr 8888....2222 ++++ 6666
-
- 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.
-
- OOOOPPPPEEEERRRRAAAANNNNDDDDSSSS
- 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 0000), or in hexadecimal (if the first two
- characters of the operand are 0000xxxx). 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:
-
- [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
-
-
-
-
-
-
- PPPPaaaaggggeeee 1111
-
-
-
-
-
-
- eeeexxxxpppprrrr((((3333TTTTccccllll)))) eeeexxxxpppprrrr((((3333TTTTccccllll))))
-
-
-
- [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 ``ssssiiiinnnn(((($$$$xxxx))))''. 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 aaaa has the
- value 3 and the variable bbbb 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:
-
- eeeexxxxpppprrrr 3333....1111 ++++ $$$$aaaa 6666....1111
- eeeexxxxpppprrrr 2222 ++++ """"$$$$aaaa....$$$$bbbb"""" 5555....6666
- eeeexxxxpppprrrr 4444****[[[[lllllllleeeennnnggggtttthhhh """"6666 2222""""]]]] 8888
- eeeexxxxpppprrrr {{{{{{{{wwwwoooorrrrdddd oooonnnneeee}}}} <<<< """"wwwwoooorrrrdddd $$$$aaaa""""}}}}0000
-
-
- OOOOPPPPEEEERRRRAAAATTTTOOOORRRRSSSS
- The valid operators are listed below, grouped in decreasing order of
- precedence:
-
- ---- ++++ ~~~~ !!!! Unary minus, unary plus, bit-wise NOT, logical NOT. |
- None of these operands may be applied to string
- operands, and bit-wise NOT may be applied only to
- integers.
-
- **** //// %%%% Multiply, divide, remainder. None of these operands
- 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.
-
- <<<< >>>> <<<<==== >>>>==== 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
-
-
-
- PPPPaaaaggggeeee 2222
-
-
-
-
-
-
- eeeexxxxpppprrrr((((3333TTTTccccllll)))) eeeexxxxpppprrrr((((3333TTTTccccllll))))
-
-
-
- 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.
-
- See the C manual for more details on the results produced by each
- operator. All of the binary operators group left-to-right within the
- same precedence level. For example, the command
-
- eeeexxxxpppprrrr 4444****2222 <<<< 7777
-
- 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
-
- eeeexxxxpppprrrr {{{{$$$$vvvv ???? [[[[aaaa]]]] :::: [[[[bbbb]]]]}}}}
-
- only one of [[[[aaaa]]]] or [[[[bbbb]]]] will actually be evaluated, depending on the value
- of $$$$vvvv. Note, however, that this is only true if the entire expression is
- enclosed in braces; otherwise the Tcl parser will evaluate both [[[[aaaa]]]] and
- [[[[bbbb]]]] before invoking the eeeexxxxpppprrrr command.
-
- MMMMAAAATTTTHHHH FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
- Tcl supports the following mathematical functions in expressions: |
-
- aaaaccccoooossss ccccoooossss hhhhyyyyppppooootttt ssssiiiinnnnhhhh |
- aaaassssiiiinnnn ccccoooosssshhhh lllloooogggg ssssqqqqrrrrtttt |
- aaaattttaaaannnn eeeexxxxpppp lllloooogggg11110000 ttttaaaannnn |
- aaaattttaaaannnn2222 fffflllloooooooorrrr ppppoooowwww ttttaaaannnnhhhh |
-
-
-
- PPPPaaaaggggeeee 3333
-
-
-
-
-
-
- eeeexxxxpppprrrr((((3333TTTTccccllll)))) eeeexxxxpppprrrr((((3333TTTTccccllll))))
-
-
-
- cccceeeeiiiillll ffffmmmmoooodddd ssssiiiinnnn |
-
- 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:
-
- aaaabbbbssss((((_a_r_g))))
- Returns the absolute value of _a_r_g. _A_r_g may be either integer or |
- floating-point, and the result is returned in the same form.
-
- ddddoooouuuubbbblllleeee((((_a_r_g))))
- If _a_r_g is a floating value, returns _a_r_g, otherwise converts _a_r_g to |
- floating and returns the converted value.
-
- iiiinnnntttt((((_a_r_g))))
- If _a_r_g is an integer value, returns _a_r_g, otherwise converts _a_r_g to |
- integer by truncation and returns the converted value.
-
- rrrroooouuuunnnndddd((((_a_r_g))))
- If _a_r_g is an integer value, returns _a_r_g, otherwise converts _a_r_g to |
- integer by rounding and returns the converted value. |
-
- In addition to these predefined functions, applications may define |
- additional functions using TTTTccccllll____CCCCrrrreeeeaaaatttteeeeMMMMaaaatttthhhhFFFFuuuunnnncccc().
-
- TTTTYYYYPPPPEEEESSSS,,,, OOOOVVVVEEEERRRRFFFFLLLLOOOOWWWW,,,, AAAANNNNDDDD PPPPRRRREEEECCCCIIIISSSSIIIIOOOONNNN
- All internal computations involving integers are done with the C type
- _l_o_n_g, and all internal computations involving floating-point are done
- with the C type _d_o_u_b_l_e. 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,
-
- eeeexxxxpppprrrr 5555 //// 4444
-
- returns 1, while
-
- eeeexxxxpppprrrr 5555 //// 4444....0000
- eeeexxxxpppprrrr 5555 //// (((( [[[[ssssttttrrrriiiinnnngggg lllleeeennnnggggtttthhhh """"aaaabbbbccccdddd""""]]]] ++++ 0000....0000 ))))
-
- 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,|
-
-
-
- PPPPaaaaggggeeee 4444
-
-
-
-
-
-
- eeeexxxxpppprrrr((((3333TTTTccccllll)))) eeeexxxxpppprrrr((((3333TTTTccccllll))))
-
-
-
- eeeexxxxpppprrrr 22220000....0000////5555....0000 |
-
- returns ``4.0'', not ``4''. The global variable ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn determines|
- the the number of significant digits that are retained when floating |
- values are converted to strings (except that trailing zeroes are |
- omitted). If ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn is unset then 6 digits of precision are used.|
- To retain all of the significant bits of an IEEE floating-point number |
- set ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn 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.
-
-
- SSSSTTTTRRRRIIIINNNNGGGG OOOOPPPPEEEERRRRAAAATTTTIIIIOOOONNNNSSSS
- String values may be used as operands of the comparison operators,
- although the expression evaluator tries to do comparisons as integer or
- floating-point when it can. If one of the operands of a comparison is a
- string and the other has a numeric value, the numeric operand is
- converted back to a string using the C _s_p_r_i_n_t_f format specifier %%%%dddd for
- integers and %%%%gggg for floating-point values. For example, the commands
-
- eeeexxxxpppprrrr {{{{""""0000xxxx00003333"""" >>>> """"2222""""}}}}
- eeeexxxxpppprrrr {{{{""""0000yyyy"""" <<<< """"0000xxxx11112222""""}}}}
-
- 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 ======== when you really want string comparison and the |
- values of the operands could be arbitrary; it's better in these cases to|
- use the ssssttttrrrriiiinnnngggg ccccoooommmmppppaaaarrrreeee command instead.
-
-
- KKKKEEEEYYYYWWWWOOOORRRRDDDDSSSS
- arithmetic, boolean, compare, expression
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- PPPPaaaaggggeeee 5555
-
-
-
-