Seth Woolley's Man Viewer

Manual for expr - man n expr

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

expr(1,3,n)(n)                      Tcl Built-In Commands                     expr(1,3,n)(n)

       expr(1,3,n) - Evaluate an expression

       expr(1,3,n) arg ?arg arg ...?

       Concatenates  arg's  (adding  separator spaces between them), evaluates
       the result as a Tcl expression, and returns the value.   The  operators
       permitted in(1,8) Tcl expressions are a subset of the operators permitted in(1,8)
       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  expr(1,3,n)  8.2  +  6 evaluates to 14.2.  Tcl expressions differ from C
       expressions in(1,8) the way that operands are specified.  Also, Tcl  expres-
       sions support non-numeric operands and string(3,n) comparisons.

       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(1,8) decimal (the normal case), in(1,8) octal (if(3,n) the
       first character of the operand is 0), or in(1,8) hexadecimal (if(3,n)  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(3,n) that is possible.  Floating-point numbers may be speci-
       fied in(1,8) 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(1,8) 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(3,n) (and  only  a  limited
       set(7,n,1 builtins) 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(3,n) that is
       possible at all.)

       Operands may be specified in(1,8) 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(3,n)  enclosed in(1,8) 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(3,n) enclosed in(1,8) braces.  The characters between the open(2,3,n)
              brace and matching close(2,7,n) brace will be used as the operand with-
              out any substitutions.

       [5]    As a Tcl command enclosed in(1,8) 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 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'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(1,8) braces to prevent the com-
       mand 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(1,3,n)  3.1  +  $a           6.1  expr(1,3,n)  2  +
       "$a.$b"        5.6 expr(1,3,n) 4*[llength "6 2"]  8 expr(1,3,n) {{word one}  <  "word

       The  valid  operators  are listed below, grouped in(1,8) decreasing order of

       -  +  ~  !          Unary minus, unary plus, bit-wise NOT, logical NOT.
                           None of these operands may be applied to string(3,n) op-
                           erands, and bit-wise NOT may  be  applied  only  to

       *  /  %             Multiply,  divide,  remainder.  None of these oper-
                           ands may be applied to string(3,n) 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.

       +  -                Add  and subtract.  Valid for any numeric operands.

       <<  >>              Left and right shift.  Valid for  integer  operands
                           only.   A  right  shift  always propagates the sign

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

       ==  !=              Boolean  equal  and  not equal.  Each operator pro-
                           duces a zero/one result.   Valid  for  all  operand

       eq  ne              Boolean  string(3,n)  equal  and string(3,n) not equal.  Each
                           operator produces a zero/one result.   The  operand
                           types are interpreted only as strings.

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

       ^                   Bit-wise  exclusive OR.  Valid for integer operands

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

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

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

       x?y:z               If-then-else, as in(1,8) 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 oper-
       ator.  All of the binary operators group left-to-right within the  same
       precedence level.  For example, the command expr(1,3,n) 4*2 < 7 returns 0.

       The  &&,  ||,  and ?: operators have ``lazy evaluation'', just as in(1,8) C,
       which means that operands are not evaluated if(3,n) they are not  needed  to
       determine  the  outcome.   For example, in(1,8) the command expr(1,3,n) {$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(3,n) the entire
       expression is enclosed in(1,8) braces;  otherwise the Tcl parser will evalu-
       ate both [a] and [b] before invoking the expr(1,3,n) command.

       Tcl  supports  the following mathematical functions in(1,8) expressions, all
       of which work  solely  with  floating-point  numbers  unless  otherwise
       noted:     abs         cosh        log        sqrt     acos        dou-
       ble      log10      srand        asin        exp         pow        tan
       atan        floor       rand(1,3)       tanh
       atan2       fmod        round      wide     ceil        hypot       sin
       cos         int         sinh

              Returns the absolute value of arg.  Arg may be either integer or
              floating-point, and the result is returned in(1,8) the same form.

              Returns the arc cosine of arg, in(1,8) the range [0,pi] radians.  Arg
              should be in(1,8) the range [-1,1].

              Returns  the arc sine of arg, in(1,8) the range [-pi/2,pi/2] radians.
              Arg should be in(1,8) the range [-1,1].

              Returns the arc tangent of arg, in(1,8) the range [-pi/2,pi/2]  radi-

       atan2(y, x)
              Returns  the  arc tangent of y/x, in(1,8) the range [-pi,pi] radians.
              x and y cannot both be 0.  If x  is  greater  than  0,  this  is
              equivalent to atan(y/x).

              Returns  the smallest integral floating-point value (i.e. with a
              zero fractional part) not less(1,3) than arg.

              Returns the cosine of arg, measured in(1,8) radians.

              Returns the hyperbolic cosine of arg.  If the result would cause
              an overflow, an error(8,n) is returned.

              If  arg  is  a floating-point value, returns arg, otherwise con-
              verts arg to floating-point and returns the converted value.

              Returns the exponential of  arg,  defined  as  e**arg.   If  the
              result would cause an overflow, an error(8,n) is returned.

              Returns  the  largest integral floating-point value (i.e. with a
              zero fractional part) not greater than arg.

       fmod(x, y)
              Returns the floating-point remainder of the division of x by  y.
              If y is 0, an error(8,n) is returned.

       hypot(x, y)
              Computes the length of the hypotenuse of a right-angled triangle

              If arg is an integer value of the  same  width  as  the  machine
              word,  returns arg, otherwise converts arg 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.

              Returns the natural logarithm of arg.  Arg must  be  a  positive

              Returns  the  base  10 logarithm of arg.  Arg must be a positive

       pow(x, y)
              Computes the value of x raised to the power y.  If  x  is  nega-
              tive, y must be an integer value.

       rand(1,3)() Returns a pseudo-random floating-point value in(1,8) the range (0,1).
              The generator algorithm is a simple linear congruential  genera-
              tor that is not cryptographically secure.  Each result from rand(1,3)
              completely determines all future results from  subsequent  calls
              to  rand(1,3),  so  rand(1,3) should 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(3,n) of the machine or may be
              set(7,n,1 builtins) with the srand function.

              If arg is an integer value, returns arg, otherwise converts  arg
              to integer by rounding and returns the converted value.

              Returns the sine of arg, measured in(1,8) radians.

              Returns  the  hyperbolic sine of arg.  If the result would cause
              an overflow, an error(8,n) is returned.

              Returns the square root of arg.  Arg must be non-negative.

              The arg, which must be an integer, is used to reset(1,7,1 tput) the seed for
              the  random(3,4,6)  number generator of rand(1,3).  Returns the first random(3,4,6)
              number (see rand(1,3)()) from that seed.  Each  interpreter  has  its
              own seed.

              Returns the tangent of arg, measured in(1,8) radians.

              Returns the hyperbolic tangent of arg.

              Converts arg to an integer value at least 64-bits wide (by sign-
              extension if(3,n) arg is a 32-bit number) if(3,n) it is not one already.

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

       All  internal  computations involving integers are done with the C type
       long, and all internal computations involving floating-point  are  done
       with  the  C  type double.  When converting a string(3,n) to floating-point,
       exponent overflow is detected and results in(1,8) a Tcl error.  For  conver-
       sion  to  integer  from  string(3,n),  detection  of overflow depends on the
       behavior of some routines in(1,8) 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(3,n)  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(1,3,n) 5 /
       4 returns 1, while expr(1,3,n) 5 / 4.0 expr(1,3,n) 5 / ( [string(3,n) 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(1,8,3 Search::Dict)  like  integer  values.   For
       example, expr(1,3,n) 20.0/5.0 returns 4.0, not 4.

       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(1,8) the case of the eq and ne opera-
       tors.  If one of the operands of a comparison is a string(3,n) and the other
       has  a numeric value, the numeric operand is converted back to a string(3,n)
       using the C sprintf format specifier %d for integers and %g for  float-
       ing-point  values.   For example, the commands expr(1,3,n) {"0x03" > "2"} expr(1,3,n)
       {"0y" < "0x12"} both return 1.  The  first  comparison  is  done  using
       integer  comparison,  and  the  second  is done using string(3,n) comparison
       after the second operand is converted to the  string(3,n)  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(3,n)  comparison  and  the values of the operands could be arbitrary;
       it's better in(1,8) these cases to use the eq or ne operators, or the string(3,n)
       command instead.

       Enclose expressions in(1,8) 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 expr(1,3,n) command.  For example, the commands set(7,n,1 builtins) a 3
       set(7,n,1 builtins)  b  {$a  +  2}  expr(1,3,n)  $b*4 return 11, not a multiple of 4.  This is
       because the Tcl parser will first substitute $a + 2 for the variable b,
       then the expr(1,3,n) command will evaluate the expression $a + 2*4.

       Most  expressions  do  not  require  a  second  round of substitutions.
       Either they are enclosed in(1,8) braces or, if(3,n) 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(1,2,n) the expression is executed.

       array(n), string(3,n)(n), Tcl(n)

       arithmetic, boolean, compare, expression, fuzzy comparison

Tcl                                   8.4                              expr(1,3,n)(n)

References for this manual (incoming links)