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 of the first argument with respect
94 to the second. Each number must have an integral value. Note
95 that Tcl defines this operation exactly even for negative num‐
96 bers, so that the following equality holds true:
97 (x / y) * y == x - (x % y)
98
99 ** ?number ...?
100 Returns the result of raising each value to the power of the
101 result of recursively operating on the result of processing the
102 following arguments, so “** 2 3 4” is the same as “** 2 [** 3
103 4]”. Each number may be any numeric value, though the second
104 number must not be fractional if the first is negative. If no
105 arguments are given, the result will be one, and if only one
106 argument is given, the result will be that argument. The result
107 will have an integral value only when all arguments are integral
108 values.
109
110 COMPARISON OPERATORS
111 The behaviors of the comparison operator commands (most of which oper‐
112 ate preferentially on numeric arguments) are as follows:
113
114 == ?arg ...?
115 Returns whether each argument is equal to the arguments on each
116 side of it in the sense of the expr == operator (i.e., numeric
117 comparison if possible, exact string comparison otherwise). If
118 fewer than two arguments are given, this operation always
119 returns a true value.
120
121 eq ?arg ...?
122 Returns whether each argument is equal to the arguments on each
123 side of it using exact string comparison. If fewer than two
124 arguments are given, this operation always returns a true value.
125
126 != arg arg
127 Returns whether the two arguments are not equal to each other,
128 in the sense of the expr != operator (i.e., numeric comparison
129 if possible, exact string comparison otherwise).
130
131 ne arg arg
132 Returns whether the two arguments are not equal to each other
133 using exact string comparison.
134
135 < ?arg ...?
136 Returns whether the arbitrarily-many arguments are ordered, with
137 each argument after the first having to be strictly more than
138 the one preceding it. Comparisons are performed preferentially
139 on the numeric values, and are otherwise performed using UNICODE
140 string comparison. If fewer than two arguments are present, this
141 operation always returns a true value. When the arguments are
142 numeric but should be compared as strings, the string compare
143 command should be used instead.
144
145 <= ?arg ...?
146 Returns whether the arbitrarily-many arguments are ordered, with
147 each argument after the first having to be equal to or 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 strictly less 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 equal to or 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 BIT-WISE OPERATORS
176 The behaviors of the bit-wise operator commands (all of which only
177 operate on integral arguments) are as follows:
178
179 ~ number
180 Returns the bit-wise negation of number. Number may be an inte‐
181 ger of any size. Note that the result of this operation will
182 always have the opposite sign to the input number.
183
184 & ?number ...?
185 Returns the bit-wise AND of each of the arbitrarily many argu‐
186 ments. Each number must have an integral value. If no arguments
187 are given, the result will be minus one.
188
189 | ?number ...?
190 Returns the bit-wise OR 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 zero.
193
194 ^ ?number ...?
195 Returns the bit-wise XOR 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 number
200 Returns the result of bit-wise shifting the first argument left
201 by the number of bits specified in the second argument. Each
202 number must have an integral value.
203
204 >> number number
205 Returns the result of bit-wise shifting the first argument right
206 by the number of bits specified in the second argument. Each
207 number must have an integral value.
208
209 LIST OPERATORS
210 The behaviors of the list-oriented operator commands are as follows:
211
212 in arg list
213 Returns whether the value arg is present in the list list
214 (according to exact string comparison of elements).
215
216 ni arg list
217 Returns whether the value arg is not present in the list list
218 (according to exact string comparison of elements).
219
221 The simplest way to use the operators is often by using namespace path
222 to make the commands available. This has the advantage of not affecting
223 the set of commands defined by the current namespace.
224 namespace path {::tcl::mathop ::tcl::mathfunc}
225
226 # Compute the sum of some numbers
227 set sum [+ 1 2 3]
228
229 # Compute the average of a list
230 set list {1 2 3 4 5 6}
231 set mean [/ [+ {*}$list] [double [llength $list]]]
232
233 # Test for list membership
234 set gotIt [in 3 $list]
235
236 # Test to see if a value is within some defined range
237 set inRange [<= 1 $x 5]
238
239 # Test to see if a list is sorted
240 set sorted [<= {*}$list]
241
243 expr(n), mathfunc(n), namespace(n)
244
246 command, expression, operator
247
248
249
250Tcl 8.5 mathop(n)