Numbers

part of Tcl for Web Nerds by Hal Abelson, Philip Greenspun, and Lydia Sandon; updated July 2011
Arithmetic is not done directly by the Tcl interpreter. It is done by calling the C library using the expr command on arthmetic expressions. The detailed parsing rules for arithmetic expressions depend on the particular Unix implementation, but they are more or less like in C.

Here are some examples:

# integer division truncates
% expr 7 / 2
3

# the percent sign is used to compute integer remainder
% expr 7%2 

# floating point propagates
% expr 7.0 / 2
3.5

% expr sin(.5)+cos(.9)
1.10103550687

% # a zero in front of number means to interpret as octal
% expr 017 + 01
16

% # a 0x in front means to interpret as hex
% expr 0xA + 1
11

% # numbers can be treated like strings!
% string length 100.34
6
% string range 100.34 0 2
100
More: See http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm.

Reference

Here are the numeric functions included in Tcl. (Details may vary depending on your Unix implementation of expr.)

Continue on to Control structure
Return to Table of Contents

lsandon@alum.mit.edu
Add a comment | Add a link