| 1 |
.\" |
| 2 |
.\" Copyright (c) 2006-2007 Donal K. Fellows. |
| 3 |
.\" |
| 4 |
.\" See the file "license.terms" for information on usage and redistribution |
| 5 |
.\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
.\" |
| 7 |
.TH mathop n 8.5 Tcl "Tcl Mathematical Operator Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
.\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
mathop \- Mathematical operators as Tcl commands |
| 13 |
.SH SYNOPSIS |
| 14 |
package require \fBTcl 8.5\fR |
| 15 |
.sp |
| 16 |
\fB::tcl::mathop::!\fR \fInumber\fR |
| 17 |
.br |
| 18 |
\fB::tcl::mathop::~\fR \fInumber\fR |
| 19 |
.br |
| 20 |
\fB::tcl::mathop::+\fR ?\fInumber\fR ...? |
| 21 |
.br |
| 22 |
\fB::tcl::mathop::\-\fR \fInumber\fR ?\fInumber\fR ...? |
| 23 |
.br |
| 24 |
\fB::tcl::mathop::*\fR ?\fInumber\fR ...? |
| 25 |
.br |
| 26 |
\fB::tcl::mathop::/\fR \fInumber\fR ?\fInumber\fR ...? |
| 27 |
.br |
| 28 |
\fB::tcl::mathop::%\fR \fInumber number\fR |
| 29 |
.br |
| 30 |
\fB::tcl::mathop::**\fR ?\fInumber\fR ...? |
| 31 |
.br |
| 32 |
\fB::tcl::mathop::&\fR ?\fInumber\fR ...? |
| 33 |
.br |
| 34 |
\fB::tcl::mathop::|\fR ?\fInumber\fR ...? |
| 35 |
.br |
| 36 |
\fB::tcl::mathop::^\fR ?\fInumber\fR ...? |
| 37 |
.br |
| 38 |
\fB::tcl::mathop::<<\fR \fInumber number\fR |
| 39 |
.br |
| 40 |
\fB::tcl::mathop::>>\fR \fInumber number\fR |
| 41 |
.br |
| 42 |
\fB::tcl::mathop::==\fR ?\fIarg\fR ...? |
| 43 |
.br |
| 44 |
\fB::tcl::mathop::!=\fR \fIarg arg\fR |
| 45 |
.br |
| 46 |
\fB::tcl::mathop::<\fR ?\fIarg\fR ...? |
| 47 |
.br |
| 48 |
\fB::tcl::mathop::<=\fR ?\fIarg\fR ...? |
| 49 |
.br |
| 50 |
\fB::tcl::mathop::>=\fR ?\fIarg\fR ...? |
| 51 |
.br |
| 52 |
\fB::tcl::mathop::>\fR ?\fIarg\fR ...? |
| 53 |
.br |
| 54 |
\fB::tcl::mathop::eq\fR ?\fIarg\fR ...? |
| 55 |
.br |
| 56 |
\fB::tcl::mathop::ne\fR \fIarg arg\fR |
| 57 |
.br |
| 58 |
\fB::tcl::mathop::in\fR \fIarg list\fR |
| 59 |
.br |
| 60 |
\fB::tcl::mathop::ni\fR \fIarg list\fR |
| 61 |
.sp |
| 62 |
.BE |
| 63 |
.SH DESCRIPTION |
| 64 |
.PP |
| 65 |
The commands in the \fB::tcl::mathop\fR namespace implement the same set of |
| 66 |
operations as supported by the \fBexpr\fR command. All are exported from the |
| 67 |
namespace, but are not imported into any other namespace by default. Note that |
| 68 |
renaming, reimplementing or deleting any of the commands in the namespace does |
| 69 |
\fInot\fR alter the way that the \fBexpr\fR command behaves, and nor does |
| 70 |
defining any new commands in the \fB::tcl::mathop\fR namespace. |
| 71 |
.PP |
| 72 |
The following operator commands are supported: |
| 73 |
.DS |
| 74 |
.ta 2c 4c 6c 8c |
| 75 |
\fB~\fR \fB!\fR \fB+\fR \fB\-\fR \fB*\fR |
| 76 |
\fB/\fR \fB%\fR \fB**\fR \fB&\fR \fB|\fR |
| 77 |
\fB^\fR \fB>>\fR \fB<<\fR \fB==\fR \fBeq\fR |
| 78 |
\fB!=\fR \fBne\fR \fB<\fR \fB<=\fR \fB>\fR |
| 79 |
\fB>=\fR \fBin\fR \fBni\fR |
| 80 |
.DE |
| 81 |
.SS "MATHEMATICAL OPERATORS" |
| 82 |
.PP |
| 83 |
The behaviors of the mathematical operator commands are as follows: |
| 84 |
.TP |
| 85 |
\fB!\fR \fIboolean\fR |
| 86 |
. |
| 87 |
Returns the boolean negation of \fIboolean\fR, where \fIboolean\fR may be any |
| 88 |
numeric value or any other form of boolean value (i.e. it returns truth if the |
| 89 |
argument is falsity or zero, and falsity if the argument is truth or |
| 90 |
non-zero). |
| 91 |
.TP |
| 92 |
\fB+\fR ?\fInumber\fR ...? |
| 93 |
. |
| 94 |
Returns the sum of arbitrarily many arguments. Each \fInumber\fR argument may |
| 95 |
be any numeric value. If no arguments are given, the result will be zero (the |
| 96 |
summation identity). |
| 97 |
.TP |
| 98 |
\fB\-\fR \fInumber\fR ?\fInumber\fR ...? |
| 99 |
. |
| 100 |
If only a single \fInumber\fR argument is given, returns the negation of that |
| 101 |
numeric value. Otherwise returns the number that results when all subsequent |
| 102 |
numeric values are subtracted from the first one. All \fInumber\fR arguments |
| 103 |
must be numeric values. At least one argument must be given. |
| 104 |
.TP |
| 105 |
\fB*\fR ?\fInumber\fR ...? |
| 106 |
. |
| 107 |
Returns the product of arbitrarily many arguments. Each \fInumber\fR may be |
| 108 |
any numeric value. If no arguments are given, the result will be one (the |
| 109 |
multiplicative identity). |
| 110 |
.TP |
| 111 |
\fB/\fR \fInumber\fR ?\fInumber\fR ...? |
| 112 |
. |
| 113 |
If only a single \fInumber\fR argument is given, returns the reciprocal of that |
| 114 |
numeric value (i.e. the value obtained by dividing 1.0 by that value). |
| 115 |
Otherwise returns the number that results when the first numeric argument is |
| 116 |
divided by all subsequent numeric arguments. All \fInumber\fR arguments must |
| 117 |
be numeric values. At least one argument must be given. |
| 118 |
.RS |
| 119 |
.PP |
| 120 |
Note that when the leading values in the list of arguments are integers, |
| 121 |
integer division will be used for those initial steps (i.e. the intermediate |
| 122 |
results will be as if the functions \fIfloor\fR and \fIint\fR are applied to |
| 123 |
them, in that order). If all values in the operation are integers, the result |
| 124 |
will be an integer. |
| 125 |
.RE |
| 126 |
.TP |
| 127 |
\fB%\fR \fInumber number\fR |
| 128 |
. |
| 129 |
Returns the integral modulus (i.e., remainder) of the first argument |
| 130 |
with respect to the second. |
| 131 |
Each \fInumber\fR must have an integral value. |
| 132 |
Also, the sign of the result will be the same as the sign of the second |
| 133 |
\fInumber\fR, which must not be zero. |
| 134 |
.RS |
| 135 |
.PP |
| 136 |
Note that Tcl defines this operation exactly even for negative numbers, so |
| 137 |
that the following command returns a true value (omitting the namespace for |
| 138 |
clarity): |
| 139 |
.PP |
| 140 |
.CS |
| 141 |
\fB==\fR [\fB*\fR [\fB/\fI x y\fR] \fIy\fR] [\fB\-\fI x\fR [\fB%\fI x y\fR]] |
| 142 |
.CE |
| 143 |
.RE |
| 144 |
.TP |
| 145 |
\fB**\fR ?\fInumber\fR ...? |
| 146 |
. |
| 147 |
Returns the result of raising each value to the power of the result of |
| 148 |
recursively operating on the result of processing the following arguments, so |
| 149 |
.QW "\fB** 2 3 4\fR" |
| 150 |
is the same as |
| 151 |
.QW "\fB** 2 [** 3 4]\fR" . |
| 152 |
Each \fInumber\fR may be |
| 153 |
any numeric value, though the second number must not be fractional if the |
| 154 |
first is negative. The maximum exponent value that Tcl can handle if the |
| 155 |
first number is an integer > 1 is 268435455. If no arguments are given, |
| 156 |
the result will be one, and if only one argument is given, the result will |
| 157 |
be that argument. The result will have an integral value only when all |
| 158 |
arguments are integral values. |
| 159 |
.SS "COMPARISON OPERATORS" |
| 160 |
.PP |
| 161 |
The behaviors of the comparison operator commands (most of which operate |
| 162 |
preferentially on numeric arguments) are as follows: |
| 163 |
.TP |
| 164 |
\fB==\fR ?\fIarg\fR ...? |
| 165 |
. |
| 166 |
Returns whether each argument is equal to the arguments on each side of it in |
| 167 |
the sense of the \fBexpr\fR == operator (\fIi.e.\fR, numeric comparison if |
| 168 |
possible, exact string comparison otherwise). If fewer than two arguments |
| 169 |
are given, this operation always returns a true value. |
| 170 |
.TP |
| 171 |
\fBeq\fR ?\fIarg\fR ...? |
| 172 |
. |
| 173 |
Returns whether each argument is equal to the arguments on each side of it |
| 174 |
using exact string comparison. If fewer than two arguments are given, this |
| 175 |
operation always returns a true value. |
| 176 |
.TP |
| 177 |
\fB!=\fR \fIarg arg\fR |
| 178 |
. |
| 179 |
Returns whether the two arguments are not equal to each other, in the sense of |
| 180 |
the \fBexpr\fR != operator (\fIi.e.\fR, numeric comparison if possible, exact |
| 181 |
string comparison otherwise). |
| 182 |
.TP |
| 183 |
\fBne\fR \fIarg arg\fR |
| 184 |
. |
| 185 |
Returns whether the two arguments are not equal to each other using exact |
| 186 |
string comparison. |
| 187 |
.TP |
| 188 |
\fB<\fR ?\fIarg\fR ...? |
| 189 |
. |
| 190 |
Returns whether the arbitrarily-many arguments are ordered, with each argument |
| 191 |
after the first having to be strictly more than the one preceding it. |
| 192 |
Comparisons are performed preferentially on the numeric values, and are |
| 193 |
otherwise performed using UNICODE string comparison. If fewer than two |
| 194 |
arguments are present, this operation always returns a true value. When the |
| 195 |
arguments are numeric but should be compared as strings, the \fBstring |
| 196 |
compare\fR command should be used instead. |
| 197 |
.TP |
| 198 |
\fB<=\fR ?\fIarg\fR ...? |
| 199 |
. |
| 200 |
Returns whether the arbitrarily-many arguments are ordered, with each argument |
| 201 |
after the first having to be equal to or more than the one preceding it. |
| 202 |
Comparisons are performed preferentially on the numeric values, and are |
| 203 |
otherwise performed using UNICODE string comparison. If fewer than two |
| 204 |
arguments are present, this operation always returns a true value. When the |
| 205 |
arguments are numeric but should be compared as strings, the \fBstring |
| 206 |
compare\fR command should be used instead. |
| 207 |
.TP |
| 208 |
\fB>\fR ?\fIarg\fR ...? |
| 209 |
. |
| 210 |
Returns whether the arbitrarily-many arguments are ordered, with each argument |
| 211 |
after the first having to be strictly less than the one preceding it. |
| 212 |
Comparisons are performed preferentially on the numeric values, and are |
| 213 |
otherwise performed using UNICODE string comparison. If fewer than two |
| 214 |
arguments are present, this operation always returns a true value. When the |
| 215 |
arguments are numeric but should be compared as strings, the \fBstring |
| 216 |
compare\fR command should be used instead. |
| 217 |
.TP |
| 218 |
\fB>=\fR ?\fIarg\fR ...? |
| 219 |
. |
| 220 |
Returns whether the arbitrarily-many arguments are ordered, with each argument |
| 221 |
after the first having to be equal to or less than the one preceding it. |
| 222 |
Comparisons are performed preferentially on the numeric values, and are |
| 223 |
otherwise performed using UNICODE string comparison. If fewer than two |
| 224 |
arguments are present, this operation always returns a true value. When the |
| 225 |
arguments are numeric but should be compared as strings, the \fBstring |
| 226 |
compare\fR command should be used instead. |
| 227 |
.SS "BIT-WISE OPERATORS" |
| 228 |
.PP |
| 229 |
The behaviors of the bit-wise operator commands (all of which only operate on |
| 230 |
integral arguments) are as follows: |
| 231 |
.TP |
| 232 |
\fB~\fR \fInumber\fR |
| 233 |
. |
| 234 |
Returns the bit-wise negation of \fInumber\fR. \fINumber\fR may be an integer |
| 235 |
of any size. Note that the result of this operation will always have the |
| 236 |
opposite sign to the input \fInumber\fR. |
| 237 |
.TP |
| 238 |
\fB&\fR ?\fInumber\fR ...? |
| 239 |
. |
| 240 |
Returns the bit-wise AND of each of the arbitrarily many arguments. Each |
| 241 |
\fInumber\fR must have an integral value. If no arguments are given, the |
| 242 |
result will be minus one. |
| 243 |
.TP |
| 244 |
\fB|\fR ?\fInumber\fR ...? |
| 245 |
. |
| 246 |
Returns the bit-wise OR of each of the arbitrarily many arguments. Each |
| 247 |
\fInumber\fR must have an integral value. If no arguments are given, the |
| 248 |
result will be zero. |
| 249 |
.TP |
| 250 |
\fB^\fR ?\fInumber\fR ...? |
| 251 |
. |
| 252 |
Returns the bit-wise XOR of each of the arbitrarily many arguments. Each |
| 253 |
\fInumber\fR must have an integral value. If no arguments are given, the |
| 254 |
result will be zero. |
| 255 |
.TP |
| 256 |
\fB<<\fR \fInumber number\fR |
| 257 |
. |
| 258 |
Returns the result of bit-wise shifting the first argument left by the |
| 259 |
number of bits specified in the second argument. Each \fInumber\fR |
| 260 |
must have an integral value. |
| 261 |
.TP |
| 262 |
\fB>>\fR \fInumber number\fR |
| 263 |
. |
| 264 |
Returns the result of bit-wise shifting the first argument right by |
| 265 |
the number of bits specified in the second argument. Each \fInumber\fR |
| 266 |
must have an integral value. |
| 267 |
.SS "LIST OPERATORS" |
| 268 |
.PP |
| 269 |
The behaviors of the list-oriented operator commands are as follows: |
| 270 |
.TP |
| 271 |
\fBin\fR \fIarg list\fR |
| 272 |
. |
| 273 |
Returns whether the value \fIarg\fR is present in the list \fIlist\fR |
| 274 |
(according to exact string comparison of elements). |
| 275 |
.TP |
| 276 |
\fBni\fR \fIarg list\fR |
| 277 |
. |
| 278 |
Returns whether the value \fIarg\fR is not present in the list \fIlist\fR |
| 279 |
(according to exact string comparison of elements). |
| 280 |
.SH EXAMPLES |
| 281 |
.PP |
| 282 |
The simplest way to use the operators is often by using \fBnamespace path\fR |
| 283 |
to make the commands available. This has the advantage of not affecting the |
| 284 |
set of commands defined by the current namespace. |
| 285 |
.PP |
| 286 |
.CS |
| 287 |
namespace path {\fB::tcl::mathop\fR ::tcl::mathfunc} |
| 288 |
|
| 289 |
\fI# Compute the sum of some numbers\fR |
| 290 |
set sum [\fB+\fR 1 2 3] |
| 291 |
|
| 292 |
\fI# Compute the average of a list\fR |
| 293 |
set list {1 2 3 4 5 6} |
| 294 |
set mean [\fB/\fR [\fB+\fR {*}$list] [double [llength $list]]] |
| 295 |
|
| 296 |
\fI# Test for list membership\fR |
| 297 |
set gotIt [\fBin\fR 3 $list] |
| 298 |
|
| 299 |
\fI# Test to see if a value is within some defined range\fR |
| 300 |
set inRange [\fB<=\fR 1 $x 5] |
| 301 |
|
| 302 |
\fI# Test to see if a list is sorted\fR |
| 303 |
set sorted [\fB<=\fR {*}$list] |
| 304 |
.CE |
| 305 |
.SH "SEE ALSO" |
| 306 |
expr(n), mathfunc(n), namespace(n) |
| 307 |
.SH KEYWORDS |
| 308 |
command, expression, operator |
| 309 |
'\" Local Variables: |
| 310 |
'\" mode: nroff |
| 311 |
'\" End: |