Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / man / mann / expr.n
CommitLineData
86530b38
AT
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-2000 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.10.2.2 2004/10/27 09:35:38 dkf Exp $
9'\"
10'\" The definitions below are for supplemental macros used in Tcl/Tk
11'\" manual entries.
12'\"
13'\" .AP type name in/out ?indent?
14'\" Start paragraph describing an argument to a library procedure.
15'\" type is type of argument (int, etc.), in/out is either "in", "out",
16'\" or "in/out" to describe whether procedure reads or modifies arg,
17'\" and indent is equivalent to second arg of .IP (shouldn't ever be
18'\" needed; use .AS below instead)
19'\"
20'\" .AS ?type? ?name?
21'\" Give maximum sizes of arguments for setting tab stops. Type and
22'\" name are examples of largest possible arguments that will be passed
23'\" to .AP later. If args are omitted, default tab stops are used.
24'\"
25'\" .BS
26'\" Start box enclosure. From here until next .BE, everything will be
27'\" enclosed in one large box.
28'\"
29'\" .BE
30'\" End of box enclosure.
31'\"
32'\" .CS
33'\" Begin code excerpt.
34'\"
35'\" .CE
36'\" End code excerpt.
37'\"
38'\" .VS ?version? ?br?
39'\" Begin vertical sidebar, for use in marking newly-changed parts
40'\" of man pages. The first argument is ignored and used for recording
41'\" the version when the .VS was added, so that the sidebars can be
42'\" found and removed when they reach a certain age. If another argument
43'\" is present, then a line break is forced before starting the sidebar.
44'\"
45'\" .VE
46'\" End of vertical sidebar.
47'\"
48'\" .DS
49'\" Begin an indented unfilled display.
50'\"
51'\" .DE
52'\" End of indented unfilled display.
53'\"
54'\" .SO
55'\" Start of list of standard options for a Tk widget. The
56'\" options follow on successive lines, in four columns separated
57'\" by tabs.
58'\"
59'\" .SE
60'\" End of list of standard options for a Tk widget.
61'\"
62'\" .OP cmdName dbName dbClass
63'\" Start of description of a specific option. cmdName gives the
64'\" option's name as specified in the class command, dbName gives
65'\" the option's name in the option database, and dbClass gives
66'\" the option's class in the option database.
67'\"
68'\" .UL arg1 arg2
69'\" Print arg1 underlined, then print arg2 normally.
70'\"
71'\" RCS: @(#) $Id: man.macros,v 1.4 2000/08/25 06:18:32 ericm Exp $
72'\"
73'\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
74.if t .wh -1.3i ^B
75.nr ^l \n(.l
76.ad b
77'\" # Start an argument description
78.de AP
79.ie !"\\$4"" .TP \\$4
80.el \{\
81. ie !"\\$2"" .TP \\n()Cu
82. el .TP 15
83.\}
84.ta \\n()Au \\n()Bu
85.ie !"\\$3"" \{\
86\&\\$1 \\fI\\$2\\fP (\\$3)
87.\".b
88.\}
89.el \{\
90.br
91.ie !"\\$2"" \{\
92\&\\$1 \\fI\\$2\\fP
93.\}
94.el \{\
95\&\\fI\\$1\\fP
96.\}
97.\}
98..
99'\" # define tabbing values for .AP
100.de AS
101.nr )A 10n
102.if !"\\$1"" .nr )A \\w'\\$1'u+3n
103.nr )B \\n()Au+15n
104.\"
105.if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
106.nr )C \\n()Bu+\\w'(in/out)'u+2n
107..
108.AS Tcl_Interp Tcl_CreateInterp in/out
109'\" # BS - start boxed text
110'\" # ^y = starting y location
111'\" # ^b = 1
112.de BS
113.br
114.mk ^y
115.nr ^b 1u
116.if n .nf
117.if n .ti 0
118.if n \l'\\n(.lu\(ul'
119.if n .fi
120..
121'\" # BE - end boxed text (draw box now)
122.de BE
123.nf
124.ti 0
125.mk ^t
126.ie n \l'\\n(^lu\(ul'
127.el \{\
128.\" Draw four-sided box normally, but don't draw top of
129.\" box if the box started on an earlier page.
130.ie !\\n(^b-1 \{\
131\h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
132.\}
133.el \}\
134\h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
135.\}
136.\}
137.fi
138.br
139.nr ^b 0
140..
141'\" # VS - start vertical sidebar
142'\" # ^Y = starting y location
143'\" # ^v = 1 (for troff; for nroff this doesn't matter)
144.de VS
145.if !"\\$2"" .br
146.mk ^Y
147.ie n 'mc \s12\(br\s0
148.el .nr ^v 1u
149..
150'\" # VE - end of vertical sidebar
151.de VE
152.ie n 'mc
153.el \{\
154.ev 2
155.nf
156.ti 0
157.mk ^t
158\h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
159.sp -1
160.fi
161.ev
162.\}
163.nr ^v 0
164..
165'\" # Special macro to handle page bottom: finish off current
166'\" # box/sidebar if in box/sidebar mode, then invoked standard
167'\" # page bottom macro.
168.de ^B
169.ev 2
170'ti 0
171'nf
172.mk ^t
173.if \\n(^b \{\
174.\" Draw three-sided box if this is the box's first page,
175.\" draw two sides but no top otherwise.
176.ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
177.el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
178.\}
179.if \\n(^v \{\
180.nr ^x \\n(^tu+1v-\\n(^Yu
181\kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
182.\}
183.bp
184'fi
185.ev
186.if \\n(^b \{\
187.mk ^y
188.nr ^b 2
189.\}
190.if \\n(^v \{\
191.mk ^Y
192.\}
193..
194'\" # DS - begin display
195.de DS
196.RS
197.nf
198.sp
199..
200'\" # DE - end display
201.de DE
202.fi
203.RE
204.sp
205..
206'\" # SO - start of list of standard options
207.de SO
208.SH "STANDARD OPTIONS"
209.LP
210.nf
211.ta 5.5c 11c
212.ft B
213..
214'\" # SE - end of list of standard options
215.de SE
216.fi
217.ft R
218.LP
219See the \\fBoptions\\fR manual entry for details on the standard options.
220..
221'\" # OP - start of full description for a single option
222.de OP
223.LP
224.nf
225.ta 4c
226Command-Line Name: \\fB\\$1\\fR
227Database Name: \\fB\\$2\\fR
228Database Class: \\fB\\$3\\fR
229.fi
230.IP
231..
232'\" # CS - begin code excerpt
233.de CS
234.RS
235.nf
236.ta .25i .5i .75i 1i
237..
238'\" # CE - end code excerpt
239.de CE
240.fi
241.RE
242..
243.de UL
244\\$1\l'|0\(ul'\\$2
245..
246.TH expr n 8.4 Tcl "Tcl Built-In Commands"
247.BS
248'\" Note: do not modify the .SH NAME line immediately below!
249.SH NAME
250expr \- Evaluate an expression
251.SH SYNOPSIS
252\fBexpr \fIarg \fR?\fIarg arg ...\fR?
253.BE
254
255.SH DESCRIPTION
256.PP
257Concatenates \fIarg\fRs (adding separator spaces between them),
258evaluates the result as a Tcl expression, and returns the value.
259The operators permitted in Tcl expressions are a subset of
260the operators permitted in C expressions, and they have the
261same meaning and precedence as the corresponding C operators.
262Expressions almost always yield numeric results
263(integer or floating-point values).
264For example, the expression
265.CS
266\fBexpr 8.2 + 6\fR
267.CE
268evaluates to 14.2.
269Tcl expressions differ from C expressions in the way that
270operands are specified. Also, Tcl expressions support
271non-numeric operands and string comparisons.
272.SH OPERANDS
273.PP
274A Tcl expression consists of a combination of operands, operators,
275and parentheses.
276White space may be used between the operands and operators and
277parentheses; it is ignored by the expression's instructions.
278Where possible, operands are interpreted as integer values.
279Integer values may be specified in decimal (the normal case), in octal (if the
280first character of the operand is \fB0\fR), or in hexadecimal (if the first
281two characters of the operand are \fB0x\fR).
282If an operand does not have one of the integer formats given
283above, then it is treated as a floating-point number if that is
284possible. Floating-point numbers may be specified in any of the
285ways accepted by an ANSI-compliant C compiler (except that the
286\fBf\fR, \fBF\fR, \fBl\fR, and \fBL\fR suffixes will not be permitted in
287most installations). For example, all of the
288following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16.
289If no numeric interpretation is possible (note that all literal
290operands that are not numeric or boolean must be quoted with either
291braces or with double quotes), then an operand is left as a string
292(and only a limited set of operators may be applied to it).
293.PP
294.VS 8.4
295On 32-bit systems, integer values MAX_INT (0x7FFFFFFF) and MIN_INT
296(-0x80000000) will be represented as 32-bit values, and integer values
297outside that range will be represented as 64-bit values (if that is
298possible at all.)
299.VE 8.4
300.PP
301Operands may be specified in any of the following ways:
302.IP [1]
303As a numeric value, either integer or floating-point.
304.IP [2]
305As a boolean value, using any form understood by \fBstring is boolean\fR.
306.IP [3]
307As a Tcl variable, using standard \fB$\fR notation.
308The variable's value will be used as the operand.
309.IP [4]
310As a string enclosed in double-quotes.
311The expression parser will perform backslash, variable, and
312command substitutions on the information between the quotes,
313and use the resulting value as the operand
314.IP [5]
315As a string enclosed in braces.
316The characters between the open brace and matching close brace
317will be used as the operand without any substitutions.
318.IP [6]
319As a Tcl command enclosed in brackets.
320The command will be executed and its result will be used as
321the operand.
322.IP [7]
323As a mathematical function whose arguments have any of the above
324forms for operands, such as \fBsin($x)\fR. See below for a list of defined
325functions.
326.LP
327Where the above substitutions occur (e.g. inside quoted strings), they
328are performed by the expression's instructions.
329However, the command parser may already have performed one round of
330substitution before the expression processor was called.
331As discussed below, it is usually best to enclose expressions
332in braces to prevent the command parser from performing substitutions
333on the contents.
334.PP
335For some examples of simple expressions, suppose the variable
336\fBa\fR has the value 3 and
337the variable \fBb\fR has the value 6.
338Then the command on the left side of each of the lines below
339will produce the value on the right side of the line:
340.CS
341.ta 6c
342\fBexpr 3.1 + $a 6.1
343expr 2 + "$a.$b" 5.6
344expr 4*[llength "6 2"] 8
345expr {{word one} < "word $a"} 0\fR
346.CE
347.SH OPERATORS
348.PP
349The valid operators are listed below, grouped in decreasing order
350of precedence:
351.TP 20
352\fB\-\0\0+\0\0~\0\0!\fR
353Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operators
354may be applied to string operands, and bit-wise NOT may be
355applied only to integers.
356.TP 20
357\fB*\0\0/\0\0%\fR
358Multiply, divide, remainder. None of these operators may be
359applied to string operands, and remainder may be applied only
360to integers.
361The remainder will always have the same sign as the divisor and
362an absolute value smaller than the divisor.
363.TP 20
364\fB+\0\0\-\fR
365Add and subtract. Valid for any numeric operands.
366.TP 20
367\fB<<\0\0>>\fR
368Left and right shift. Valid for integer operands only.
369A right shift always propagates the sign bit.
370.TP 20
371\fB<\0\0>\0\0<=\0\0>=\fR
372Boolean less, greater, less than or equal, and greater than or equal.
373Each operator produces 1 if the condition is true, 0 otherwise.
374These operators may be applied to strings as well as numeric operands,
375in which case string comparison is used.
376.TP 20
377\fB==\0\0!=\fR
378Boolean equal and not equal. Each operator produces a zero/one result.
379Valid for all operand types.
380.VS 8.4
381.TP 20
382\fBeq\0\0ne\fR
383Boolean string equal and string not equal. Each operator produces a
384zero/one result. The operand types are interpreted only as strings.
385.VE 8.4
386.TP 20
387\fB&\fR
388Bit-wise AND. Valid for integer operands only.
389.TP 20
390\fB^\fR
391Bit-wise exclusive OR. Valid for integer operands only.
392.TP 20
393\fB|\fR
394Bit-wise OR. Valid for integer operands only.
395.TP 20
396\fB&&\fR
397Logical AND. Produces a 1 result if both operands are non-zero,
3980 otherwise.
399Valid for boolean and numeric (integers or floating-point) operands only.
400.TP 20
401\fB||\fR
402Logical OR. Produces a 0 result if both operands are zero, 1 otherwise.
403Valid for boolean and numeric (integers or floating-point) operands only.
404.TP 20
405\fIx\fB?\fIy\fB:\fIz\fR
406If-then-else, as in C. If \fIx\fR
407evaluates to non-zero, then the result is the value of \fIy\fR.
408Otherwise the result is the value of \fIz\fR.
409The \fIx\fR operand must have a boolean or numeric value.
410.LP
411See the C manual for more details on the results
412produced by each operator.
413All of the binary operators group left-to-right within the same
414precedence level. For example, the command
415.CS
416\fBexpr 4*2 < 7\fR
417.CE
418returns 0.
419.PP
420The \fB&&\fR, \fB||\fR, and \fB?:\fR operators have ``lazy
421evaluation'', just as in C,
422which means that operands are not evaluated if they are
423not needed to determine the outcome. For example, in the command
424.CS
425\fBexpr {$v ? [a] : [b]}\fR
426.CE
427only one of \fB[a]\fR or \fB[b]\fR will actually be evaluated,
428depending on the value of \fB$v\fR. Note, however, that this is
429only true if the entire expression is enclosed in braces; otherwise
430the Tcl parser will evaluate both \fB[a]\fR and \fB[b]\fR before
431invoking the \fBexpr\fR command.
432.SH "MATH FUNCTIONS"
433.PP
434Tcl supports the following mathematical functions in expressions, all
435of which work solely with floating-point numbers unless otherwise noted:
436.DS
437.ta 3c 6c 9c
438\fBabs\fR \fBcosh\fR \fBlog\fR \fBsqrt\fR
439\fBacos\fR \fBdouble\fR \fBlog10\fR \fBsrand\fR
440\fBasin\fR \fBexp\fR \fBpow\fR \fBtan\fR
441\fBatan\fR \fBfloor\fR \fBrand\fR \fBtanh\fR
442\fBatan2\fR \fBfmod\fR \fBround\fR \fBwide\fR
443\fBceil\fR \fBhypot\fR \fBsin\fR
444\fBcos\fR \fBint\fR \fBsinh\fR
445.DE
446.PP
447.TP
448\fBabs(\fIarg\fB)\fR
449Returns the absolute value of \fIarg\fR. \fIArg\fR may be either
450integer or floating-point, and the result is returned in the same form.
451.TP
452\fBacos(\fIarg\fB)\fR
453Returns the arc cosine of \fIarg\fR, in the range [\fI0\fR,\fIpi\fR]
454radians. \fIArg\fR should be in the range [\fI-1\fR,\fI1\fR].
455.TP
456\fBasin(\fIarg\fB)\fR
457Returns the arc sine of \fIarg\fR, in the range [\fI-pi/2\fR,\fIpi/2\fR]
458radians. \fIArg\fR should be in the range [\fI-1\fR,\fI1\fR].
459.TP
460\fBatan(\fIarg\fB)\fR
461Returns the arc tangent of \fIarg\fR, in the range [\fI-pi/2\fR,\fIpi/2\fR]
462radians.
463.TP
464\fBatan2(\fIy, x\fB)\fR
465Returns the arc tangent of \fIy\fR/\fIx\fR, in the range [\fI-pi\fR,\fIpi\fR]
466radians. \fIx\fR and \fIy\fR cannot both be 0. If \fIx\fR is greater
467than \fI0\fR, this is equivalent to \fBatan(\fIy/x\fB)\fR.
468.TP
469\fBceil(\fIarg\fB)\fR
470Returns the smallest integral floating-point value (i.e. with a zero
471fractional part) not less than \fIarg\fR.
472.TP
473\fBcos(\fIarg\fB)\fR
474Returns the cosine of \fIarg\fR, measured in radians.
475.TP
476\fBcosh(\fIarg\fB)\fR
477Returns the hyperbolic cosine of \fIarg\fR. If the result would cause
478an overflow, an error is returned.
479.TP
480\fBdouble(\fIarg\fB)\fR
481If \fIarg\fR is a floating-point value, returns \fIarg\fR, otherwise converts
482\fIarg\fR to floating-point and returns the converted value.
483.TP
484\fBexp(\fIarg\fB)\fR
485Returns the exponential of \fIarg\fR, defined as \fIe\fR**\fIarg\fR.
486If the result would cause an overflow, an error is returned.
487.TP
488\fBfloor(\fIarg\fB)\fR
489Returns the largest integral floating-point value (i.e. with a zero
490fractional part) not greater than \fIarg\fR.
491.TP
492\fBfmod(\fIx, y\fB)\fR
493Returns the floating-point remainder of the division of \fIx\fR by
494\fIy\fR. If \fIy\fR is 0, an error is returned.
495.TP
496\fBhypot(\fIx, y\fB)\fR
497Computes the length of the hypotenuse of a right-angled triangle
498\fBsqrt(\fIx\fR*\fIx\fR+\fIy\fR*\fIy\fB)\fR.
499.TP
500\fBint(\fIarg\fB)\fR
501.VS 8.4
502If \fIarg\fR is an integer value of the same width as the machine
503word, returns \fIarg\fR, otherwise
504converts \fIarg\fR to an integer (of the same size as a machine word,
505i.e. 32-bits on 32-bit systems, and 64-bits on 64-bit systems) by
506truncation and returns the converted value.
507.VE 8.4
508.TP
509\fBlog(\fIarg\fB)\fR
510Returns the natural logarithm of \fIarg\fR. \fIArg\fR must be a
511positive value.
512.TP
513\fBlog10(\fIarg\fB)\fR
514Returns the base 10 logarithm of \fIarg\fR. \fIArg\fR must be a
515positive value.
516.TP
517\fBpow(\fIx, y\fB)\fR
518Computes the value of \fIx\fR raised to the power \fIy\fR. If \fIx\fR
519is negative, \fIy\fR must be an integer value.
520.TP
521\fBrand()\fR
522Returns a pseudo-random floating-point value in the range (\fI0\fR,\fI1\fR).
523The generator algorithm is a simple linear congruential generator that
524is not cryptographically secure. Each result from \fBrand\fR completely
525determines all future results from subsequent calls to \fBrand\fR, so
526\fBrand\fR should not be used to generate a sequence of secrets, such as
527one-time passwords. The seed of the generator is initialized from the
528internal clock of the machine or may be set with the \fBsrand\fR function.
529.TP
530\fBround(\fIarg\fB)\fR
531If \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts
532\fIarg\fR to integer by rounding and returns the converted value.
533.TP
534\fBsin(\fIarg\fB)\fR
535Returns the sine of \fIarg\fR, measured in radians.
536.TP
537\fBsinh(\fIarg\fB)\fR
538Returns the hyperbolic sine of \fIarg\fR. If the result would cause
539an overflow, an error is returned.
540.TP
541\fBsqrt(\fIarg\fB)\fR
542Returns the square root of \fIarg\fR. \fIArg\fR must be non-negative.
543.TP
544\fBsrand(\fIarg\fB)\fR
545The \fIarg\fR, which must be an integer, is used to reset the seed for
546the random number generator of \fBrand\fR. Returns the first random
547number (see \fBrand()\fR) from that seed. Each interpreter has its own seed.
548.TP
549\fBtan(\fIarg\fB)\fR
550Returns the tangent of \fIarg\fR, measured in radians.
551.TP
552\fBtanh(\fIarg\fB)\fR
553Returns the hyperbolic tangent of \fIarg\fR.
554.TP
555\fBwide(\fIarg\fB)\fR
556.VS 8.4
557Converts \fIarg\fR to an integer value at least 64-bits wide (by sign-extension
558if \fIarg\fR is a 32-bit number) if it is not one already.
559.VE 8.4
560.PP
561In addition to these predefined functions, applications may
562define additional functions using \fBTcl_CreateMathFunc\fR().
563.SH "TYPES, OVERFLOW, AND PRECISION"
564.PP
565All internal computations involving integers are done with the C type
566\fIlong\fR, and all internal computations involving floating-point are
567done with the C type \fIdouble\fR.
568When converting a string to floating-point, exponent overflow is
569detected and results in a Tcl error.
570For conversion to integer from string, detection of overflow depends
571on the behavior of some routines in the local C library, so it should
572be regarded as unreliable.
573In any case, integer overflow and underflow are generally not detected
574reliably for intermediate results. Floating-point overflow and underflow
575are detected to the degree supported by the hardware, which is generally
576pretty reliable.
577.PP
578Conversion among internal representations for integer, floating-point,
579and string operands is done automatically as needed.
580For arithmetic computations, integers are used until some
581floating-point number is introduced, after which floating-point is used.
582For example,
583.CS
584\fBexpr 5 / 4\fR
585.CE
586returns 1, while
587.CS
588\fBexpr 5 / 4.0\fR
589\fBexpr 5 / ( [string length "abcd"] + 0.0 )\fR
590.CE
591both return 1.25.
592Floating-point values are always returned with a ``\fB.\fR''
593or an \fBe\fR so that they will not look like integer values. For
594example,
595.CS
596\fBexpr 20.0/5.0\fR
597.CE
598returns \fB4.0\fR, not \fB4\fR.
599.SH "STRING OPERATIONS"
600.PP
601String values may be used as operands of the comparison operators,
602although the expression evaluator tries to do comparisons as integer
603or floating-point when it can,
604.VS 8.4
605except in the case of the \fBeq\fR and \fBne\fR operators.
606.VE 8.4
607If one of the operands of a comparison is a string and the other
608has a numeric value, the numeric operand is converted back to
609a string using the C \fIsprintf\fR format specifier
610\fB%d\fR for integers and \fB%g\fR for floating-point values.
611For example, the commands
612.CS
613\fBexpr {"0x03" > "2"}\fR
614\fBexpr {"0y" < "0x12"}\fR
615.CE
616both return 1. The first comparison is done using integer
617comparison, and the second is done using string comparison after
618the second operand is converted to the string \fB18\fR.
619Because of Tcl's tendency to treat values as numbers whenever
620possible, it isn't generally a good idea to use operators like \fB==\fR
621when you really want string comparison and the values of the
622operands could be arbitrary; it's better in these cases to use
623.VS 8.4
624the \fBeq\fR or \fBne\fR operators, or
625.VE 8.4
626the \fBstring\fR command instead.
627
628.SH "PERFORMANCE CONSIDERATIONS"
629.PP
630Enclose expressions in braces for the best speed and the smallest
631storage requirements.
632This allows the Tcl bytecode compiler to generate the best code.
633.PP
634As mentioned above, expressions are substituted twice:
635once by the Tcl parser and once by the \fBexpr\fR command.
636For example, the commands
637.CS
638\fBset a 3\fR
639\fBset b {$a + 2}\fR
640\fBexpr $b*4\fR
641.CE
642return 11, not a multiple of 4.
643This is because the Tcl parser will first substitute \fB$a + 2\fR for
644the variable \fBb\fR,
645then the \fBexpr\fR command will evaluate the expression \fB$a + 2*4\fR.
646.PP
647Most expressions do not require a second round of substitutions.
648Either they are enclosed in braces or, if not,
649their variable and command substitutions yield numbers or strings
650that don't themselves require substitutions.
651However, because a few unbraced expressions
652need two rounds of substitutions,
653the bytecode compiler must emit
654additional instructions to handle this situation.
655The most expensive code is required for
656unbraced expressions that contain command substitutions.
657These expressions must be implemented by generating new code
658each time the expression is executed.
659.SH EXAMPLES
660Define a procedure that computes an "interesting" mathematical
661function:
662.CS
663proc calc {x y} {
664 \fBexpr\fR { ($x*$x - $y*$y) / exp($x*$x + $y*$y) }
665}
666.CE
667.PP
668Convert polar coordinates into cartesian coordinates:
669.CS
670# convert from ($radius,$angle)
671set x [\fBexpr\fR { $radius * cos($angle) }]
672set y [\fBexpr\fR { $radius * sin($angle) }]
673.CE
674.PP
675Convert cartesian coordinates into polar coordinates:
676.CS
677# convert from ($x,$y)
678set radius [\fBexpr\fR { hypot($y, $x) }]
679set angle [\fBexpr\fR { atan2($y, $x) }]
680.CE
681.PP
682Print a message describing the relationship of two string values to
683each other:
684.CS
685puts "a and b are [\fBexpr\fR {$a eq $b ? {equal} : {different}}]"
686.CE
687.PP
688Set a variable to whether an environment variable is both defined at
689all and also set to a true boolean value:
690.CS
691set isTrue [\fBexpr\fR {
692 [info exists ::env(SOME_ENV_VAR)] &&
693 [string is true -strict $::env(SOME_ENV_VAR)]
694}]
695.CE
696.PP
697Generate a random integer in the range 0..99 inclusive:
698.CS
699set randNum [\fBexpr\fR { int(100 * rand()) }]
700.CE
701
702.SH "SEE ALSO"
703array(n), for(n), if(n), string(n), Tcl(n), while(n)
704
705.SH KEYWORDS
706arithmetic, boolean, compare, expression, fuzzy comparison