OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [doc/] [expr.n] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
'\"
2
'\" Copyright (c) 1993 The Regents of the University of California.
3
'\" Copyright (c) 1994-1997 Sun Microsystems, Inc.
4
'\"
5
'\" See the file "license.terms" for information on usage and redistribution
6
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7
'\"
8
'\" RCS: @(#) $Id: expr.n,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $
9
'\"
10
.so man.macros
11
.TH expr n 8.0 Tcl "Tcl Built-In Commands"
12
.BS
13
'\" Note:  do not modify the .SH NAME line immediately below!
14
.SH NAME
15
expr \- Evaluate an expression
16
.SH SYNOPSIS
17
\fBexpr \fIarg \fR?\fIarg arg ...\fR?
18
.BE
19
 
20
.SH DESCRIPTION
21
.PP
22
Concatenates \fIarg\fR's (adding separator spaces between them),
23
evaluates the result as a Tcl expression, and returns the value.
24
The operators permitted in Tcl expressions are a subset of
25
the operators permitted in C expressions, and they have the
26
same meaning and precedence as the corresponding C operators.
27
Expressions almost always yield numeric results
28
(integer or floating-point values).
29
For example, the expression
30
.CS
31
\fBexpr 8.2 + 6\fR
32
.CE
33
evaluates to 14.2.
34
Tcl expressions differ from C expressions in the way that
35
operands are specified.  Also, Tcl expressions support
36
non-numeric operands and string comparisons.
37
.SH OPERANDS
38
.PP
39
A Tcl expression consists of a combination of operands, operators,
40
and parentheses.
41
White space may be used between the operands and operators and
42
parentheses; it is ignored by the expression's instructions.
43
Where possible, operands are interpreted as integer values.
44
Integer values may be specified in decimal (the normal case), in octal (if the
45
first character of the operand is \fB0\fR), or in hexadecimal (if the first
46
two characters of the operand are \fB0x\fR).
47
If an operand does not have one of the integer formats given
48
above, then it is treated as a floating-point number if that is
49
possible.  Floating-point numbers may be specified in any of the
50
ways accepted by an ANSI-compliant C compiler (except that the
51
\fBf\fR, \fBF\fR, \fBl\fR, and \fBL\fR suffixes will not be permitted in
52
most installations).  For example, all of the
53
following are valid floating-point numbers:  2.1, 3., 6e4, 7.91e+16.
54
If no numeric interpretation is possible, then an operand is left
55
as a string (and only a limited set of operators may be applied to
56
it).
57
.PP
58
Operands may be specified in any of the following ways:
59
.IP [1]
60
As an numeric value, either integer or floating-point.
61
.IP [2]
62
As a Tcl variable, using standard \fB$\fR notation.
63
The variable's value will be used as the operand.
64
.IP [3]
65
As a string enclosed in double-quotes.
66
The expression parser will perform backslash, variable, and
67
command substitutions on the information between the quotes,
68
and use the resulting value as the operand
69
.IP [4]
70
As a string enclosed in braces.
71
The characters between the open brace and matching close brace
72
will be used as the operand without any substitutions.
73
.IP [5]
74
As a Tcl command enclosed in brackets.
75
The command will be executed and its result will be used as
76
the operand.
77
.IP [6]
78
As a mathematical function whose arguments have any of the above
79
forms for operands, such as \fBsin($x)\fR.  See below for a list of defined
80
functions.
81
.LP
82
Where substitutions occur above (e.g. inside quoted strings), they
83
are performed by the expression's instructions.
84
However, an additional layer of substitution may already have
85
been performed by the command parser before the expression
86
processor was called.
87
As discussed below, it is usually best to enclose expressions
88
in braces to prevent the command parser from performing substitutions
89
on the contents.
90
.PP
91
For some examples of simple expressions, suppose the variable
92
\fBa\fR has the value 3 and
93
the variable \fBb\fR has the value 6.
94
Then the command on the left side of each of the lines below
95
will produce the value on the right side of the line:
96
.CS
97
.ta 6c
98
\fBexpr 3.1 + $a        6.1
99
expr 2 + "$a.$b"        5.6
100
expr 4*[llength "6 2"]  8
101
expr {{word one} < "word $a"}   0\fR
102
.CE
103
.SH OPERATORS
104
.PP
105
The valid operators are listed below, grouped in decreasing order
106
of precedence:
107
.TP 20
108
\fB\-\0\0+\0\0~\0\0!\fR
109
Unary minus, unary plus, bit-wise NOT, logical NOT.  None of these operands
110
may be applied to string operands, and bit-wise NOT may be
111
applied only to integers.
112
.TP 20
113
\fB*\0\0/\0\0%\fR
114
Multiply, divide, remainder.  None of these operands may be
115
applied to string operands, and remainder may be applied only
116
to integers.
117
The remainder will always have the same sign as the divisor and
118
an absolute value smaller than the divisor.
119
.TP 20
120
\fB+\0\0\-\fR
121
Add and subtract.  Valid for any numeric operands.
122
.TP 20
123
\fB<<\0\0>>\fR
124
Left and right shift.  Valid for integer operands only.
125
A right shift always propagates the sign bit.
126
.TP 20
127
\fB<\0\0>\0\0<=\0\0>=\fR
128
Boolean less, greater, less than or equal, and greater than or equal.
129
Each operator produces 1 if the condition is true, 0 otherwise.
130
These operators may be applied to strings as well as numeric operands,
131
in which case string comparison is used.
132
.TP 20
133
\fB==\0\0!=\fR
134
Boolean equal and not equal.  Each operator produces a zero/one result.
135
Valid for all operand types.
136
.TP 20
137
\fB&\fR
138
Bit-wise AND.  Valid for integer operands only.
139
.TP 20
140
\fB^\fR
141
Bit-wise exclusive OR.  Valid for integer operands only.
142
.TP 20
143
\fB|\fR
144
Bit-wise OR.  Valid for integer operands only.
145
.TP 20
146
\fB&&\fR
147
Logical AND.  Produces a 1 result if both operands are non-zero,
148
 
149
Valid for boolean and numeric (integers or floating-point) operands only.
150
.TP 20
151
\fB||\fR
152
Logical OR.  Produces a 0 result if both operands are zero, 1 otherwise.
153
Valid for boolean and numeric (integers or floating-point) operands only.
154
.TP 20
155
\fIx\fB?\fIy\fB:\fIz\fR
156
If-then-else, as in C.  If \fIx\fR
157
evaluates to non-zero, then the result is the value of \fIy\fR.
158
Otherwise the result is the value of \fIz\fR.
159
The \fIx\fR operand must have a numeric value.
160
.LP
161
See the C manual for more details on the results
162
produced by each operator.
163
All of the binary operators group left-to-right within the same
164
precedence level.  For example, the command
165
.CS
166
\fBexpr 4*2 < 7\fR
167
.CE
168
returns 0.
169
.PP
170
The \fB&&\fR, \fB||\fR, and \fB?:\fR operators have ``lazy
171
evaluation'', just as in C,
172
which means that operands are not evaluated if they are
173
not needed to determine the outcome.  For example, in the command
174
.CS
175
\fBexpr {$v ? [a] : [b]}\fR
176
.CE
177
only one of \fB[a]\fR or \fB[b]\fR will actually be evaluated,
178
depending on the value of \fB$v\fR.  Note, however, that this is
179
only true if the entire expression is enclosed in braces;  otherwise
180
the Tcl parser will evaluate both \fB[a]\fR and \fB[b]\fR before
181
invoking the \fBexpr\fR command.
182
.SH "MATH FUNCTIONS"
183
.PP
184
Tcl supports the following mathematical functions in expressions:
185
.DS
186
.ta 3c 6c 9c
187
\fBacos\fR      \fBcos\fR       \fBhypot\fR     \fBsinh\fR
188
\fBasin\fR      \fBcosh\fR      \fBlog\fR       \fBsqrt\fR
189
\fBatan\fR      \fBexp\fR       \fBlog10\fR     \fBtan\fR
190
\fBatan2\fR     \fBfloor\fR     \fBpow\fR       \fBtanh\fR
191
\fBceil\fR      \fBfmod\fR      \fBsin\fR
192
.DE
193
Each of these functions invokes the math library function of the same
194
name;  see the manual entries for the library functions for details
195
on what they do.  Tcl also implements the following functions for
196
conversion between integers and floating-point numbers and the
197
generation of random numbers:
198
.TP
199
\fBabs(\fIarg\fB)\fR
200
Returns the absolute value of \fIarg\fR.  \fIArg\fR may be either
201
integer or floating-point, and the result is returned in the same form.
202
.TP
203
\fBdouble(\fIarg\fB)\fR
204
If \fIarg\fR is a floating value, returns \fIarg\fR, otherwise converts
205
\fIarg\fR to floating and returns the converted value.
206
.TP
207
\fBint(\fIarg\fB)\fR
208
If \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts
209
\fIarg\fR to integer by truncation and returns the converted value.
210
.TP
211
\fBrand()\fR
212
Returns a floating point number from zero to just less than one or,
213
in mathematical terms, the range [0,1).  The seed comes from the
214
internal clock of the machine or may be set manual with the srand
215
function.
216
.TP
217
\fBround(\fIarg\fB)\fR
218
If \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts
219
\fIarg\fR to integer by rounding and returns the converted value.
220
.TP
221
\fBsrand(\fIarg\fB)\fR
222
The \fIarg\fR, which must be an integer, is used to reset the seed for
223
the random number generator.  Returns the first random number from
224
that seed.  Each interpreter has it's own seed.
225
.PP
226
In addition to these predefined functions, applications may
227
define additional functions using \fBTcl_CreateMathFunc\fR().
228
.SH "TYPES, OVERFLOW, AND PRECISION"
229
.PP
230
All internal computations involving integers are done with the C type
231
\fIlong\fR, and all internal computations involving floating-point are
232
done with the C type \fIdouble\fR.
233
When converting a string to floating-point, exponent overflow is
234
detected and results in a Tcl error.
235
For conversion to integer from string, detection of overflow depends
236
on the behavior of some routines in the local C library, so it should
237
be regarded as unreliable.
238
In any case, integer overflow and underflow are generally not detected
239
reliably for intermediate results.  Floating-point overflow and underflow
240
are detected to the degree supported by the hardware, which is generally
241
pretty reliable.
242
.PP
243
Conversion among internal representations for integer, floating-point,
244
and string operands is done automatically as needed.
245
For arithmetic computations, integers are used until some
246
floating-point number is introduced, after which floating-point is used.
247
For example,
248
.CS
249
\fBexpr 5 / 4\fR
250
.CE
251
returns 1, while
252
.CS
253
\fBexpr 5 / 4.0\fR
254
\fBexpr 5 / ( [string length "abcd"] + 0.0 )\fR
255
.CE
256
both return 1.25.
257
Floating-point values are always returned with a ``\fB.\fR''
258
or an \fBe\fR so that they will not look like integer values.  For
259
example,
260
.CS
261
\fBexpr 20.0/5.0\fR
262
.CE
263
returns \fB4.0\fR, not \fB4\fR.
264
 
265
.SH "STRING OPERATIONS"
266
.PP
267
String values may be used as operands of the comparison operators,
268
although the expression evaluator tries to do comparisons as integer
269
or floating-point when it can.
270
If one of the operands of a comparison is a string and the other
271
has a numeric value, the numeric operand is converted back to
272
a string using the C \fIsprintf\fR format specifier
273
\fB%d\fR for integers and \fB%g\fR for floating-point values.
274
For example, the commands
275
.CS
276
\fBexpr {"0x03" > "2"}\fR
277
\fBexpr {"0y" < "0x12"}\fR
278
.CE
279
both return 1.  The first comparison is done using integer
280
comparison, and the second is done using string comparison after
281
the second operand is converted to the string \fB18\fR.
282
Because of Tcl's tendency to treat values as numbers whenever
283
possible, it isn't generally a good idea to use operators like \fB==\fR
284
when you really want string comparison and the values of the
285
operands could be arbitrary;  it's better in these cases to use the
286
\fBstring compare\fR command instead.
287
 
288
.SH "PERFORMANCE CONSIDERATIONS"
289
.VS
290
.PP
291
Enclose expressions in braces for the best speed and the smallest
292
storage requirements.
293
This allows the Tcl bytecode compiler to generate the best code.
294
.PP
295
As mentioned above, expressions are substituted twice:
296
once by the Tcl parser and once by the \fBexpr\fR command.
297
For example, the commands
298
.CS
299
\fBset a 3\fR
300
\fBset b {$a + 2}\fR
301
\fBexpr $b*4\fR
302
.CE
303
return 11, not a multiple of 4.
304
This is because the Tcl parser will first substitute \fB$a + 2\fR for
305
the variable \fBb\fR,
306
then the \fBexpr\fR command will evaluate the expression \fB$a + 2*4\fR.
307
.PP
308
Most expressions do not require a second round of substitutions.
309
Either they are enclosed in braces or, if not,
310
their variable and command substitutions yield numbers or strings
311
that don't themselves require substitutions.
312
However, because a few unbraced expressions
313
need two rounds of substitutions,
314
the bytecode compiler must emit
315
additional instructions to handle this situation.
316
The most expensive code is required for
317
unbraced expressions that contain command substitutions.
318
These expressions must be implemented by generating new code
319
each time the expression is executed.
320
.VE
321
 
322
.SH KEYWORDS
323
arithmetic, boolean, compare, expression, fuzzy comparison

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.