ZetCode

Tcl mathop Command

last modified April 3, 2025

The Tcl mathop command provides efficient mathematical operations. It offers better performance than expr for simple operations. The command supports various arithmetic and bitwise operations. It's useful for performance-critical code sections.

Basic Definition

The mathop command performs mathematical operations on arguments. It evaluates operations directly without string parsing overhead. The command supports both binary and unary operations. It returns the result of the operation.

Syntax: mathop op ?arg ...?. The op specifies the operation to perform. Arguments are the values to operate on. The command supports all standard mathematical operations.

Basic Arithmetic Operations

This example demonstrates basic arithmetic operations with mathop.

basic_mathop.tcl
set a 10
set b 3

puts "Addition: [mathop + $a $b]"
puts "Subtraction: [mathop - $a $b]"
puts "Multiplication: [mathop * $a $b]"
puts "Division: [mathop / $a $b]"

This shows addition, subtraction, multiplication, and division. The mathop command performs each operation efficiently. Results are printed using puts.

Bitwise Operations

mathop supports various bitwise operations for integer values.

bitwise_mathop.tcl
set x 0b1010
set y 0b1100

puts "AND: [format %b [mathop & $x $y]]"
puts "OR: [format %b [mathop | $x $y]]"
puts "XOR: [format %b [mathop ^ $x $y]]"
puts "Left shift: [format %b [mathop << $x 1]]"

This demonstrates bitwise AND, OR, XOR, and left shift operations. The results are formatted as binary strings for clarity. mathop handles these operations efficiently.

Comparison Operations

mathop can perform comparison operations returning boolean results.

comparison_mathop.tcl
set val1 15
set val2 20

puts "Greater than: [mathop > $val1 $val2]"
puts "Less than: [mathop < $val1 $val2]"
puts "Equal: [mathop == $val1 $val2]"
puts "Not equal: [mathop != $val1 $val2]"

This shows numeric comparison operations. Each operation returns 1 for true or 0 for false. The results are printed directly to standard output.

Unary Operations

mathop supports several unary operations on single arguments.

unary_mathop.tcl
set num 5

puts "Negation: [mathop - $num]"
puts "Bitwise NOT: [mathop ~ $num]"
puts "Absolute value: [mathop abs -7]"
puts "Square root: [mathop sqrt 16]"

This demonstrates unary operations including negation, bitwise NOT, absolute value, and square root. Each operation is performed on a single argument. Results are printed immediately.

Multiple Operations

mathop can be nested to perform multiple operations in sequence.

nested_mathop.tcl
set x 4
set y 2
set z 3

set result [mathop + [mathop * $x $y] [mathop / $z $y]]
puts "Result: $result"

This example shows nested mathop commands. It calculates (x*y)+(z/y). The innermost operations are evaluated first. The final result is stored and printed.

Floating-Point Operations

mathop handles floating-point numbers with standard operations.

float_mathop.tcl
set pi 3.14159
set radius 2.5

set area [mathop * [mathop * $pi $radius] $radius]
puts "Circle area: $area"

This calculates the area of a circle using floating-point numbers. The formula πr² is implemented with nested mathop commands. The result is printed with full precision.

Best Practices

This tutorial covered the Tcl mathop command with practical examples showing its usage 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.