ZetCode

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.

basic_mathop.tcl
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.

bitwise_mathop.tcl
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.

comparison_mathop.tcl
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.

unary_mathop.tcl
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.

nested_mathop.tcl
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.

float_mathop.tcl
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

This tutorial covered the Tcl mathop command with practical examples showing the operator commands in different mathematical scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.