mathop(3tcl)
π mathop(3tcl) β Tcl Mathematical Operator Commands π
okay so bestie, welcome to the mathop(3tcl) manual page β it's giving math operators but make them Tcl commands, and honestly? iconic. β¨
π NAME π
mathop β Mathematical operators as Tcl commands. That's literally it, bestie. The name is mathop, and it's all about mathematical operators as Tcl commands. Periodt. π
β¨ SYNOPSIS β¨
here's the whole call sheet, verbatim β no cap, every line kept exactly:
package require Tcl 8.5-
::tcl::mathop::! number
::tcl::mathop::~ number
::tcl::mathop::+ ?number ...?
::tcl::mathop::- number ?number ...?
::tcl::mathop::* ?number ...?
::tcl::mathop::/ number ?number ...?
::tcl::mathop::% number number
::tcl::mathop::** ?number ...?
::tcl::mathop::& ?number ...?
::tcl::mathop::| ?number ...?
::tcl::mathop::^ ?number ...?
::tcl::mathop::<< number number
::tcl::mathop::>> number number
::tcl::mathop::== ?arg ...?
::tcl::mathop::!= arg arg
::tcl::mathop::< ?arg ...?
::tcl::mathop::<= ?arg ...?
::tcl::mathop::>= ?arg ...?
::tcl::mathop::> ?arg ...?
::tcl::mathop::eq ?arg ...?
::tcl::mathop::ne arg arg
::tcl::mathop::lt ?arg ...? 2
::tcl::mathop::le ?arg ...? 2
::tcl::mathop::gt ?arg ...? 2
::tcl::mathop::ge ?arg ...? 2
::tcl::mathop::in arg list
::tcl::mathop::ni arg list
those lil 2s next to lt, le, gt, and ge? yeah, they were in the original layout too β kept them so nothing goes missing. π«Ά
β¨ DESCRIPTION β¨
okay so bestie, listen π the commands living inside the ::tcl::mathop namespace do the exact same set of operations that the expr command supports. That's the whole vibe. Iconic behavior, brand new address.
Every single one of them is exported from the namespace β but here's the tea π« β they're not imported into any other namespace by default. So you're gonna have to come get them yourself. No auto-delivery, no door dash, you walk.
β οΈ and this part is important, no cap: renaming, reimplementing, or deleting any of the commands in the namespace does not alter the way that theexprcommand behaves. And defining any brand new commands in the::tcl::mathopnamespace also does nothing toexpr. Those two are living separate lives.exprdoes not care what you do in there fr fr.
The following operator commands are supported:
~ ! + - *
/ % ** & |
^ >> << == eq
!= ne < <= >
>= in ni lt le
gt ge
πΈ MATHEMATICAL OPERATORS πΈ
The behaviors of the mathematical operator commands are as follows:
!boolean β gives you the boolean negation of boolean, where boolean can be any numeric value or literally any other form of boolean value. Translation: it returns truth if the argument is falsity or zero, and falsity if the argument is truth or non-zero. flip it and reverse it π+?number ...? β returns the sum of arbitrarily many arguments. Each number argument may be any numeric value. If you hand it no arguments at all, the result will be zero (the summation identity). empty sum, still slaying.-number ?number ...? β if you give it only a single number argument, it returns the negation of that numeric value. Otherwise it returns the number you get when all subsequent numeric values are subtracted from the first one. All number arguments must be numeric values. At least one argument must be given. one is the bare minimum, bestie π₯Ί*?number ...? β returns the product of arbitrarily many arguments. Each number may be any numeric value. If no arguments are given, the result will be one (the multiplicative identity). queen of starting from one π/number ?number ...? β if you give it only a single number argument, it returns the reciprocal of that numeric value (i.e. the value obtained by dividing1.0by that value). Otherwise it returns the number that results when the first numeric argument is divided by all subsequent numeric arguments. All number arguments must be numeric values. At least one argument must be given.
note bestie, this is the fine print and it matters: when the leading values in the list of arguments are integers, integer division will be used for those initial steps (i.e. the intermediate results will be as if the functions floor and int are applied to them, in that order). If all values in the operation are integers, the result will be an integer. so like, do not be shocked when your ints stay ints π
%number number β returns the integral modulus (i.e., remainder) of the first argument with respect to the second. Each number must have an integral value. Also, the sign of the result will be the same as the sign of the second number, which must not be zero. that zero? it is not invited. do not do it. π
Note that Tcl defines this operation exactly even for negative numbers, so that the following command returns a true value (omitting the namespace for clarity):
== [* [/ x y] y] [- x [% x y]]
**?number ...? β returns the result of raising each value to the power of the result of recursively operating on the result of processing the following arguments, so** 2 3 4is the same as** 2 [** 3 4]. Each number may be any numeric value, though the second number must not be fractional if the first is negative. The maximum exponent value that Tcl can handle if the first number is an integer> 1is268435455. If no arguments are given, the result will be one, and if only one argument is given, the result will be that argument. The result will have an integral value only when all arguments are integral values. nested power tower behavior, she's doing the most and we respect it π§
π COMPARISON OPERATORS π
The behaviors of the comparison operator commands (most of which operate preferentially on numeric arguments) are as follows:
==?arg ...? β returns whether each argument is equal to the arguments on each side of it in the sense of theexpr ==operator (i.e., numeric comparison if possible, exact string comparison otherwise). If fewer than two arguments are given, this operation always returns a true value. tiny input, big yes π«Άeq?arg ...? β returns whether each argument is equal to the arguments on each side of it using exact string comparison. If fewer than two arguments are given, this operation always returns a true value.!=arg arg β returns whether the two arguments are not equal to each other, in the sense of theexpr !=operator (i.e., numeric comparison if possible, exact string comparison otherwise).nearg arg β returns whether the two arguments are not equal to each other using exact string comparison.<?arg ...? β returns whether the arbitrarily-many arguments are ordered, with each argument after the first having to be strictly more than the one preceding it. Comparisons are performed preferentially on the numeric values, and are otherwise performed using UNICODE string comparison. If fewer than two arguments are present, this operation always returns a true value. When the arguments are numeric but should be compared as strings, theltoperator or thestring comparecommand should be used instead. iykyk π<=?arg ...? β returns whether the arbitrarily-many arguments are ordered, with each argument after the first having to be equal to or more than the one preceding it. Comparisons are performed preferentially on the numeric values, and are otherwise performed using UNICODE string comparison. If fewer than two arguments are present, this operation always returns a true value. When the arguments are numeric but should be compared as strings, theleoperator or thestring comparecommand should be used instead.>?arg ...? β returns whether the arbitrarily-many arguments are ordered, with each argument after the first having to be strictly less than the one preceding it. Comparisons are performed preferentially on the numeric values, and are otherwise performed using UNICODE string comparison. If fewer than two arguments are present, this operation always returns a true value. When the arguments are numeric but should be compared as strings, thegtoperator or thestring comparecommand should be used instead.>=?arg ...? β returns whether the arbitrarily-many arguments are ordered, with each argument after the first having to be equal to or less than the one preceding it. Comparisons are performed preferentially on the numeric values, and are otherwise performed using UNICODE string comparison. If fewer than two arguments are present, this operation always returns a true value. When the arguments are numeric but should be compared as strings, thegeoperator or thestring comparecommand should be used instead.lt?arg ...? β returns whether the arbitrarily-many arguments are ordered, with each argument after the first having to be strictly more than the one preceding it. Comparisons are performed using UNICODE string comparison. If fewer than two arguments are present, this operation always returns a true value.le?arg ...? β returns whether the arbitrarily-many arguments are ordered, with each argument after the first having to be equal to or strictly more than the one preceding it. Comparisons are performed using UNICODE string comparison. If fewer than two arguments are present, this operation always returns a true value.gt?arg ...? β returns whether the arbitrarily-many arguments are ordered, with each argument after the first having to be strictly less than the one preceding it. Comparisons are performed using UNICODE string comparison. If fewer than two arguments are present, this operation always returns a true value.ge?arg ...? β returns whether the arbitrarily-many arguments are ordered, with each argument after the first having to be equal to or strictly less than the one preceding it. Comparisons are performed using UNICODE string comparison. If fewer than two arguments are present, this operation always returns a true value.
π BIT-WISE OPERATORS π
The behaviors of the bit-wise operator commands (all of which only operate on integral arguments) are as follows:
~number β returns the bit-wise negation of number. Number may be an integer of any size. Note that the result of this operation will always have the opposite sign to the input number. she flips the sign AND the bits, double threat π&?number ...? β returns the bit-wise AND of each of the arbitrarily many arguments. Each number must have an integral value. If no arguments are given, the result will be minus one.|?number ...? β returns the bit-wise OR of each of the arbitrarily many arguments. Each number must have an integral value. If no arguments are given, the result will be zero.^?number ...? β returns the bit-wise XOR of each of the arbitrarily many arguments. Each number must have an integral value. If no arguments are given, the result will be zero.<<number number β returns the result of bit-wise shifting the first argument left by the number of bits specified in the second argument. Each number must have an integral value.>>number number β returns the result of bit-wise shifting the first argument right by the number of bits specified in the second argument. Each number must have an integral value.
π LIST OPERATORS π
The behaviors of the list-oriented operator commands are as follows:
inarg list β returns whether the value arg is present in the list list (according to exact string comparison of elements).niarg list β returns whether the value arg is not present in the list list (according to exact string comparison of elements).
okay bestie we are so not done yet, keep scrolling, the rest is coming πβ¨
β¨ EXAMPLES β¨
Okay bestie, here's the tea: the easiest way to get at all these operators is usually to just pull them in with namespace path so the commands are ~right there~. And the slay part? Doing it this way means you are not messing with the set of commands your current namespace defines. No side effects, no drama. π
namespace path {::tcl::mathop ::tcl::mathfunc}
Now let's actually cook. π³
Get you a sum β add some numbers together:
# Compute the sum of some numbers
set sum [+ 1 2 3]
Get you an average β grab the mean of a list:
# Compute the average of a list
set list {1 2 3 4 5 6}
set mean [/ [+ {*}$list] [double [llength $list]]]
Is it in the list tho β test for list membership:
# Test for list membership
set gotIt [in 3 $list]
Range check, we love boundaries β see if a value sits inside some defined range:
# Test to see if a value is within some defined range
set inRange [<= 1 $x 5]
Sorting allegations β check if a list is numerically sorted:
# Test to see if a list is numerically sorted
set sorted [<= {*}$list]
And the lexical version β check if a list is lexically sorted:
# Test to see if a list is lexically sorted
set alphaList {a b c d e f}
set sorted [le {*}$alphaList]
π SEE ALSO
Go read these next, bestie, they're the vibe-adjacent icons:
π KEYWORDS
Search terms so you can find this page again when it's 3am and you forgot everything:
commandexpressionoperator
and that's the whole page babes πβ¨ you are now officially certified in math operators, go be the main character of your own Tcl script π«Ά
This little gem ships with Tcl 8.5, filed under mathop(3tcl) in the manual β so go forth, slay, and remember: the operators were rooting for you the whole time. πππ¦β
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2006-2007 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms