Tcl mathop Command
last modified October 20, 2026
The Tcl ::tcl::mathop namespace provides the operators of
expr as ordinary commands. Calling + or
* directly is convenient when an operator is needed as a command,
for example + {*}$list to sum a list, and works well with list
commands such as lmap.
Basic Definition
There is no command named mathop. The operators live in the
::tcl::mathop namespace, and each one is a command that takes its
operands as arguments and returns the result. The commands accept any number of
operands where it makes sense.
Syntax: ::tcl::mathop::op ?arg ...?. The simplest way to use the
commands is to add the namespace to the command path with
namespace path ::tcl::mathop, which lets us write + 1 2
instead of the full name. The functions such as abs and
sqrt are in ::tcl::mathfunc, not in this namespace.
Basic Arithmetic Operations
This example demonstrates basic arithmetic operations with mathop.
namespace path ::tcl::mathop set a 10 set b 3 puts "Addition: [+ $a $b]" puts "Subtraction: [- $a $b]" puts "Multiplication: [* $a $b]" puts "Division: [/ $a $b]"
This shows addition, subtraction, multiplication, and division. The
namespace path command makes the operator commands available by
their short names. Dividing two integers gives an integer result (3).
Bitwise Operations
mathop supports various bitwise operations for integer values.
namespace path ::tcl::mathop set x 0b1010 set y 0b1100 puts "AND: [format %b [& $x $y]]" puts "OR: [format %b [| $x $y]]" puts "XOR: [format %b [^ $x $y]]" puts "Left shift: [format %b [<< $x 1]]"
This demonstrates bitwise AND, OR, XOR, and left shift operations. The
results are formatted as binary strings for clarity. The inputs are binary
literals (0b prefix), which Tcl 8.6 understands.
Comparison Operations
The comparison operators return boolean results.
namespace path ::tcl::mathop set val1 15 set val2 20 puts "Greater than: [> $val1 $val2]" puts "Less than: [< $val1 $val2]" puts "Equal: [== $val1 $val2]" puts "Not equal: [!= $val1 $val2]"
This shows numeric comparison operations. Each operation returns 1 for true
or 0 for false. The comparison commands also take more than two operands:
< 1 2 3 returns 1 because each operand is less than the next.
Unary Operations
Some operators work on a single argument.
namespace path ::tcl::mathop set num 5 puts "Negation: [- $num]" puts "Bitwise NOT: [~ $num]" puts "Logical NOT: [! 0]" puts "Unary plus: [+ $num]"
This demonstrates unary operations including negation, bitwise NOT, logical
NOT, and unary plus. Each operation is performed on a single argument.
Functions such as abs and sqrt belong to
::tcl::mathfunc.
Multiple Operations
The operator commands can be nested to perform multiple operations in sequence.
namespace path ::tcl::mathop set x 4 set y 2 set z 3 set result [+ [* $x $y] [/ $z $y]] puts "Result: $result"
This example shows nested operator commands. It calculates (x*y)+(z/y). The innermost operations are evaluated first. Because z and y are integers, z/y is 1, so the result is 9.
Floating-Point Operations
The operator commands handle floating-point numbers too.
namespace path ::tcl::mathop set pi 3.14159 set radius 2.5 set area [* $pi $radius $radius] puts "Circle area: $area"
This calculates the area of a circle using floating-point numbers. The formula
πr² is implemented with a single * call, because the multiplication
operator accepts any number of operands.
Best Practices
- Choice: Use the operator commands when an operator must be called like a command, for example with
{*}expansion orlmap. - Readability: For ordinary arithmetic, a braced
exprexpression is usually clearer. - Precision: Be aware of integer vs floating-point operations.
- Error handling: Check for division by zero and other edge cases.
- Namespace path: Set
namespace path ::tcl::mathopin the scope that needs it, or call the full name such as::tcl::mathop::+.
This tutorial covered the Tcl mathop command with practical
examples showing the operator commands in different mathematical scenarios.
Author
List all Tcl Tutorials.