1mathop(n) Tcl Mathematical Operator Commands mathop(n)
2
3
4
5______________________________________________________________________________
6
8 mathop - Mathematical operators as Tcl commands
9
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
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 ^ >> << == eq != ne < <= >
50 >= in ni
51
52 MATHEMATICAL OPERATORS
53 The behaviors of the mathematical operator commands are as follows:
54
55 ! boolean
56 Returns the boolean negation of boolean, where boolean may be
57 any numeric value or any other form of boolean value (i.e. it
58 returns truth if the argument is falsity or zero, and falsity if
59 the argument is truth or non-zero).
60
61 + ?number ...?
62 Returns the sum of arbitrarily many arguments. Each number argu‐
63 ment may be any numeric value. If no arguments are given, the
64 result will be zero (the summation identity).
65
66 - number ?number ...?
67 If only a single number argument is given, returns the negation
68 of that numeric value. Otherwise returns the number that results
69 when all subsequent numeric values are subtracted from the first
70 one. All number arguments must be numeric values. At least one
71 argument must be given.
72
73 * ?number ...?
74 Returns the product of arbitrarily many arguments. Each number
75 may be any numeric value. If no arguments are given, the result
76 will be one (the multiplicative identity).
77
78 / number ?number ...?
79 If only a single number argument is given, returns the recipro‐
80 cal of that numeric value (i.e. the value obtained by dividing
81 1.0 by that value). Otherwise returns the number that results
82 when the first numeric argument is divided by all subsequent
83 numeric arguments. All number arguments must be numeric values.
84 At least one argument must be given.
85
86 Note that when the leading values in the list of arguments are
87 integers, integer division will be used for those initial steps
88 (i.e. the intermediate results will be as if the functions floor
89 and int are applied to them, in that order). If all values in
90 the operation are integers, the result will be an integer.
91
92 % number number
93 Returns the integral modulus (i.e., remainder) of the first
94 argument with respect to the second. Each number must have an
95 integral value. Also, the sign of the result will be the same
96 as the sign of the second number, which must not be zero.
97
98 Note that Tcl defines this operation exactly even for negative
99 numbers, so that the following command returns a true value
100 (omitting the namespace for clarity):
101
102 == [* [/ x y] y] [- x [% x y]]
103
104 ** ?number ...?
105 Returns the result of raising each value to the power of the
106 result of recursively operating on the result of processing the
107 following arguments, so “** 2 3 4” is the same as “** 2 [** 3
108 4]”. Each number may be any numeric value, though the second
109 number must not be fractional if the first is negative. If no
110 arguments are given, the result will be one, and if only one
111 argument is given, the result will be that argument. The result
112 will have an integral value only when all arguments are integral
113 values.
114
115 COMPARISON OPERATORS
116 The behaviors of the comparison operator commands (most of which oper‐
117 ate preferentially on numeric arguments) are as follows:
118
119 == ?arg ...?
120 Returns whether each argument is equal to the arguments on each
121 side of it in the sense of the expr == operator (i.e., numeric
122 comparison if possible, exact string comparison otherwise). If
123 fewer than two arguments are given, this operation always
124 returns a true value.
125
126 eq ?arg ...?
127 Returns whether each argument is equal to the arguments on each
128 side of it using exact string comparison. If fewer than two
129 arguments are given, this operation always returns a true value.
130
131 != arg arg
132 Returns whether the two arguments are not equal to each other,
133 in the sense of the expr != operator (i.e., numeric comparison
134 if possible, exact string comparison otherwise).
135
136 ne arg arg
137 Returns whether the two arguments are not equal to each other
138 using exact string comparison.
139
140 < ?arg ...?
141 Returns whether the arbitrarily-many arguments are ordered, with
142 each argument after the first having to be strictly more than
143 the one preceding it. Comparisons are performed preferentially
144 on the numeric values, and are otherwise performed using UNICODE
145 string comparison. If fewer than two arguments are present, this
146 operation always returns a true value. When the arguments are
147 numeric but should be compared as strings, the string compare
148 command should be used instead.
149
150 <= ?arg ...?
151 Returns whether the arbitrarily-many arguments are ordered, with
152 each argument after the first having to be equal to or more than
153 the one preceding it. Comparisons are performed preferentially
154 on the numeric values, and are otherwise performed using UNICODE
155 string comparison. If fewer than two arguments are present, this
156 operation always returns a true value. When the arguments are
157 numeric but should be compared as strings, the string compare
158 command should be used instead.
159
160 > ?arg ...?
161 Returns whether the arbitrarily-many arguments are ordered, with
162 each argument after the first having to be strictly less than
163 the one preceding it. Comparisons are performed preferentially
164 on the numeric values, and are otherwise performed using UNICODE
165 string comparison. If fewer than two arguments are present, this
166 operation always returns a true value. When the arguments are
167 numeric but should be compared as strings, the string compare
168 command should be used instead.
169
170 >= ?arg ...?
171 Returns whether the arbitrarily-many arguments are ordered, with
172 each argument after the first having to be equal to or less than
173 the one preceding it. Comparisons are performed preferentially
174 on the numeric values, and are otherwise performed using UNICODE
175 string comparison. If fewer than two arguments are present, this
176 operation always returns a true value. When the arguments are
177 numeric but should be compared as strings, the string compare
178 command should be used instead.
179
180 BIT-WISE OPERATORS
181 The behaviors of the bit-wise operator commands (all of which only
182 operate on integral arguments) are as follows:
183
184 ~ number
185 Returns the bit-wise negation of number. Number may be an inte‐
186 ger of any size. Note that the result of this operation will
187 always have the opposite sign to the input number.
188
189 & ?number ...?
190 Returns the bit-wise AND of each of the arbitrarily many argu‐
191 ments. Each number must have an integral value. If no arguments
192 are given, the result will be minus one.
193
194 | ?number ...?
195 Returns the bit-wise OR 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 zero.
198
199 ^ ?number ...?
200 Returns the bit-wise XOR 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 number
205 Returns the result of bit-wise shifting the first argument left
206 by the number of bits specified in the second argument. Each
207 number must have an integral value.
208
209 >> number number
210 Returns the result of bit-wise shifting the first argument right
211 by the number of bits specified in the second argument. Each
212 number must have an integral value.
213
214 LIST OPERATORS
215 The behaviors of the list-oriented operator commands are as follows:
216
217 in arg list
218 Returns whether the value arg is present in the list list
219 (according to exact string comparison of elements).
220
221 ni arg list
222 Returns whether the value arg is not present in the list list
223 (according to exact string comparison of elements).
224
226 The simplest way to use the operators is often by using namespace path
227 to make the commands available. This has the advantage of not affecting
228 the set of commands defined by the current namespace.
229 namespace path {::tcl::mathop ::tcl::mathfunc}
230
231 # Compute the sum of some numbers
232 set sum [+ 1 2 3]
233
234 # Compute the average of a list
235 set list {1 2 3 4 5 6}
236 set mean [/ [+ {*}$list] [double [llength $list]]]
237
238 # Test for list membership
239 set gotIt [in 3 $list]
240
241 # Test to see if a value is within some defined range
242 set inRange [<= 1 $x 5]
243
244 # Test to see if a list is sorted
245 set sorted [<= {*}$list]
246
248 expr(n), mathfunc(n), namespace(n)
249
251 command, expression, operator
252
253
254
255Tcl 8.5 mathop(n)