:TITLE: Results of a command - Math 101 ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl6.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: The command for doing math type operations is expr.

Here's some excerpts from the man page.

Expr concatenates arg'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 of the operators permitted in C expressions, and they have the same meaning and precedence as the corresponding C operators. Expressions almost always yield numeric results (integer or floating-point values). For example, the expression

expr 8.2 + 6

evaluates to 14.2. Tcl expressions differ from C expressions in the way that operands are specified. Also, Tcl expressions support non-numeric operands and string comparisons.

OPERANDS

A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between the operands and operators and parentheses; it is ignored by the expression processor. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x).

Note that the octal and hexadecimal conversion takes place differently in the expr command than in the Tcl substitution phase.

If an operand does not have one of the integer formats given above, then it is treated as a floating-point number if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI- compliant C compiler. For example, all of the following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interpretation is possible, then an operand is left as a string (and only a limited set of operators may be applied to it).

Operands may be specified in any of the following ways:

OPERATORS

The valid operators are listed below, grouped in decreasing order of precedence:

- + ~ !
Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operators may be applied to string operands, and bit-wise NOT may be applied only to integers.
* / %
Multiply, divide, remainder. None of these operators may be applied to string operands, and remainder may be applied only to integers. The remainder will always have the same sign as the divisor and an absolute value smaller than the divisor.
+ -
Add and subtract. Valid for any numeric operands.
<< >>
Left and right shift. Valid for integer operands only.
&
Bit-wise AND. Valid for integer operands only.
^
Bit-wise exclusive OR. Valid for integer operands only.
|
Bit-wise OR. Valid for integer operands only.
&&
Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise. Valid for numeric operands only (integers or floating-point).
||
Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. Valid for numeric operands only (integers or floating-point).
x?y:z
If-then-else, as in C. If x evaluates to non-zero, then the result is the value of y. Otherwise the result is the value of z. The x operand must have a numeric value.

MATH FUNCTIONS

Tcl supports the following mathematical functions in expressions:

             acos        cos         hypot      sinh
             asin        cosh        log        sqrt
             atan        exp         log10      tan
             atan2       floor       pow        tanh
             ceil        fmod        sin
TYPE CONVERSIONS

Tcl supports the following functions to convert from one representation of a number to another:

 	abs double int round 

If you haven't hit the Run Example button yet, do so now, and examine the results of the example. Play around a bit with the example code, and then go on to the next lesson. :TEXT_END: :LESSON_TEXT_START_LEVEL 1: The command for doing math type operations is expr. The following discussion of the expr command is extracted and adapted from the expr man page.

Expr concatenates arg's (adding separator spaces between them), evaluates the result as a Tcl expression, and returns the value. The operators permitted in Tcl expressions include all the standard math functions. Expressions almost always yield numeric results (integer or floating-point values).

OPERANDS

A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between the operands and operators and parentheses; it is ignored by the expression processor. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x).

Note that the octal and hexadecimal conversion takes place differently in the expr command than in the Tcl substitution phase. In the substitution phase, a \x32 would be converted to an ascii "2", while the expr command would covert a 0x32 to a decimal 50.

If an operand does not have one of the integer formats given above, then it is treated as a floating-point number if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant C compiler. For example, all of the following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interpretation is possible, then an operand is left as a string (and only a limited set of operators may be applied to it).

Operands may be specified in any of the following ways:

OPERATORS

The valid operators are listed below, grouped in decreasing order of precedence:

- + ~ !
Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operators may be applied to string operands, and bit-wise NOT may be applied only to integers.
* / %
Multiply, divide, remainder. None of these operators may be applied to string operands, and remainder may be applied only to integers. The remainder will always have the same sign as the divisor and an absolute value smaller than the divisor.
+ -
Add and subtract. Valid for any numeric operands.
<< >>
Left and right shift. Valid for integer operands only.
&
Bit-wise AND. Valid for integer operands only.
^
Bit-wise exclusive OR. Valid for integer operands only.
|
Bit-wise OR. Valid for integer operands only.
&&
Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise. Valid for numeric operands only (integers or floating-point).
||
Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. Valid for numeric operands only (integers or floating-point).
x?y:z
If-then-else, as in C. If x evaluates to non-zero, then the result is the value of y. Otherwise the result is the value of z. The x operand must have a numeric value.

MATH FUNCTIONS

Tcl supports the following mathematical functions in expressions:

             acos        cos         hypot      sinh
             asin        cosh        log        sqrt
             atan        exp         log10      tan
             atan2       floor       pow        tanh
             ceil        fmod        sin
TYPE CONVERSIONS

Tcl supports the following functions to convert from one representation of a number to another:

 	abs double int round 

If you haven't hit the Run Example button yet, do so now, and examine the results of the example. Play around a bit with the example code, and then go on to the next lesson. :TEXT_END: :LESSON_TEXT_START_LEVEL 2: The driving force for using a computer has traditionally been to perform mathematic manipulations of data. Some languages like BASIC or C do math operations whenever they appear in an expression. Tcl, however, has a special command for doing math operations. The command for doing mathematic operations is expr. The following discussion of the expr command is extracted and adapted from the expr man page.

Expr accepts the strings that follow it, evaluates them as a Tcl expression, and returns the value. The operators permitted in Tcl expressions include all the standard math functions. Mathematic expressions always yield numeric results (integer or floating-point values) while logical operations which return boolean (true or false) results.

Like regular arithmetic, the Tcl math expression consists of operators, operands, and parentheses. White space may be used between the operands, operators and parentheses.

An operand is:

  • An integer value.
  • A floating point value.
  • A $variable_name
  • A text string.

Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x).

Note that the octal and hexadecimal conversion takes place differently in the expr command than in the Tcl substitution phase.

For example, in the Tcl substitution phase, a \x32 (the ascii code for the number 2) would be converted to an ascii "2" while 0x32 would be converted within the expr command to a decimal 50.

This code is a true statement:

expr \x32 == 0x2

It is true because during the substitution phase the \x32 was replaced with an ascii "2", while the 0x2 was passed to expr unchanged. Expr then interprets 0x2 as a hexadecimal 2, compares decimal 2 to hexadecimal 2, and finds that they are equal, which is obviously true.

If an operand does not have one of the integer formats given above, then it is treated as a floating-point number if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant C compiler. For example, all of the following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interpretation is possible, then an operand is left as a string (and only a limited set of operators may be applied to it).

If there are no parentheses to explicitly declare the order of operations, then Tcl does arithmetic operations in a particular order, just as you would do them on paper. This is called the order of precedence of the operators.

As you see in the list below, the command expr "5 + -3 * 4" is evaluated in this order:

  1. The unary minus sign in front of the 3 (3 is negative)
  2. The multiplication sign (-3 is multiplied 4)
  3. The plus sign ( add 5 to the product of -3 and 4)

Operands may be specified in any of the following ways:

  • As an numeric value, either integer or floating- point.
  • As a Tcl variable, using standard $ notation. The variable's value will be used as the operand.

OPERATORS

The valid operators are listed below, grouped in decreasing order of precedence:

- + ~ !
Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operators may be applied to string operands, and bit-wise NOT may be applied only to integers.
* / %
Multiply, divide, remainder. None of these operators may be applied to string operands, and remainder may be applied only to integers. The remainder will always have the same sign as the divisor and an absolute value smaller than the divisor.
+ -
Add and subtract. Valid for any numeric operands.
<< >>
Left and right shift. Valid for integer operands only.
&
Bit-wise AND. Valid for integer operands only.
^
Bit-wise exclusive OR. Valid for integer operands only.
|
Bit-wise OR. Valid for integer operands only.
&&
Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise. Valid for numeric operands only (integers or floating-point).
||
Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. Valid for numeric operands only (integers or floating-point).
x?y:z
If-then-else, as in C. If x evaluates to non-zero, then the result is the value of y. Otherwise the result is the value of z. The x operand must have a numeric value.

MATH FUNCTIONS

Tcl supports the following mathematical functions in expressions:

             acos        cos         hypot      sinh
             asin        cosh        log        sqrt
             atan        exp         log10      tan
             atan2       floor       pow        tanh
             ceil        fmod        sin
TYPE CONVERSIONS

Tcl supports the following functions to convert from one representation of a number to another:

 	abs double int round 
The expr command supports most of the standard C library math calls.

If you haven't hit the Run Example button yet, do so now, and examine the results of the example. Play around a bit with the example code, and then go on to the next lesson. :TEXT_END: :CODE_START: set X 100; set Y 256; set Z [expr "$Y + $X"] set Z_LABEL "$Y plus $X is " puts "$Z_LABEL $Z" puts "The square root of $Y is [expr sqrt($Y)]\n" puts "Because of the precedence rules \"5 + -3 * 4\" is: [expr -3 * 4 + 5]" puts "Because of the parentheses \"(5 + -3) * 4\" is: [expr (5 + -3) * 4]" puts "\n................. more examples of differences between \" and \{" puts {$Z_LABEL [expr $Y + $X]} puts "$Z_LABEL {[expr $Y + $X]}" puts "The command to add two numbers is: \[expr \$a + \$b]" :TEXT_END: