summaryrefslogtreecommitdiffstats
path: root/doc/expr.n
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2008-10-15 10:43:37 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2008-10-15 10:43:37 (GMT)
commit89b27a8118e809ac322a0200fbda93fd65b03d15 (patch)
tree76a9fcf427bd66b39f4124071860e59744540d77 /doc/expr.n
parent9621a454a0bde1f84cef51026947815d4eb244b6 (diff)
downloadtcl-89b27a8118e809ac322a0200fbda93fd65b03d15.zip
tcl-89b27a8118e809ac322a0200fbda93fd65b03d15.tar.gz
tcl-89b27a8118e809ac322a0200fbda93fd65b03d15.tar.bz2
Lots of very minor formatting fixes.
Diffstat (limited to 'doc/expr.n')
-rw-r--r--doc/expr.n29
1 files changed, 28 insertions, 1 deletions
diff --git a/doc/expr.n b/doc/expr.n
index 613a3bc..b078873 100644
--- a/doc/expr.n
+++ b/doc/expr.n
@@ -6,7 +6,7 @@
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
-'\" RCS: @(#) $Id: expr.n,v 1.35 2008/06/29 22:28:24 dkf Exp $
+'\" RCS: @(#) $Id: expr.n,v 1.36 2008/10/15 10:43:37 dkf Exp $
'\"
.so man.macros
.TH expr n 8.5 Tcl "Tcl Built-In Commands"
@@ -28,9 +28,11 @@ as the corresponding C operators.
Expressions almost always yield numeric results
(integer or floating-point values).
For example, the expression
+.PP
.CS
\fBexpr 8.2 + 6\fR
.CE
+.PP
evaluates to 14.2.
Tcl expressions differ from C expressions in the way that
operands are specified. Also, Tcl expressions support
@@ -205,18 +207,22 @@ divide operators, and produces a result that is the same as the output
of the \fBpow\fR function (after any type conversions.)
All of the binary operators group left-to-right within the same
precedence level. For example, the command
+.PP
.CS
\fBexpr\fR {4*2 < 7}
.CE
+.PP
returns 0.
.PP
The \fB&&\fR, \fB||\fR, and \fB?:\fR operators have
.QW "lazy evaluation" ,
just as in C, which means that operands are not evaluated if they are
not needed to determine the outcome. For example, in the command
+.PP
.CS
\fBexpr {$v ? [a] : [b]}\fR
.CE
+.PP
only one of
.QW \fB[a]\fR
or
@@ -235,14 +241,19 @@ When the expression parser encounters a mathematical function
such as \fBsin($x)\fR, it replaces it with a call to an ordinary
Tcl function in the \fBtcl::mathfunc\fR namespace. The processing
of an expression such as:
+.PP
.CS
\fBexpr {sin($x+$y)}\fR
.CE
+.PP
is the same in every way as the processing of:
+.PP
.CS
\fBexpr {[tcl::mathfunc::sin [expr {$x+$y}]]}\fR
.CE
+.PP
which in turn is the same as the processing of:
+.PP
.CS
\fBtcl::mathfunc::sin [expr {$x+$y}]\fR
.CE
@@ -280,23 +291,29 @@ and string operands is done automatically as needed.
For arithmetic computations, integers are used until some
floating-point number is introduced, after which floating-point is used.
For example,
+.PP
.CS
\fBexpr\fR {5 / 4}
.CE
+.PP
returns 1, while
+.PP
.CS
\fBexpr\fR {5 / 4.0}
\fBexpr\fR {5 / ( [string length "abcd"] + 0.0 )}
.CE
+.PP
both return 1.25.
Floating-point values are always returned with a
.QW \fB.\fR
or an
.QW \fBe\fR
so that they will not look like integer values. For example,
+.PP
.CS
\fBexpr\fR {20.0/5.0}
.CE
+.PP
returns \fB4.0\fR, not \fB4\fR.
.SS "STRING OPERATIONS"
.PP
@@ -311,10 +328,12 @@ Canonical string representation for integer values is a decimal string
format. Canonical string representation for floating-point values
is that produced by the \fB%g\fR format specifier of Tcl's
\fBformat\fR command. For example, the commands
+.PP
.CS
\fBexpr {"0x03" > "2"}\fR
\fBexpr {"0y" < "0x12"}\fR
.CE
+.PP
both return 1. The first comparison is done using integer
comparison, and the second is done using string comparison after
the second operand is converted to the string \fB18\fR.
@@ -332,11 +351,13 @@ This allows the Tcl bytecode compiler to generate the best code.
As mentioned above, expressions are substituted twice:
once by the Tcl parser and once by the \fBexpr\fR command.
For example, the commands
+.PP
.CS
\fBset a 3\fR
\fBset b {$a + 2}\fR
\fBexpr $b*4\fR
.CE
+.PP
return 11, not a multiple of 4.
This is because the Tcl parser will first substitute \fB$a + 2\fR for
the variable \fBb\fR,
@@ -362,6 +383,7 @@ operator, consider using the commands documented in the \fBmathfunc\fR(n) or
Define a procedure that computes an
.QW interesting
mathematical function:
+.PP
.CS
proc tcl::mathfunc::calc {x y} {
\fBexpr\fR { ($x**2 - $y**2) / exp($x**2 + $y**2) }
@@ -369,6 +391,7 @@ proc tcl::mathfunc::calc {x y} {
.CE
.PP
Convert polar coordinates into cartesian coordinates:
+.PP
.CS
# convert from ($radius,$angle)
set x [\fBexpr\fR { $radius * cos($angle) }]
@@ -376,6 +399,7 @@ set y [\fBexpr\fR { $radius * sin($angle) }]
.CE
.PP
Convert cartesian coordinates into polar coordinates:
+.PP
.CS
# convert from ($x,$y)
set radius [\fBexpr\fR { hypot($y, $x) }]
@@ -384,12 +408,14 @@ set angle [\fBexpr\fR { atan2($y, $x) }]
.PP
Print a message describing the relationship of two string values to
each other:
+.PP
.CS
puts "a and b are [\fBexpr\fR {$a eq $b ? {equal} : {different}}]"
.CE
.PP
Set a variable to whether an environment variable is both defined at
all and also set to a true boolean value:
+.PP
.CS
set isTrue [\fBexpr\fR {
[info exists ::env(SOME_ENV_VAR)] &&
@@ -398,6 +424,7 @@ set isTrue [\fBexpr\fR {
.CE
.PP
Generate a random integer in the range 0..99 inclusive:
+.PP
.CS
set randNum [\fBexpr\fR { int(100 * rand()) }]
.CE