How can I do math in Tcl

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

See Also

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.


expr is the primary tool for doing calculations in Tcl.

Prior to Tcl 8.5, built-in math operations were limited to numbers that could be stored in as a C long or double. To perform math on longer numbers an some alternative like TomPoindexter's mpexpr extension was 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...

Unless you have a very good reason not to, you should always pass a literal value to expr so that Tcl can parse compile and compile it just once.

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 because the literal value $a $op $b is not a syntactictally-correct expression.