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. If no
114              arguments are given, the result will be one,  and  if  only  one
115              argument  is given, the result will be that argument. The result
116              will have an integral value only when all arguments are integral
117              values.
118
119   COMPARISON OPERATORS
120       The  behaviors of the comparison operator commands (most of which oper‐
121       ate preferentially on numeric arguments) are as follows:
122
123       == ?arg ...?
124              Returns whether each argument is equal to the arguments on  each
125              side  of  it in the sense of the expr == operator (i.e., numeric
126              comparison if possible, exact string comparison  otherwise).  If
127              fewer  than  two  arguments  are  given,  this  operation always
128              returns a true value.
129
130       eq ?arg ...?
131              Returns whether each argument is equal to the arguments on  each
132              side  of  it  using  exact  string comparison. If fewer than two
133              arguments are given, this operation always returns a true value.
134
135       != arg arg
136              Returns whether the two arguments are not equal to  each  other,
137              in  the  sense of the expr != operator (i.e., numeric comparison
138              if possible, exact string comparison otherwise).
139
140       ne arg arg
141              Returns whether the two arguments are not equal  to  each  other
142              using exact string comparison.
143
144       < ?arg ...?
145              Returns whether the arbitrarily-many arguments are ordered, with
146              each argument after the first having to be  strictly  more  than
147              the  one preceding it.  Comparisons are performed preferentially
148              on the numeric values, and are otherwise performed using UNICODE
149              string comparison. If fewer than two arguments are present, this
150              operation always returns a true value. When  the  arguments  are
151              numeric  but  should  be compared as strings, the string compare
152              command should be used instead.
153
154       <= ?arg ...?
155              Returns whether the arbitrarily-many arguments are ordered, with
156              each argument after the first having to be equal to or more than
157              the one preceding it.  Comparisons are performed  preferentially
158              on the numeric values, and are otherwise performed using UNICODE
159              string comparison. If fewer than two arguments are present, this
160              operation  always  returns  a true value. When the arguments are
161              numeric but should be compared as strings,  the  string  compare
162              command should be used instead.
163
164       > ?arg ...?
165              Returns whether the arbitrarily-many arguments are ordered, with
166              each argument after the first having to be  strictly  less  than
167              the  one preceding it.  Comparisons are performed preferentially
168              on the numeric values, and are otherwise performed using UNICODE
169              string comparison. If fewer than two arguments are present, this
170              operation always returns a true value. When  the  arguments  are
171              numeric  but  should  be compared as strings, the string compare
172              command should be used instead.
173
174       >= ?arg ...?
175              Returns whether the arbitrarily-many arguments are ordered, with
176              each argument after the first having to be equal to or less than
177              the one preceding it.  Comparisons are performed  preferentially
178              on the numeric values, and are otherwise performed using UNICODE
179              string comparison. If fewer than two arguments are present, this
180              operation  always  returns  a true value. When the arguments are
181              numeric but should be compared as strings,  the  string  compare
182              command should be used instead.
183
184   BIT-WISE OPERATORS
185       The  behaviors  of  the  bit-wise  operator commands (all of which only
186       operate on integral arguments) are as follows:
187
188       ~ number
189              Returns the bit-wise negation of number. Number may be an  inte‐
190              ger  of  any  size.  Note that the result of this operation will
191              always have the opposite sign to the input number.
192
193       & ?number ...?
194              Returns the bit-wise AND of each of the arbitrarily  many  argu‐
195              ments.  Each number must have an integral value. If no arguments
196              are given, the result will be minus one.
197
198       | ?number ...?
199              Returns the bit-wise OR of each of the  arbitrarily  many  argu‐
200              ments.  Each number must have an integral value. If no arguments
201              are given, the result will be zero.
202
203       ^ ?number ...?
204              Returns the bit-wise XOR of each of the arbitrarily  many  argu‐
205              ments.  Each number must have an integral value. If no arguments
206              are given, the result will be zero.
207
208       << number number
209              Returns the result of bit-wise shifting the first argument  left
210              by  the  number  of  bits specified in the second argument. Each
211              number must have an integral value.
212
213       >> number number
214              Returns the result of bit-wise shifting the first argument right
215              by  the  number  of  bits specified in the second argument. Each
216              number must have an integral value.
217
218   LIST OPERATORS
219       The behaviors of the list-oriented operator commands are as follows:
220
221       in arg list
222              Returns whether the value  arg  is  present  in  the  list  list
223              (according to exact string comparison of elements).
224
225       ni arg list
226              Returns  whether  the  value arg is not present in the list list
227              (according to exact string comparison of elements).
228

EXAMPLES

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

SEE ALSO

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

KEYWORDS

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