-->
Page 509
tan(x) | Tangent of x (x in radians) |
tanh(x) | Hyperbolic tangent of x |
atan(x) | Arctangent of x (-pi/2 to pi/2) |
exp(x) | e raised to the power of x |
log(x) | Natural log of x |
log10(x) | Log base 10 of x |
sqrt(x) | The square root of x |
The following math function takes two number arguments:
pow(x,y) | x raised to the power of y |
This is used as follows:
set a 2; set b [expr pow($a,3)]; puts $b;
The output will be 8.0, the value of 2 raised to the third power.
Quoting and substitution are both used heavily in relation to variables. You saw the most basic version of quoting (using double quotes to make strings) and substitution earlier in this chapter. tcl supports one more type of quoting, brace quoting, and one more type of substitution, command substitution.
To review, the most common use of double quotes is to create strings with embedded whitespace:
set kiwi "Fresh from New Zealand";
Double quotes can also be used to make multiline strings:
set kiwi "Fresh from New Zealand 3 for a dollar";
In addition to making multiline strings, the standard ANSI C language escape sequences can be used in tcl strings:
set kiwi "Fresh from New Zealand\n\t3 for a dollar."
This outputs the following:
Fresh from New Zealand 3 for a dollar.
In addition to this, the two types of substitution can be applied within double-quoted strings. The first type of substitution, variable substitution, is explained in the "Variables" section, earlier in this chapter. In a double-quoted string, you can access the value of a variable by preceding the variable's name with $. Thus the following commands
Page 510
set fruit kiwi; set place "New Zealand"; set how_many 3; puts "$fruit, fresh from $place, $how_many for a dollar";
output this:
kiwi, fresh from New Zealand, 3 for a dollar
The other type of substitution is command substitution. A command substitution block begins with a left bracket ([) and ends with a right bracket (]). For example,
set len_in 2; puts "$len_in inches is [expr $len_in*2.54] cm";
outputs
2 inches is 5.08 cm
The 5.08 is the result of the command
expr $len_in*2.54
Because this command is in brackets, the value that it returns is substituted in. In this case, the tcl command expr is used, but any tcl command can be placed between brackets. Command substitution can be used in most commands, and is not limited to double-quoted commands. For example, the commands
set len_in 2; set len_cm [expr $len_in*2.54]; puts "$len_in inches is $len_cm cm";
produce the same output as
set len_in 2; puts "$len_in inches is [expr $len_in*2.54] cm";
The other type of quoting available in tcl is brace quoting, which is similar to the quoting using single quotes in UNIX shells. Brace quoting creates a string with the given characters, no substitution (command or variable) takes place, and the C language escape sequences are not interpreted. For example, the command
puts "This\nis a\nmulti-line\nstring"
produces the following output:
This is a multi-line string
The command
puts {This\nis a\nmulti-line\nstring}
produces the following output:
This\nis a\nmulti-line\nstring
Page 511
To get tabs, newlines, and other special characters in a brace-quoted string, they must be entered physically:
puts {This is a multi-line string}
This will produce the desired output. The real use for brace-quoted strings comes when
certain characters with special meanings need to be given as values for variables. For example, the
commands
set price 1.00; puts "Pears, $$price per pound";
output
Pears, $1.00 per pound
but the $$price has the potential to be confusing, so it would be better if the variable price had the value $1.00. You could use brace quoting to achieve the following:
set price {$1.00}; puts "Pears, $price per pound";
The other use of brace quoting is to defer evaluation and is used in control structures and procedure definitions. In such cases, the values of variables are substituted in after the entire block is read.
tcl provides several commands for flow control and supports all the standard ANSI C comparison operators for both string and numeric data.
This section starts with the if/elseif/else commands. The simplest if statement is one like the following:
if {$x < 0} { set x 10; }
This example only has one line in the body of the if clause, but any number of lines and subblocks can be added. If additional tests need to be performed, each test is given in parentheses as follows:
if { ($x == "SJ") || ($x == "LA") } { puts "You Live in California!"; }
Tests can be nested as in the following example:
if { ( ($arch == "ppc") || ($arch == "intel") ) && ($os != "Linux") } { puts "Get Linux!"; }