mathop(n)

๐Ÿ“ฆ
version 9.0.3 ยท 3 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05

๐Ÿ’– 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 dividing 1.0 by 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 4 is 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 is 268435455. 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 the expr == 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 the expr != operator (i.e., numeric comparison if possible, exact string comparison otherwise).
  • ne arg 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, the lt operator or the string compare command 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, the le operator or the string compare command 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, the gt operator or the string compare command 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, the ge operator or the string compare command 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 and lt le gt ge are 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:

  • in arg list โ€” returns whether the value arg is present in the list list (according to exact string comparison of elements).
  • ni arg list โ€” returns whether the value arg is not present in the list list (according to exact string comparison of elements).
in and ni are 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. no expr. just plus doing plus things. ๐Ÿ”ฅ
  • average of a list โ€” set list {1 2 3 4 5 6} then set mean [/ [+ {*}$list] [double [llength $list]]] โ€” you sum the whole list with +, expand it with {*}, count it with llength, and divide. it's giving main character energy, ngl. ๐Ÿ˜ญ
  • test for list membership โ€” set gotIt [in 3 $list] โ€” is 3 in the list? in says yes or no. simple. no notes. ๐Ÿซถ
  • value within a defined range โ€” set inRange [<= 1 $x 5] โ€” chained comparison!! is $x between 1 and 5? 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} then set sorted [le {*}$alphaList] โ€” same deal but with le for lexicographic (string) ordering. letters only, bestie. ๐ŸŽ€
notice how <= handles the numeric one and le handles 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 ๐Ÿ”‘

  • command
  • expression
  • operator

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