1mathop(n)             Tcl Mathematical Operator Commands             mathop(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       mathop - Mathematical operators as Tcl commands
9

SYNOPSIS

11       package require Tcl 8.5
12
13       ::tcl::mathop::! number
14       ::tcl::mathop::~ number
15       ::tcl::mathop::+ ?number ...?
16       ::tcl::mathop::- number ?number ...?
17       ::tcl::mathop::* ?number ...?
18       ::tcl::mathop::/ number ?number ...?
19       ::tcl::mathop::% number number
20       ::tcl::mathop::** ?number ...?
21       ::tcl::mathop::& ?number ...?
22       ::tcl::mathop::| ?number ...?
23       ::tcl::mathop::^ ?number ...?
24       ::tcl::mathop::<< number number
25       ::tcl::mathop::>> number number
26       ::tcl::mathop::== ?arg ...?
27       ::tcl::mathop::!= arg arg
28       ::tcl::mathop::< ?arg ...?
29       ::tcl::mathop::<= ?arg ...?
30       ::tcl::mathop::>= ?arg ...?
31       ::tcl::mathop::> ?arg ...?
32       ::tcl::mathop::eq ?arg ...?
33       ::tcl::mathop::ne arg arg
34       ::tcl::mathop::in arg list
35       ::tcl::mathop::ni arg list
36
37______________________________________________________________________________
38

DESCRIPTION

40       The  commands  in the ::tcl::mathop namespace implement the same set of
41       operations as supported by the expr command. All are exported from  the
42       namespace,  but  are  not imported into any other namespace by default.
43       Note that renaming, reimplementing or deleting any of the  commands  in
44       the namespace does not alter the way that the expr command behaves, and
45       nor does defining any new commands in the ::tcl::mathop namespace.
46
47       The following operator commands are supported:
48
49              ~       !       +       -      *
50              /       %       **      &      |
51              ^       >>      <<      ==     eq
52              !=      ne      <       <=     >
53              >=      in      ni
54
55
56   MATHEMATICAL OPERATORS
57       The behaviors of the mathematical operator commands are as follows:
58
59       ! boolean
60              Returns the boolean negation of boolean, where  boolean  may  be
61              any  numeric  value  or any other form of boolean value (i.e. it
62              returns truth if the argument is falsity or zero, and falsity if
63              the argument is truth or non-zero).
64
65       + ?number ...?
66              Returns the sum of arbitrarily many arguments. Each number argu‐
67              ment may be any numeric value. If no arguments  are  given,  the
68              result will be zero (the summation identity).
69
70       - number ?number ...?
71              If  only a single number argument is given, returns the negation
72              of that numeric value. Otherwise returns the number that results
73              when all subsequent numeric values are subtracted from the first
74              one. All number arguments must be numeric values. At  least  one
75              argument must be given.
76
77       * ?number ...?
78              Returns  the  product of arbitrarily many arguments. Each number
79              may be any numeric value. If no arguments are given, the  result
80              will be one (the multiplicative identity).
81
82       / number ?number ...?
83              If  only a single number argument is given, returns the recipro‐
84              cal of that numeric value (i.e. the value obtained  by  dividing
85              1.0  by  that value).  Otherwise returns the number that results
86              when the first numeric argument is  divided  by  all  subsequent
87              numeric  arguments. All number arguments must be numeric values.
88              At least one argument must be given.
89
90              Note that when the leading values in the list of  arguments  are
91              integers,  integer division will be used for those initial steps
92              (i.e. the intermediate results will be as if the functions floor
93              and  int  are  applied to them, in that order). If all values in
94              the operation are integers, the result will be an integer.
95
96       % number number
97              Returns the integral modulus  (i.e.,  remainder)  of  the  first
98              argument  with  respect to the second.  Each number must have an
99              integral value.  Also, the sign of the result will be  the  same
100              as the sign of the second number, which must not be zero.
101
102              Note  that  Tcl defines this operation exactly even for negative
103              numbers, so that the following  command  returns  a  true  value
104              (omitting the namespace for clarity):
105
106                     == [* [/ x y] y] [- x [% x y]]
107
108       ** ?number ...?
109              Returns  the  result  of  raising each value to the power of the
110              result of recursively operating on the result of processing  the
111              following  arguments,  so  “** 2 3 4” is the same as “** 2 [** 3
112              4]”.  Each number may be any numeric value,  though  the  second
113              number  must  not  be  fractional if the first is negative.  The
114              maximum exponent value that Tcl can handle if the  first  number
115              is  an  integer > 1 is 268435455. If no arguments are given, the
116              result will be one, and if  only  one  argument  is  given,  the
117              result  will  be that argument. The result will have an integral
118              value only when all arguments are integral values.
119
120   COMPARISON OPERATORS
121       The behaviors of the comparison operator commands (most of which  oper‐
122       ate preferentially on numeric arguments) are as follows:
123
124       == ?arg ...?
125              Returns  whether each argument is equal to the arguments on each
126              side of it in the sense of the expr == operator  (i.e.,  numeric
127              comparison  if  possible, exact string comparison otherwise). If
128              fewer than  two  arguments  are  given,  this  operation  always
129              returns a true value.
130
131       eq ?arg ...?
132              Returns  whether each argument is equal to the arguments on each
133              side of it using exact string  comparison.  If  fewer  than  two
134              arguments are given, this operation always returns a true value.
135
136       != arg arg
137              Returns  whether  the two arguments are not equal to each other,
138              in the sense of the expr != operator (i.e.,  numeric  comparison
139              if possible, exact string comparison otherwise).
140
141       ne arg arg
142              Returns  whether  the  two arguments are not equal to each other
143              using exact string comparison.
144
145       < ?arg ...?
146              Returns whether the arbitrarily-many arguments are ordered, with
147              each  argument  after  the first having to be strictly more than
148              the one preceding it.  Comparisons are performed  preferentially
149              on the numeric values, and are otherwise performed using UNICODE
150              string comparison. If fewer than two arguments are present, this
151              operation  always  returns  a true value. When the arguments are
152              numeric but should be compared as strings,  the  string  compare
153              command should be used instead.
154
155       <= ?arg ...?
156              Returns whether the arbitrarily-many arguments are ordered, with
157              each argument after the first having to be equal to or more than
158              the  one preceding it.  Comparisons are performed preferentially
159              on the numeric values, and are otherwise performed using UNICODE
160              string comparison. If fewer than two arguments are present, this
161              operation always returns a true value. When  the  arguments  are
162              numeric  but  should  be compared as strings, the string compare
163              command should be used instead.
164
165       > ?arg ...?
166              Returns whether the arbitrarily-many arguments are ordered, with
167              each  argument  after  the first having to be strictly less than
168              the one preceding it.  Comparisons are performed  preferentially
169              on the numeric values, and are otherwise performed using UNICODE
170              string comparison. If fewer than two arguments are present, this
171              operation  always  returns  a true value. When the arguments are
172              numeric but should be compared as strings,  the  string  compare
173              command should be used instead.
174
175       >= ?arg ...?
176              Returns whether the arbitrarily-many arguments are ordered, with
177              each argument after the first having to be equal to or less than
178              the  one preceding it.  Comparisons are performed preferentially
179              on the numeric values, and are otherwise performed using UNICODE
180              string comparison. If fewer than two arguments are present, this
181              operation always returns a true value. When  the  arguments  are
182              numeric  but  should  be compared as strings, the string compare
183              command should be used instead.
184
185   BIT-WISE OPERATORS
186       The behaviors of the bit-wise operator  commands  (all  of  which  only
187       operate on integral arguments) are as follows:
188
189       ~ number
190              Returns  the bit-wise negation of number. Number may be an inte‐
191              ger of any size. Note that the result  of  this  operation  will
192              always have the opposite sign to the input number.
193
194       & ?number ...?
195              Returns  the  bit-wise AND of each of the arbitrarily many argu‐
196              ments. Each number must have an integral value. If no  arguments
197              are given, the result will be minus one.
198
199       | ?number ...?
200              Returns  the  bit-wise  OR of each of the arbitrarily many argu‐
201              ments. Each number must have an integral value. If no  arguments
202              are given, the result will be zero.
203
204       ^ ?number ...?
205              Returns  the  bit-wise XOR of each of the arbitrarily many argu‐
206              ments. Each number must have an integral value. If no  arguments
207              are given, the result will be zero.
208
209       << number number
210              Returns  the result of bit-wise shifting the first argument left
211              by the number of bits specified in  the  second  argument.  Each
212              number must have an integral value.
213
214       >> number number
215              Returns the result of bit-wise shifting the first argument right
216              by the number of bits specified in  the  second  argument.  Each
217              number must have an integral value.
218
219   LIST OPERATORS
220       The behaviors of the list-oriented operator commands are as follows:
221
222       in arg list
223              Returns  whether  the  value  arg  is  present  in the list list
224              (according to exact string comparison of elements).
225
226       ni arg list
227              Returns whether the value arg is not present in  the  list  list
228              (according to exact string comparison of elements).
229

EXAMPLES

231       The  simplest way to use the operators is often by using namespace path
232       to make the commands available. This has the advantage of not affecting
233       the set of commands defined by the current namespace.
234
235              namespace path {::tcl::mathop ::tcl::mathfunc}
236
237              # Compute the sum of some numbers
238              set sum [+ 1 2 3]
239
240              # Compute the average of a list
241              set list {1 2 3 4 5 6}
242              set mean [/ [+ {*}$list] [double [llength $list]]]
243
244              # Test for list membership
245              set gotIt [in 3 $list]
246
247              # Test to see if a value is within some defined range
248              set inRange [<= 1 $x 5]
249
250              # Test to see if a list is sorted
251              set sorted [<= {*}$list]
252

SEE ALSO

254       expr(n), mathfunc(n), namespace(n)
255

KEYWORDS

257       command, expression, operator
258
259
260
261Tcl                                   8.5                            mathop(n)
Impressum