1mNAME0m
       expr - Evaluate an expression

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


1mDESCRIPTION0m
       Concatenates 4marg24ms (adding separator spaces between them), evaluates the
       result as a Tcl expression, and returns the value.  The operators  per-
       mitted  in Tcl expressions are a subset of the operators permitted in C
       expressions, and they have the same meaning and precedence as the  cor-
       responding  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 (note that all literal operands that  are  not  numeric  or
       boolean  must be quoted with either braces or with double quotes), then
       an operand is left as a string (and only a limited set of operators may
       be applied to it).

       On  32-bit  systems,  integer  values  MAX_INT (0x7FFFFFFF) and MIN_INT
       (-0x80000000) will be represented as 32-bit values, and integer  values
       outside  that  range  will  be represented as 64-bit values (if that is
       possible at all.)

       Operands may be specified in any of the following ways:

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

       [2]    As a boolean value, using  any  form  understood  by  1mstring  is0m
              1mboolean22m.

       [3]    As  a  Tcl  variable, using standard 1m$ 22mnotation.  The variable's
              value will be used as the operand.

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

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

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

       Where the above substitutions occur (e.g. inside quoted strings),  they
       are  performed  by the expression's instructions.  However, the command
       parser may already have performed one round 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, 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 operators may be  applied  to  string
                           operands,  and  bit-wise NOT may be applied only to
                           integers.

       1m*  /  %             22mMultiply, divide, remainder.  None of these  opera-
                           tors may be applied to string operands, and remain-
                           der 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.

       1meq  ne              22mBoolean string equal and string  not  equal.   Each
                           operator  produces  a zero/one result.  The operand
                           types are interpreted only as strings.

       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 boolean or 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, all
       of which work  solely  with  floating-point  numbers  unless  otherwise
       noted:

              1mabs         cosh        log        sqrt0m
              1macos        double      log10      srand0m
              1masin        exp         pow        tan0m
              1matan        floor       rand       tanh0m
              1matan2       fmod        round      wide0m
              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 [4m024m,4mpi24m] radians.  4mArg0m
              should be in the range [4m-124m,4m124m].

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

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

       1matan2(4m22my,24m 4mx24m1m)0m
              Returns  the  arc tangent of 4my24m/4mx24m, in the range [4m-pi24m,4mpi24m] radians.
              4mx24m and 4my24m cannot both be 0.  If 4mx24m  is  greater  than  4m024m,  this  is
              equivalent to 1matan(4m22my/x24m1m)22m.

       1mceil(4m22marg24m1m)0m
              Returns  the smallest integral floating-point value (i.e. with a
              zero fractional part) 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-point value, returns 4marg24m, otherwise con-
              verts 4marg24m to floating-point and returns the converted value.

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

       1mfloor(4m22marg24m1m)0m
              Returns  the  largest integral floating-point value (i.e. with a
              zero fractional part) 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
              1msqrt(4m22mx24m*4mx24m+4my24m*4my24m1m)22m.

       1mint(4m22marg24m1m)0m
              If 4marg24m is an integer value of the  same  width  as  the  machine
              word,  returns 4marg24m, otherwise converts 4marg24m to an integer (of the
              same size as a machine word, i.e. 32-bits on 32-bit systems, and
              64-bits  on  64-bit  systems) by truncation and returns the con-
              verted 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 pseudo-random floating-point value in the range (4m024m,4m124m).
              The generator algorithm is a simple linear congruential  genera-
              tor that is not cryptographically secure.  Each result from 1mrand0m
              completely determines all future results from  subsequent  calls
              to  1mrand22m,  so  1mrand 22mshould not be used to generate a sequence of
              secrets, such as one-time passwords.  The seed of the  generator
              is  initialized from the internal clock of the machine or may be
              set with the 1msrand 22mfunction.

       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 of 1mrand22m.  Returns the first random
              number (see 1mrand()22m) from that seed.  Each  interpreter  has  its
              own seed.

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

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

       1mwide(4m22marg24m1m)0m
              Converts 4marg24m to an integer value at least 64-bits wide (by sign-
              extension if 4marg24m is a 32-bit number) if it is not one already.

       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, except in the case of the 1meq 22mand 1mne 22mopera-
       tors.  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 4msprintf24m format specifier 1m%d 22mfor integers and 1m%g 22mfor  float-
       ing-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 1meq 22mor 1mne 22moperators, or 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.

1mEXAMPLES0m
       Define a procedure that computes an  "interesting"  mathematical  func-
       tion:
              proc calc {x y} {
                  1mexpr 22m{ ($x*$x - $y*$y) / exp($x*$x + $y*$y) }
              }

       Convert polar coordinates into cartesian coordinates:
              # convert from ($radius,$angle)
              set x [1mexpr 22m{ $radius * cos($angle) }]
              set y [1mexpr 22m{ $radius * sin($angle) }]

       Convert cartesian coordinates into polar coordinates:
              # convert from ($x,$y)
              set radius [1mexpr 22m{ hypot($y, $x) }]
              set angle  [1mexpr 22m{ atan2($y, $x) }]

       Print  a  message  describing  the relationship of two string values to
       each other:
              puts "a and b are [1mexpr 22m{$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 [1mexpr 22m{
                  [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 [1mexpr 22m{ int(100 * rand()) }]


1mSEE ALSO0m
       array(n), for(n), if(n), string(n), Tcl(n), while(n)


1mKEYWORDS0m
