📄 expr.n
字号:
'\"'\" Copyright (c) 1993 The Regents of the University of California.'\" Copyright (c) 1994-1997 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" SCCS: @(#) expr.n 1.28 97/09/18 18:21:30'\" .so man.macros.TH expr n 8.0 Tcl "Tcl Built-In Commands".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEexpr \- Evaluate an expression.SH SYNOPSIS\fBexpr \fIarg \fR?\fIarg arg ...\fR?.BE.SH DESCRIPTION.PPConcatenates \fIarg\fR's (adding separator spaces between them),evaluates the result as a Tcl expression, and returns the value.The operators permitted in Tcl expressions are a subset ofthe operators permitted in C expressions, and they have thesame meaning and precedence as the corresponding C operators.Expressions almost always yield numeric results(integer or floating-point values).For example, the expression.CS\fBexpr 8.2 + 6\fR.CEevaluates to 14.2.Tcl expressions differ from C expressions in the way thatoperands are specified. Also, Tcl expressions supportnon-numeric operands and string comparisons..SH OPERANDS.PPA Tcl expression consists of a combination of operands, operators,and parentheses.White space may be used between the operands and operators andparentheses; it is ignored by the expression's instructions.Where possible, operands are interpreted as integer values.Integer values may be specified in decimal (the normal case), in octal (if thefirst character of the operand is \fB0\fR), or in hexadecimal (if the firsttwo characters of the operand are \fB0x\fR).If an operand does not have one of the integer formats givenabove, then it is treated as a floating-point number if that ispossible. Floating-point numbers may be specified in any of theways accepted by an ANSI-compliant C compiler (except that the\fBf\fR, \fBF\fR, \fBl\fR, and \fBL\fR suffixes will not be permitted inmost installations). For example, all of thefollowing are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16.If no numeric interpretation is possible, then an operand is leftas a string (and only a limited set of operators may be applied toit)..PPOperands may be specified in any of the following ways:.IP [1]As an numeric value, either integer or floating-point..IP [2]As a Tcl variable, using standard \fB$\fR notation.The variable's value will be used as the operand..IP [3]As a string enclosed in double-quotes.The expression parser will perform backslash, variable, andcommand substitutions on the information between the quotes,and use the resulting value as the operand.IP [4]As a string enclosed in braces.The characters between the open brace and matching close bracewill be used as the operand without any substitutions..IP [5]As a Tcl command enclosed in brackets.The command will be executed and its result will be used asthe operand..IP [6]As a mathematical function whose arguments have any of the aboveforms for operands, such as \fBsin($x)\fR. See below for a list of definedfunctions..LPWhere substitutions occur above (e.g. inside quoted strings), theyare performed by the expression's instructions.However, an additional layer of substitution may already havebeen performed by the command parser before the expressionprocessor was called.As discussed below, it is usually best to enclose expressionsin braces to prevent the command parser from performing substitutionson the contents..PPFor some examples of simple expressions, suppose the variable\fBa\fR has the value 3 andthe variable \fBb\fR has the value 6.Then the command on the left side of each of the lines belowwill produce the value on the right side of the line:.CS.ta 6c\fBexpr 3.1 + $a 6.1expr 2 + "$a.$b" 5.6expr 4*[llength "6 2"] 8expr {{word one} < "word $a"} 0\fR.CE.SH OPERATORS.PPThe valid operators are listed below, grouped in decreasing orderof precedence:.TP 20\fB\-\0\0+\0\0~\0\0!\fRUnary minus, unary plus, bit-wise NOT, logical NOT. None of these operandsmay be applied to string operands, and bit-wise NOT may beapplied only to integers..TP 20\fB*\0\0/\0\0%\fRMultiply, divide, remainder. None of these operands may beapplied to string operands, and remainder may be applied onlyto integers.The remainder will always have the same sign as the divisor andan absolute value smaller than the divisor..TP 20\fB+\0\0\-\fRAdd and subtract. Valid for any numeric operands..TP 20\fB<<\0\0>>\fRLeft and right shift. Valid for integer operands only.A right shift always propagates the sign bit..TP 20\fB<\0\0>\0\0<=\0\0>=\fRBoolean less, greater, less than or equal, and greater than or equal.Each operator produces 1 if the condition is true, 0 otherwise.These operators may be applied to strings as well as numeric operands,in which case string comparison is used..TP 20\fB==\0\0!=\fRBoolean equal and not equal. Each operator produces a zero/one result.Valid for all operand types..TP 20\fB&\fRBit-wise AND. Valid for integer operands only..TP 20\fB^\fRBit-wise exclusive OR. Valid for integer operands only..TP 20\fB|\fRBit-wise OR. Valid for integer operands only..TP 20\fB&&\fRLogical AND. Produces a 1 result if both operands are non-zero,0 otherwise.Valid for boolean and numeric (integers or floating-point) operands only..TP 20\fB||\fRLogical OR. Produces a 0 result if both operands are zero, 1 otherwise.Valid for boolean and numeric (integers or floating-point) operands only..TP 20\fIx\fB?\fIy\fB:\fIz\fRIf-then-else, as in C. If \fIx\fRevaluates to non-zero, then the result is the value of \fIy\fR.Otherwise the result is the value of \fIz\fR.The \fIx\fR operand must have a numeric value..LPSee the C manual for more details on the resultsproduced by each operator.All of the binary operators group left-to-right within the sameprecedence level. For example, the command.CS\fBexpr 4*2 < 7\fR.CEreturns 0..PPThe \fB&&\fR, \fB||\fR, and \fB?:\fR operators have ``lazyevaluation'', just as in C, which means that operands are not evaluated if they arenot needed to determine the outcome. For example, in the command.CS\fBexpr {$v ? [a] : [b]}\fR.CEonly one of \fB[a]\fR or \fB[b]\fR will actually be evaluated,depending on the value of \fB$v\fR. Note, however, that this isonly true if the entire expression is enclosed in braces; otherwisethe Tcl parser will evaluate both \fB[a]\fR and \fB[b]\fR beforeinvoking the \fBexpr\fR command..SH "MATH FUNCTIONS".PPTcl supports the following mathematical functions in expressions:.DS.ta 3c 6c 9c\fBacos\fR \fBcos\fR \fBhypot\fR \fBsinh\fR\fBasin\fR \fBcosh\fR \fBlog\fR \fBsqrt\fR\fBatan\fR \fBexp\fR \fBlog10\fR \fBtan\fR\fBatan2\fR \fBfloor\fR \fBpow\fR \fBtanh\fR\fBceil\fR \fBfmod\fR \fBsin\fR.DEEach of these functions invokes the math library function of the samename; see the manual entries for the library functions for detailson what they do. Tcl also implements the following functions forconversion between integers and floating-point numbers and thegeneration of random numbers:.TP\fBabs(\fIarg\fB)\fRReturns the absolute value of \fIarg\fR. \fIArg\fR may be eitherinteger or floating-point, and the result is returned in the same form..TP\fBdouble(\fIarg\fB)\fRIf \fIarg\fR is a floating value, returns \fIarg\fR, otherwise converts\fIarg\fR to floating and returns the converted value..TP\fBint(\fIarg\fB)\fRIf \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts\fIarg\fR to integer by truncation and returns the converted value..TP\fBrand()\fRReturns a floating point number from zero to just less than one or,in mathematical terms, the range [0,1). The seed comes from theinternal clock of the machine or may be set manual with the srandfunction..TP\fBround(\fIarg\fB)\fRIf \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts\fIarg\fR to integer by rounding and returns the converted value..TP\fBsrand(\fIarg\fB)\fRThe \fIarg\fR, which must be an integer, is used to reset the seed forthe random number generator. Returns the first random number fromthat seed. Each interpreter has it's own seed..PPIn addition to these predefined functions, applications maydefine additional functions using \fBTcl_CreateMathFunc\fR()..SH "TYPES, OVERFLOW, AND PRECISION".PPAll internal computations involving integers are done with the C type\fIlong\fR, and all internal computations involving floating-point aredone with the C type \fIdouble\fR.When converting a string to floating-point, exponent overflow isdetected and results in a Tcl error.For conversion to integer from string, detection of overflow dependson the behavior of some routines in the local C library, so it shouldbe regarded as unreliable.In any case, integer overflow and underflow are generally not detectedreliably for intermediate results. Floating-point overflow and underfloware detected to the degree supported by the hardware, which is generallypretty reliable..PPConversion among internal representations for integer, floating-point,and string operands is done automatically as needed.For arithmetic computations, integers are used until somefloating-point number is introduced, after which floating-point is used.For example,.CS\fBexpr 5 / 4\fR.CEreturns 1, while.CS\fBexpr 5 / 4.0\fR\fBexpr 5 / ( [string length "abcd"] + 0.0 )\fR.CEboth return 1.25.Floating-point values are always returned with a ``\fB.\fR''or an \fBe\fR so that they will not look like integer values. Forexample,.CS\fBexpr 20.0/5.0\fR.CEreturns \fB4.0\fR, not \fB4\fR..SH "STRING OPERATIONS".PPString values may be used as operands of the comparison operators,although the expression evaluator tries to do comparisons as integeror floating-point when it can.If one of the operands of a comparison is a string and the otherhas a numeric value, the numeric operand is converted back toa string using the C \fIsprintf\fR format specifier\fB%d\fR for integers and \fB%g\fR for floating-point values.For example, the commands.CS\fBexpr {"0x03" > "2"}\fR\fBexpr {"0y" < "0x12"}\fR.CEboth return 1. The first comparison is done using integercomparison, and the second is done using string comparison afterthe second operand is converted to the string \fB18\fR.Because of Tcl's tendency to treat values as numbers wheneverpossible, it isn't generally a good idea to use operators like \fB==\fRwhen you really want string comparison and the values of theoperands could be arbitrary; it's better in these cases to use the\fBstring compare\fR command instead..SH "PERFORMANCE CONSIDERATIONS".VS.PPEnclose expressions in braces for the best speed and the smalleststorage requirements.This allows the Tcl bytecode compiler to generate the best code..PPAs mentioned above, expressions are substituted twice:once by the Tcl parser and once by the \fBexpr\fR command.For example, the commands.CS\fBset a 3\fR\fBset b {$a + 2}\fR\fBexpr $b*4\fR.CEreturn 11, not a multiple of 4.This is because the Tcl parser will first substitute \fB$a + 2\fR forthe variable \fBb\fR,then the \fBexpr\fR command will evaluate the expression \fB$a + 2*4\fR..PPMost expressions do not require a second round of substitutions.Either they are enclosed in braces or, if not,their variable and command substitutions yield numbers or stringsthat don't themselves require substitutions.However, because a few unbraced expressions need two rounds of substitutions,the bytecode compiler must emitadditional instructions to handle this situation.The most expensive code is required forunbraced expressions that contain command substitutions.These expressions must be implemented by generating new codeeach time the expression is executed..VE.SH KEYWORDSarithmetic, boolean, compare, expression, fuzzy comparison
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -