Purpose: to discuss expr and friends ---- [Tcl]'s built in math support is limited to what will fit into a long or double C variable type. To perform math on longer numbers, some alternative, such as [Tom Poindexter]'s [mpexpr] extension, is needed. For the limits of ''long'' and workarounds to force over-big intstrings to floatstring, see [expr problems with int]. The primary method of doing calculations is by way of Tcl's '''expr''' command. Unless you have a ''very'' good reason not to, you should always enclose the argument expression to [[expr]] in curly braces. This allows the bytecode compiler to optimise your code more thoroughly since it has fewer possible interpretations and the contents of variables and the results of commands will not unexpectedly modify the meaning of the expression. [AM] A very good reason is that part or whole of the expression is not a constant, e.g. set v [expr "$a $op $b"] if the operator $op varies, then surrounding this expression with { } makes the proper evaluation fail. ---- [DKF]: From Tcl 8.4 onwards, you can also use a larger integer type termed a ''wide''. This lets you work with 64-bit values on 32-bit machines... 20050104 [Twylite] - see [expr problems with int] for more discussion on ''wide''. ---- See [expr] for a more detailed list of the functions that the man page skims over'' ---- '''OPERATORS''' in order of precedence (tightest-binding to least-tight binding) '''- + ~ !''': Unary operators; specifically a negation operation, a non-negation operation (I see little point in this one), a bit-wise NOT operation (every bit in the input value gets replaced by its inverse) and a logical NOT operation (non-zero maps to zero, and zero maps to one.) '''* / %''': Multiplication, division and ''integer'' remainder (see fmod() below.) '''+ -''': Addition and subtraction. '''<< >>''': Left and right shift. Equivalent to multiplying or dividing by a suitable power of two, and then reducing the result to the range representable in an integer on the host platform. '''< > <= >=''': Ordering relations (less than, greater than, less than or equal, greater than or equal.) Note that these operations work on strings as well as numbers, but you are probably better off testing the result of [[string compare]] instead as that is more predictable in the case of a string that looks like a number. '''== !=''': Equality and inequality. Note that these operations work on strings as well as numbers, but you are probably better off testing the result of [[string equal]] instead as that is more predictable in the case of a string that looks like a number. '''&''': Bit-wise AND. A bit is set in the result when the corresponding bit is set in both the arguments. '''^''': Bit-wise exclusive OR. A bit is set in the result when the corresponding bit is set in ''precisely one'' of the arguments. '''|''': Bit-wise OR. A bit is set in the result when the corresponding bit is set in either of the arguments. '''&&''': Logical AND. The result is a one (true) when both of the arguments are non-zero (true), and zero (false) otherwise. Note that this operation is a ''short-circuiting'' operation, and will only evaluate its second argument when the first argument is non-zero. This includes the expansion of Tcl commands in square brackets, but this delay in evaluation only occurs if the whole expression is enclosed in curly braces. '''||''': Logical OR. The result is a zero (false) when both of the arguments are zero (false), and one (true) otherwise. Note that this operation is a ''short-circuiting'' operation, and will only evaluate its second argument when the first argument is zero. This includes the expansion of Tcl commands in square brackets, but this delay in evaluation only occurs if the whole expression is enclosed in curly braces. '''x?y:z''' .: If-then-else, as in C (where x,y,z are expressions). If the value x is non-zero (true) then the expression y is evaluated to produce the result, and otherwise the expression z is evaluated to produce the result. Note that this operation is a ''short-cicuiting'' operation, and will not evaluate expression z if x is zero (false) and will not evaluate expression y if x is non-zero (true). This includes the expansion of Tcl commands in square brackets, but this delay in evaluation only occurs if the whole expression is enclosed in curly braces. It is usually clearer and easier to maintain (and no slower - the generated bytecode is identical) to use the Tcl [[if]] command instead of this. ---- '''BUILTIN FUNCTIONS''' '''abs'''(x): Absolute value (negate if negative.) '''acos'''(x): Inverse cosine (result in radians.) '''asin'''(x): Inverse sine (result in radians.) '''atan'''(x): Inverse tangent (result in radians.) '''atan2'''(y,x): Inverse tangent. Can handle cases which plain atan() can't (due to division by zero) and has a larger output range (result in radians.) '''ceil'''(x): Ceiling (defined over floating point numbers.) If the input value is not a whole number, return the next ''largest'' whole number. '''cos'''(x): Cosine (input in radians.) '''cosh'''(x): Hyperbolic cosine. '''double'''(x): Convert number to floating point. '''exp'''(x): Exponential function. Returns e**inputValue (using the FORTRAN-style notation) where e is the base of natural logarithms. '''floor'''(x): Floor (defined over floating point numbers.) If the input value is not a whole number, return the next ''smallest'' whole number. '''fmod'''(x): Floating point remainder. '''hypot'''(x,y): Hypotenuse calculator. If the projection of a straight line segment onto the X axis is x units long, and the projection of that line segment onto the Y axis is y units long, then the line segment is hypot(x,y) units long (assuming boring old Euclidean geometry.) Equivalent to sqrt(x*x+y*y). '''int'''(x): Convert number to integer by truncation. '''log'''(x): Natural [logarithm]. '''log10'''(x): Logarithm with respect to base 10. '''pow'''(x,y): Power function. In FORTRAN notation, x**y. '''rand'''(): Random number. Uses uniform distribution over the range [0,1). ''This RNG is not suitable for cryptography.'' '''round'''(x): Round to nearest whole number. ''Not suitable for financial rounding.'' '''sin'''(x): Sine (input in radians.) '''sinh'''(x): Hyperbolic sine. '''sqrt'''(x): Square root (well, the positive square root only. And Tcl doesn't do complex maths, so the input had better be positive...) '''srand'''(x): Seeds the random number generator with the given value. Each interpreter has its own random number generator, which starts out seeded with the current time. '''tan'''(x): Tangent (input in radians.) '''tanh'''(x): Hyperbolic tangent. ---- [Category Command] - [Category Mathematics]