mathop(n)
๐ mathop(n) โ Tcl Mathematical Operator Commands ๐
okay bestie, pull up a chair ๐ช because we're diving into mathop โ the Tcl Mathematical Operator Commands. it's giving math but make it a whole command-line moment. this page is long, so it comes in 3 parts, and this is part 1. more parts follow, so don't sign off at the end. periodt. ๐
๐ NAME
mathop โ Mathematical operators as Tcl commands
๐ ๏ธ SYNOPSIS
First, the vibe check: Tcl 8.5- is required. no cap.
package require Tcl 8.5-
Then the entire operator squad arrives, each one as a ::tcl::mathop:: command. here's the exact roster, character for character, bestie:
::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 ...?
::tcl::mathop::le ?arg ...?
::tcl::mathop::gt ?arg ...?
::tcl::mathop::ge ?arg ...?
::tcl::mathop::in arg list
::tcl::mathop::ni arg list
bestie, these are the exact signatures. don't @ me, just copy them exactly. ๐ญ
That's the whole synopsis lineup for this part. part 2 is on its way, so stay tuned. โจ๐ฆ
โจ DESCRIPTION โจ
okay so bestie, the commands chillin' inside the ::tcl::mathop namespace do the exact same set of operations that the expr command supports. literally the same ones. they're all exported from the namespace, but not imported into any other namespace by default โ so you have to go get them yourself, they're not gonna just show up at your party uninvited.
and here's the tea that trips people up: renaming, reimplementing or deleting any of the commands in the namespace does not alter the way that the expr command behaves, and neither does defining any new commands in the ::tcl::mathop namespace. like, you can do whatever you want in there, expr is not watching, expr does not care. ๐
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, bestie:
!boolean โ returns the boolean negation of boolean, where boolean may be any numeric value or any other form of boolean value. i.e. it returns truth if the argument is falsity or zero, and falsity if the argument is truth or non-zero. so it's giving "flip the vibe" โจ+?number ...? โ returns the sum of arbitrarily many arguments. each number argument may be any numeric value. if no arguments are given, the result will be zero (the summation identity). yes, zero โ the empty sum is a whole mood. ๐ซถ-number ?number ...? โ if only a single number argument is given, returns the negation of that numeric value. otherwise returns the number that results 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.*?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). one is the moment. ๐/number ?number ...? โ if only a single number argument is given, returns the reciprocal of that numeric value (i.e. the value obtained by dividing1.0by that value). otherwise 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.
now the caveat, and it's a big one, so listen: 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 come crying to me about your decimals bestie, the integers ate them. ๐
%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.
and Tcl is actually iconic here: it 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 > 1 is268435455. 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. nesting powers like that is genuinely main character energy, no cap. ๐ฅ
โ๏ธ 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.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.<=?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.
ok real talk:<<=>>=are the numeric-preferred girlies andltlegtgeare the strictly UNICODE string ones. mix them up and you will be confused for hours. iykyk. ๐
๐ข 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. it's giving flip era. ๐ฆ&?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).
inandniare besties. one is the "you're in the group chat" and the other is the "you've been removed from the group chat". exact string comparison only, so no sneaky numeric magic here. ๐
โจ EXAMPLES โจ
okay bestie, listen โ the simplest way to use these operators is usually by setting up a namespace path so the commands are just... there. Available. Free. Like a good friend who always shows up ๐
and the extra slay part? It doesn't mess with the set of commands defined by the current namespace. So you get all the operator goodness with zero collateral damage. obsessed. โจ
namespace path {::tcl::mathop ::tcl::mathfunc}
like, that's the whole setup. one line. iconic. ๐
then you just... go off. here's the whole vibe board of examples, all together, exactly as they live in the wild:
# Compute the sum of some numbers
set sum [+ 1 2 3]
# Compute the average of a list
set list {1 2 3 4 5 6}
set mean [/ [+ {*}$list] [double [llength $list]]]
# Test for list membership
set gotIt [in 3 $list]
# Test to see if a value is within some defined range
set inRange [<= 1 $x 5]
# Test to see if a list is numerically sorted
set sorted [<= {*}$list]
# Test to see if a list is lexically sorted
set alphaList {a b c d e f}
set sorted [le {*}$alphaList]
let me walk you through the drama of each one, because the range of these is honestly giving range ๐
- sum of some numbers โ
set sum [+ 1 2 3]โ you're just adding. no ceremony. noexpr. just plus doing plus things. ๐ฅ - average of a list โ
set list {1 2 3 4 5 6}thenset mean [/ [+ {*}$list] [double [llength $list]]]โ you sum the whole list with+, expand it with{*}, count it withllength, and divide. it's giving main character energy, ngl. ๐ญ - test for list membership โ
set gotIt [in 3 $list]โ is3in the list?insays yes or no. simple. no notes. ๐ซถ - value within a defined range โ
set inRange [<= 1 $x 5]โ chained comparison!! is$xbetween1and5? one operator, whole range check. the way that just works... ๐ - numerically sorted? โ
set sorted [<= {*}$list]โ throw the whole list at<=and let it tell you if it's in numerical order. vibe check complete. โ - lexically sorted? โ
set alphaList {a b c d e f}thenset sorted [le {*}$alphaList]โ same deal but withlefor lexicographic (string) ordering. letters only, bestie. ๐
notice how<=handles the numeric one andlehandles the lexical one โ two different operators, two different sorts of "in order". don't mix them up, fr fr. ๐
๐ SEE ALSO ๐
go read these too, they're all part of the same friend group, iykyk ๐
๐ KEYWORDS ๐
commandexpressionoperator
three little words, whole personality. โจ
and that's the whole page, bestie!! this one came straight out of Tcl, version 8.5 โ yes, the 8.5 era, the one with the date field that says exactly that. so if you ever forget any of this, you know where to find it ๐ฆ go forth and [+ 1 2 3] your way through life, you absolute legend ๐๐
๐ฆ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 ยท TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 ยท TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 ยท TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 ยท TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- 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