home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / u_man / cat3 / Tcl / expr.z / expr
Encoding:
Text File  |  1998-10-30  |  16.3 KB  |  331 lines

  1.  
  2.  
  3.  
  4. eeeexxxxpppprrrr((((3333TTTTccccllll))))                                                          eeeexxxxpppprrrr((((3333TTTTccccllll))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      expr - Evaluate an expression
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.      eeeexxxxpppprrrr _a_r_g ?_a_r_g _a_r_g ...?
  13.  
  14.  
  15. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  16.      Concatenates _a_r_g's (adding separator spaces between them), evaluates the |
  17.      result as a Tcl expression, and returns the value.  The operators
  18.      permitted in Tcl expressions are a subset of the operators permitted in C
  19.      expressions, and they have the same meaning and precedence as the
  20.      corresponding C operators.  Expressions almost always yield numeric
  21.      results (integer or floating-point values).  For example, the expression
  22.  
  23.           eeeexxxxpppprrrr 8888....2222 ++++ 6666
  24.  
  25.      evaluates to 14.2.  Tcl expressions differ from C expressions in the way
  26.      that operands are specified.  Also, Tcl expressions support non-numeric
  27.      operands and string comparisons.
  28.  
  29. OOOOPPPPEEEERRRRAAAANNNNDDDDSSSS
  30.      A Tcl expression consists of a combination of operands, operators, and
  31.      parentheses.  White space may be used between the operands and operators
  32.      and parentheses; it is ignored by the expression processor.  Where
  33.      possible, operands are interpreted as integer values.  Integer values may
  34.      be specified in decimal (the normal case), in octal (if the first
  35.      character of the operand is 0000), or in hexadecimal (if the first two
  36.      characters of the operand are 0000xxxx).  If an operand does not have one of
  37.      the integer formats given above, then it is treated as a floating-point
  38.      number if that is possible.  Floating-point numbers may be specified in
  39.      any of the ways accepted by an ANSI-compliant C compiler (except that the
  40.      ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in most
  41.      installations).  For example, all of the following are valid floating-
  42.      point numbers:  2.1, 3., 6e4, 7.91e+16.  If no numeric interpretation is
  43.      possible, then an operand is left as a string (and only a limited set of
  44.      operators may be applied to it).
  45.  
  46.      Operands may be specified in any of the following ways:
  47.  
  48.      [1]  As an numeric value, either integer or floating-point.
  49.  
  50.      [2]  As a Tcl variable, using standard $$$$ notation.  The variable's value
  51.           will be used as the operand.
  52.  
  53.      [3]  As a string enclosed in double-quotes.  The expression parser will
  54.           perform backslash, variable, and command substitutions on the
  55.           information between the quotes, and use the resulting value as the
  56.           operand
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. eeeexxxxpppprrrr((((3333TTTTccccllll))))                                                          eeeexxxxpppprrrr((((3333TTTTccccllll))))
  71.  
  72.  
  73.  
  74.      [4]  As a string enclosed in braces.  The characters between the open
  75.           brace and matching close brace will be used as the operand without
  76.           any substitutions.
  77.  
  78.      [5]  As a Tcl command enclosed in brackets.  The command will be executed
  79.           and its result will be used as the operand.
  80.  
  81.      [6]  As a mathematical function whose arguments have any of the above    |
  82.           forms for operands, such as ``ssssiiiinnnn(((($$$$xxxx))))''.  See below for a list of   |
  83.           defined functions.
  84.  
  85.      Where substitutions occur above (e.g. inside quoted strings), they are
  86.      performed by the expression processor.  However, an additional layer of
  87.      substitution may already have been performed by the command parser before
  88.      the expression processor was called.  As discussed below, it is usually
  89.      best to enclose expressions in braces to prevent the command parser from
  90.      performing substitutions on the contents.
  91.  
  92.      For some examples of simple expressions, suppose the variable aaaa has the
  93.      value 3 and the variable bbbb has the value 6.  Then the command on the left
  94.      side of each of the lines below will produce the value on the right side
  95.      of the line:
  96.  
  97.           eeeexxxxpppprrrr 3333....1111 ++++ $$$$aaaa           6666....1111
  98.           eeeexxxxpppprrrr 2222 ++++ """"$$$$aaaa....$$$$bbbb""""        5555....6666
  99.           eeeexxxxpppprrrr 4444****[[[[lllllllleeeennnnggggtttthhhh """"6666 2222""""]]]]  8888
  100.           eeeexxxxpppprrrr {{{{{{{{wwwwoooorrrrdddd oooonnnneeee}}}} <<<< """"wwwwoooorrrrdddd $$$$aaaa""""}}}}0000
  101.  
  102.  
  103. OOOOPPPPEEEERRRRAAAATTTTOOOORRRRSSSS
  104.      The valid operators are listed below, grouped in decreasing order of
  105.      precedence:
  106.  
  107.      ----  ++++  ~~~~  !!!!          Unary minus, unary plus, bit-wise NOT, logical NOT.  |
  108.                          None of these operands may be applied to string
  109.                          operands, and bit-wise NOT may be applied only to
  110.                          integers.
  111.  
  112.      ****  ////  %%%%             Multiply, divide, remainder.  None of these operands
  113.                          may be applied to string operands, and remainder may
  114.                          be applied only to integers.  The remainder will     |
  115.                          always have the same sign as the divisor and an      |
  116.                          absolute value smaller than the divisor.
  117.  
  118.      ++++  ----                Add and subtract.  Valid for any numeric operands.
  119.  
  120.      <<<<<<<<  >>>>>>>>              Left and right shift.  Valid for integer operands
  121.                          only.
  122.  
  123.      <<<<  >>>>  <<<<====  >>>>====        Boolean less, greater, less than or equal, and
  124.                          greater than or equal.  Each operator produces 1 if
  125.                          the condition is true, 0 otherwise.  These operators
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. eeeexxxxpppprrrr((((3333TTTTccccllll))))                                                          eeeexxxxpppprrrr((((3333TTTTccccllll))))
  137.  
  138.  
  139.  
  140.                          may be applied to strings as well as numeric
  141.                          operands, in which case string comparison is used.
  142.  
  143.      ========  !!!!====              Boolean equal and not equal.  Each operator produces
  144.                          a zero/one result.  Valid for all operand types.
  145.  
  146.      &&&&                   Bit-wise AND.  Valid for integer operands only.
  147.  
  148.      ^^^^                   Bit-wise exclusive OR.  Valid for integer operands
  149.                          only.
  150.  
  151.      ||||                   Bit-wise OR.  Valid for integer operands only.
  152.  
  153.      &&&&&&&&                  Logical AND.  Produces a 1 result if both operands
  154.                          are non-zero, 0 otherwise.  Valid for numeric
  155.                          operands only (integers or floating-point).
  156.  
  157.      ||||||||                  Logical OR.  Produces a 0 result if both operands are
  158.                          zero, 1 otherwise.  Valid for numeric operands only
  159.                          (integers or floating-point).
  160.  
  161.      _x????_y::::_z               If-then-else, as in C.  If _x evaluates to non-zero,
  162.                          then the result is the value of _y.  Otherwise the
  163.                          result is the value of _z.  The _x operand must have a
  164.                          numeric value.
  165.  
  166.      See the C manual for more details on the results produced by each
  167.      operator.  All of the binary operators group left-to-right within the
  168.      same precedence level.  For example, the command
  169.  
  170.           eeeexxxxpppprrrr 4444****2222 <<<< 7777
  171.  
  172.      returns 0.
  173.  
  174.      The &&&&&&&&, ||||||||, and ????:::: operators have ``lazy evaluation'', just as in C,
  175.      which means that operands are not evaluated if they are not needed to
  176.      determine the outcome.  For example, in the command
  177.  
  178.           eeeexxxxpppprrrr {{{{$$$$vvvv ???? [[[[aaaa]]]] :::: [[[[bbbb]]]]}}}}
  179.  
  180.      only one of [[[[aaaa]]]] or [[[[bbbb]]]] will actually be evaluated, depending on the value
  181.      of $$$$vvvv.  Note, however, that this is only true if the entire expression is
  182.      enclosed in braces;  otherwise the Tcl parser will evaluate both [[[[aaaa]]]] and
  183.      [[[[bbbb]]]] before invoking the eeeexxxxpppprrrr command.
  184.  
  185. MMMMAAAATTTTHHHH FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
  186.      Tcl supports the following mathematical functions in expressions:        |
  187.  
  188.           aaaaccccoooossss        ccccoooossss         hhhhyyyyppppooootttt      ssssiiiinnnnhhhh                             |
  189.           aaaassssiiiinnnn        ccccoooosssshhhh        lllloooogggg        ssssqqqqrrrrtttt                             |
  190.           aaaattttaaaannnn        eeeexxxxpppp         lllloooogggg11110000      ttttaaaannnn                              |
  191.           aaaattttaaaannnn2222       fffflllloooooooorrrr       ppppoooowwww        ttttaaaannnnhhhh                             |
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. eeeexxxxpppprrrr((((3333TTTTccccllll))))                                                          eeeexxxxpppprrrr((((3333TTTTccccllll))))
  203.  
  204.  
  205.  
  206.           cccceeeeiiiillll        ffffmmmmoooodddd        ssssiiiinnnn                                         |
  207.  
  208.      Each of these functions invokes the math library function of the same    |
  209.      name;  see the manual entries for the library functions for details on   |
  210.      what they do.  Tcl also implements the following functions for conversion|
  211.      between integers and floating-point numbers:
  212.  
  213.      aaaabbbbssss((((_a_r_g))))
  214.           Returns the absolute value of _a_r_g.  _A_r_g may be either integer or    |
  215.           floating-point, and the result is returned in the same form.
  216.  
  217.      ddddoooouuuubbbblllleeee((((_a_r_g))))
  218.           If _a_r_g is a floating value, returns _a_r_g, otherwise converts _a_r_g to  |
  219.           floating and returns the converted value.
  220.  
  221.      iiiinnnntttt((((_a_r_g))))
  222.           If _a_r_g is an integer value, returns _a_r_g, otherwise converts _a_r_g to  |
  223.           integer by truncation and returns the converted value.
  224.  
  225.      rrrroooouuuunnnndddd((((_a_r_g))))
  226.           If _a_r_g is an integer value, returns _a_r_g, otherwise converts _a_r_g to  |
  227.           integer by rounding and returns the converted value.                |
  228.  
  229.      In addition to these predefined functions, applications may define       |
  230.      additional functions using TTTTccccllll____CCCCrrrreeeeaaaatttteeeeMMMMaaaatttthhhhFFFFuuuunnnncccc().
  231.  
  232. TTTTYYYYPPPPEEEESSSS,,,, OOOOVVVVEEEERRRRFFFFLLLLOOOOWWWW,,,, AAAANNNNDDDD PPPPRRRREEEECCCCIIIISSSSIIIIOOOONNNN
  233.      All internal computations involving integers are done with the C type
  234.      _l_o_n_g, and all internal computations involving floating-point are done
  235.      with the C type _d_o_u_b_l_e.  When converting a string to floating-point,
  236.      exponent overflow is detected and results in a Tcl error.  For conversion
  237.      to integer from string, detection of overflow depends on the behavior of
  238.      some routines in the local C library, so it should be regarded as
  239.      unreliable.  In any case, integer overflow and underflow are generally
  240.      not detected reliably for intermediate results.  Floating-point overflow
  241.      and underflow are detected to the degree supported by the hardware, which
  242.      is generally pretty reliable.
  243.  
  244.      Conversion among internal representations for integer, floating-point,
  245.      and string operands is done automatically as needed.  For arithmetic
  246.      computations, integers are used until some floating-point number is
  247.      introduced, after which floating-point is used.  For example,
  248.  
  249.           eeeexxxxpppprrrr 5555 //// 4444
  250.  
  251.      returns 1, while
  252.  
  253.           eeeexxxxpppprrrr 5555 //// 4444....0000
  254.           eeeexxxxpppprrrr 5555 //// (((( [[[[ssssttttrrrriiiinnnngggg lllleeeennnnggggtttthhhh """"aaaabbbbccccdddd""""]]]] ++++ 0000....0000 ))))
  255.  
  256.      both return 1.25.  Floating-point values are always returned with a ``.''|
  257.      or an ``e'' so that they will not look like integer values.  For example,|
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. eeeexxxxpppprrrr((((3333TTTTccccllll))))                                                          eeeexxxxpppprrrr((((3333TTTTccccllll))))
  269.  
  270.  
  271.  
  272.           eeeexxxxpppprrrr 22220000....0000////5555....0000                                                       |
  273.  
  274.      returns ``4.0'', not ``4''.  The global variable ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn determines|
  275.      the the number of significant digits that are retained when floating     |
  276.      values are converted to strings (except that trailing zeroes are         |
  277.      omitted).  If ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn is unset then 6 digits of precision are used.|
  278.      To retain all of the significant bits of an IEEE floating-point number   |
  279.      set ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn to 17;  if a value is converted to string with 17      |
  280.      digits of precision and then converted back to binary for some later     |
  281.      calculation, the resulting binary value is guaranteed to be identical to |
  282.      the original one.
  283.  
  284.  
  285. SSSSTTTTRRRRIIIINNNNGGGG OOOOPPPPEEEERRRRAAAATTTTIIIIOOOONNNNSSSS
  286.      String values may be used as operands of the comparison operators,
  287.      although the expression evaluator tries to do comparisons as integer or
  288.      floating-point when it can.  If one of the operands of a comparison is a
  289.      string and the other has a numeric value, the numeric operand is
  290.      converted back to a string using the C _s_p_r_i_n_t_f format specifier %%%%dddd for
  291.      integers and %%%%gggg for floating-point values.  For example, the commands
  292.  
  293.           eeeexxxxpppprrrr {{{{""""0000xxxx00003333"""" >>>> """"2222""""}}}}
  294.           eeeexxxxpppprrrr {{{{""""0000yyyy"""" <<<< """"0000xxxx11112222""""}}}}
  295.  
  296.      both return 1.  The first comparison is done using integer comparison,
  297.      and the second is done using string comparison after the second operand
  298.      is converted to the string ``18''.  Because of Tcl's tendency to treat   |
  299.      values as numbers whenever possible, it isn't generally a good idea to   |
  300.      use operators like ======== when you really want string comparison and the     |
  301.      values of the operands could be arbitrary;  it's better in these cases to|
  302.      use the ssssttttrrrriiiinnnngggg ccccoooommmmppppaaaarrrreeee command instead.
  303.  
  304.  
  305. KKKKEEEEYYYYWWWWOOOORRRRDDDDSSSS
  306.      arithmetic, boolean, compare, expression
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.