Math Expressions; Backslash Substitution - Newport XPS-Q8 Manual

Universal high-performance motion controller/driver
Table of Contents

Advertisement

XPS-Q8
EDH0307En1041 — 10/17
2.2.4

Math Expressions

The Tcl interpreter itself does not evaluate math expressions. Tcl just performs
grouping, substitutions and command invocations. The
to parse and evaluate math expressions.
Example 1–4: Simple arithmetic.
expr
7.2
/
4
⇒ 1.8
The math syntax supported by
command deals with integer, floating point, and boolean values. Logical operations
return either 0 (false) or 1 (true). Integer values are promoted to floating point values as
needed. Octal values are indicated by a leading zero (e.g., 033 is 27 decimal).
Hexadecimal values are indicated by a leading 0x. Scientific notation for floating point
numbers is also supported. A summary of the operator precedence is provided on page
20.
You can include variable references and nested commands in math expressions.
The following example uses
foobar. As a result of the innermost command substitution, the
6
+ 7, and len gets the value 13:
Example 1–5: Nested commands.
set
x
7
set
len
[expr [string
⇒ 13
The expression evaluator supports a number of built-in math functions. Example 1–6
computes the value of pi:
Example 1–6: Built-in math functions.
set
pi
[expr
⇒ 3.1415926535897931
The implementation of expr is careful to preserve accurate numeric values and avoid
conversions between numbers and strings. However, you can make expr operate more
efficiently by grouping the entire expression in curly braces. The explanation has to do
with the byte code compiler that Tcl uses internally, the effects of which are explained
in more detail on page 15. For now, you should be aware that these expressions are all
valid and run a bit faster than the examples shown above:
Example 1–7: Grouping expressions with braces.
expr
{7.2
set
[expr {[string length
len
set
pi
[expr
2.2.5

Backslash Substitution

The final type of substitution done by the Tcl interpreter is backslash
substitution. This is used to quote characters that have special meaning to the
interpreter. For example, you can specify a literal dollar sign, brace, or bracket by
quoting it with a backslash. As a rule, however, if you find yourself using a lot of
backslashes, there is probably a simpler way to achieve the effect you are striving
for. In particular, the list command will do quoting for you automatically. In
Example 1–8 backslash is used to get a literal $:
expr
is the same as the C expression syntax. The expr
expr
to add the value of x to the length of the string
length foobar] + $x]
2*asin(1.0)]
/ 4}
{2*asin(1.0)}]
4
expr
command must be used
expr
foobar] + $x}]
Tcl Manual
command sees

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents