1mNAME0m
       expr - Evaluate an expression

1mSYNOPSIS0m
       1mexpr 4m22marg24m ?4marg24m 4marg24m 4m...24m?


1mDESCRIPTION0m
       Concatenates  4marg24m'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  expres-
       sion
              1mexpr 8.2 + 60m
       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.

1mOPERANDS0m
       A  Tcl expression consists of a combination of operands, operators, and
       parentheses.  White space may be used between the operands  and  opera-
       tors  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 1m022m), or in hexadecimal (if  the  first
       two characters of the operand are 1m0x22m).  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 speci-
       fied in any of the  ways  accepted  by  an  ANSI-compliant  C  compiler
       (except  that the 1mf22m, 1mF22m, 1ml22m, and 1mL 22msuffixes 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 1m$ 22mnotation.  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 with-
              out any substitutions.

       [5]    As a Tcl command enclosed in brackets.  The command will be exe-
              cuted 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 1msin($x)22m.  See below for a list of
              defined functions.

       Where substitutions occur above (e.g. inside quoted strings), they  are
       performed  by  the  expression's  instructions.  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 com-
       mand parser from performing substitutions on the contents.

       For some examples of simple expressions, suppose the variable 1ma 22mhas the
       value 3 and the variable 1mb 22mhas 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:
              1mexpr 3.1 + $a           6.10m
              1mexpr 2 + "$a.$b"        5.60m
              1mexpr 4*[llength "6 2"]  80m
              1mexpr {{word one} < "word $a"}00m

1mOPERATORS0m
       The valid operators are listed below, grouped in  decreasing  order  of
       precedence:

       1m-  +  ~  !          22mUnary 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.

       1m*  /  %             22mMultiply,  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.

       1m+  -                22mAdd  and subtract.  Valid for any numeric operands.

       1m<<  >>              22mLeft and right shift.  Valid for  integer  operands
                           only.   A  right  shift  always propagates the sign
                           bit.

       1m<  >  <=  >=        22mBoolean less, greater,  less  than  or  equal,  and
                           greater than or equal.  Each operator produces 1 if
                           the condition is true, 0 otherwise.   These  opera-
                           tors  may  be applied to strings as well as numeric
                           operands, in which case string comparison is  used.

       1m==  !=              22mBoolean  equal  and  not equal.  Each operator pro-
                           duces a zero/one result.   Valid  for  all  operand
                           types.

       1m&                   22mBit-wise AND.  Valid for integer operands only.

       1m^                   22mBit-wise  exclusive OR.  Valid for integer operands
                           only.

       1m|                   22mBit-wise OR.  Valid for integer operands only.

       1m&&                  22mLogical AND.  Produces a 1 result if both  operands
                           are  non-zero,  0 otherwise.  Valid for boolean and
                           numeric (integers or floating-point) operands only.

       1m||                  22mLogical  OR.   Produces a 0 result if both operands
                           are zero,  1  otherwise.   Valid  for  boolean  and
                           numeric (integers or floating-point) operands only.

       4mx24m1m?4m22my24m1m:4m22mz24m               If-then-else, as in C.  If 4mx24m evaluates to non-zero,
                           then  the  result is the value of 4my24m.  Otherwise the
                           result is the value of 4mz24m.  The 4mx24m operand must  have
                           a numeric value.

       See the C manual for more details on the results produced by each oper-
       ator.  All of the binary operators group left-to-right within the  same
       precedence level.  For example, the command
              1mexpr 4*2 < 70m
       returns 0.

       The  1m&&22m,  1m||22m,  and 1m?: 22moperators 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
              1mexpr {$v ? [a] : [b]}0m
       only  one  of  1m[a]  22mor 1m[b] 22mwill actually be evaluated, depending on the
       value of 1m$v22m.  Note, however, that this  is  only  true  if  the  entire
       expression is enclosed in braces;  otherwise the Tcl parser will evalu-
       ate both 1m[a] 22mand 1m[b] 22mbefore invoking the 1mexpr 22mcommand.

1mMATH FUNCTIONS0m
       Tcl supports the following mathematical functions in expressions:

              1mabs         cosh        log        sqrt0m
              1macos        double      log10      srand0m
              1masin        exp         pow        tan0m
              1matan        floor       rand       tanh0m
              1matan2       fmod        round0m
              1mceil        hypot       sin0m
              1mcos         int         sinh0m



       1mabs(4m22marg24m1m)0m
              Returns the absolute value of 4marg24m.  4mArg24m may be either integer or
              floating-point, and the result is returned in the same form.

       1macos(4m22marg24m1m)0m
              Returns  the arc cosine of 4marg24m, in the range [0,pi] radians. 4mArg0m
              should be in the range [-1,1].

       1masin(4m22marg24m1m)0m
              Returns the arc sine of 4marg24m, in the range [-pi/2,pi/2]  radians.
              4mArg24m should be in the range [-1,1].

       1matan(4m22marg24m1m)0m
              Returns  the arc tangent of 4marg24m, in the range [-pi/2,pi/2] radi-
              ans.

       1matan2(4m22mx,24m 4my24m1m)0m
              Returns the arc tangent of 4my24m/4mx24m, in the range  [-pi,pi]  radians.
              4mx24m and 4my24m cannot both be 0.

       1mceil(4m22marg24m1m)0m
              Returns the smallest integer value not less than 4marg24m.

       1mcos(4m22marg24m1m)0m
              Returns the cosine of 4marg24m, measured in radians.

       1mcosh(4m22marg24m1m)0m
              Returns the hyperbolic cosine of 4marg24m.  If the result would cause
              an overflow, an error is returned.

       1mdouble(4m22marg24m1m)0m
              If 4marg24m is a floating value, returns 4marg24m, otherwise converts  4marg0m
              to floating and returns the converted value.

       1mexp(4m22marg24m1m)0m
              Returns  the  exponential  of  4marg24m,  defined  as e**4marg24m.  If the
              result would cause an overflow, an error is returned.

       1mfloor(4m22marg24m1m)0m
              Returns the largest integral value not greater than 4marg24m.

       1mfmod(4m22mx,24m 4my24m1m)0m
              Returns the floating-point remainder of the division of 4mx24m by  4my24m.
              If 4my24m is 0, an error is returned.

       1mhypot(4m22mx,24m 4my24m1m)0m
              Computes the length of the hypotenuse of a right-angled triangle
              (4mx24m*4mx24m+4my24m*4my24m).

       1mint(4m22marg24m1m)0m
              If 4marg24m is an integer value, returns 4marg24m, otherwise converts  4marg0m
              to integer by truncation and returns the converted value.

       1mlog(4m22marg24m1m)0m
              Returns  the  natural  logarithm of 4marg24m.  4mArg24m must be a positive
              value.

       1mlog10(4m22marg24m1m)0m
              Returns the base 10 logarithm of 4marg24m.  4mArg24m must  be  a  positive
              value.

       1mpow(4m22mx,24m 4my24m1m)0m
              Computes  the  value  of 4mx24m raised to the power 4my24m.  If 4mx24m is nega-
              tive, 4my24m must be an integer value.

       1mrand() 22mReturns a floating point number from zero to just less than  one
              or, in mathematical terms, the range [0,1).  The seed comes from
              the internal clock of the machine or may be set manual with  the
              srand function.

       1mround(4m22marg24m1m)0m
              If  4marg24m is an integer value, returns 4marg24m, otherwise converts 4marg0m
              to integer by rounding and returns the converted value.

       1msin(4m22marg24m1m)0m
              Returns the sine of 4marg24m, measured in radians.

       1msinh(4m22marg24m1m)0m
              Returns the hyperbolic sine of 4marg24m.  If the result  would  cause
              an overflow, an error is returned.

       1msqrt(4m22marg24m1m)0m
              Returns the square root of 4marg24m.  4mArg24m must be non-negative.

       1msrand(4m22marg24m1m)0m
              The 4marg24m, which must be an integer, is used to reset the seed for
              the random number generator.  Returns the  first  random  number
              from that seed.  Each interpreter has it's own seed.

       1mtan(4m22marg24m1m)0m
              Returns the tangent of 4marg24m, measured in radians.

       1mtanh(4m22marg24m1m)0m
              Returns the hyperbolic tangent of 4marg24m.

       In  addition  to  these  predefined  functions, applications may define
       additional functions using 1mTcl_CreateMathFunc22m().

1mTYPES, OVERFLOW, AND PRECISION0m
       All internal computations involving integers are done with the  C  type
       4mlong24m,  and  all internal computations involving floating-point are done
       with the C type 4mdouble24m.  When converting a  string  to  floating-point,
       exponent  overflow is detected and results in a Tcl error.  For conver-
       sion 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.  Float-
       ing-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,
              1mexpr 5 / 40m
       returns 1, while
              1mexpr 5 / 4.00m
              1mexpr 5 / ( [string length "abcd"] + 0.0 )0m
       both return 1.25.  Floating-point values are  always  returned  with  a
       ``1m.22m''   or  an  1me  22mso that they will not look like integer values.  For
       example,
              1mexpr 20.0/5.00m
       returns 1m4.022m, not 1m422m.


1mSTRING OPERATIONS0m
       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 con-
       verted back to a string using the C 4msprintf24m  format  specifier  1m%d  22mfor
       integers and 1m%g 22mfor floating-point values.  For example, the commands
              1mexpr {"0x03" > "2"}0m
              1mexpr {"0y" < "0x12"}0m
       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 1m1822m.  Because of Tcl's tendency to treat val-
       ues as numbers whenever possible, it isn't generally a good idea to use
       operators like 1m== 22mwhen you really want string comparison and the values
       of the operands could be arbitrary;  it's better in these cases to  use
       the 1mstring 22mcommand instead.


1mPERFORMANCE CONSIDERATIONS0m
       Enclose expressions in braces for the best speed and the smallest stor-
       age 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 1mexpr 22mcommand.  For example, the commands
              1mset a 30m
              1mset b {$a + 2}0m
              1mexpr $b*40m
       return 11, not a multiple of 4.  This is because the  Tcl  parser  will
       first  substitute 1m$a + 2 22mfor the variable 1mb22m, then the 1mexpr 22mcommand will
       evaluate the expression 1m$a + 2*422m.

       Most expressions do  not  require  a  second  round  of  substitutions.
       Either  they are enclosed in braces or, if not, their variable and com-
       mand substitutions yield  numbers  or  strings  that  don't  themselves
       require  substitutions.   However,  because  a few unbraced expressions
       need two rounds of substitutions, the bytecode compiler must emit addi-
       tional  instructions to handle this situation.  The most expensive code
       is required for unbraced expressions  that  contain  command  substitu-
       tions.   These  expressions  must be implemented by generating new code
       each time the expression is executed.


1mKEYWORDS0m
