Updated 2013-10-15 23:17:11 by pooryorick

How can I do math in Tcl discusses [expr] and friends.

See Also  edit

expr
a more detailed list of the functions that the man page skims over
expr problems with int
historical information regarding the previous long limitations of numbers in Tcl, and a discussion on wide.

Description  edit

Prior to 8.5, Tcl's built in math support was limited to what will fit into a long or double C variable type. To perform math on longer numbers, some alternative, such as Tom Poindexter's mpexpr extension, were needed.

DKF: From Tcl 8.4 onwards, you can also use a larger integer type termed a wide. This lets you work with 64-bit values on 32-bit machines...

[expr] is the primary tool for doing calculations in Tcl.

Unless you have a very good reason not to, you should always enclose the argument expression to [expr] in curly braces. This allows the bytecode compiler to optimise your code more thoroughly since it has fewer possible interpretations and the contents of variables and the results of commands will not unexpectedly modify the meaning of the expression.

AM A "very good reason" is that part or whole of the expression is not a constant, e.g.
set v [expr "$a $op $b"]

Surrounding this epression with braces would fail, since $a $op $b is not a properly formed expression.