码迷,mamicode.com
首页 > 其他好文 > 详细

Tcl之Math

时间:2015-07-28 20:46:24      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

  expr is for Tcl to do math type operations. It takes all of its arguments ("2 + 2" for example) and evaluates the result as a Tcl "expression", and returns the value. Many commands use expr behind the scenes in order to evaluate test expressions, such as ifwhile and for loops.

  tip: enclosing the arguments to expr in curly braces will result in faster code. So do expr {$i * 10} instead of simply expr $i * 10

 

1)  Operators

-  +  ~  !  

  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.

  

**  

  Exponentiation (works on both floating-point numbers and 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 (bit) shift. Valid for integer operands only.
  
<  >  <=  >=
  Relational operators: 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 numeric operands as well as strings, in which case string comparison is used.
  
eq  ne  in  ni  
  compare two strings for equality (eq) or inequality (ne). and two operators for checking if a string is contained in a list (in) or not (ni). These operators all return 1 (true) or 0 (false). Using these operators ensures that the operands are regarded exclusively as strings (and lists), not as possible numbers.
  
&  
  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. 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.

 

2)  Example

 1 set X 100
 2 set Y 256
 3 set Z [expr {$Y + $X}]
 4 set Z_LABEL "$Y plus $X is "
 5 
 6 puts "$Z_LABEL $Z"
 7 puts "The square root of $Y is [expr { sqrt($Y) }]\n"
 8 
 9 puts "Because of the precedence rules \"5 + -3 * 4\"   is: [expr {-3 * 4 + 5}]"
10 puts "Because of the parentheses      \"(5 + -3) * 4\" is: [expr {(5 + -3) * 4}]"
11 
12 set A 3
13 set B 4
14 puts "The hypotenuse of a triangle: [expr {hypot($A,$B)}]"
15 
16 #
17 # The trigonometric functions work with radians ...
18 #
19 set pi6 [expr {3.1415926/6.0}]
20 puts "The sine and cosine of pi/6: [expr {sin($pi6)}] [expr {cos($pi6)}]"
21 
22 #
23 # Working with arrays
24 #
25 set a(1) 10
26 set a(2) 7
27 set a(3) 17
28 set b    2
29 puts "Sum: [expr {$a(1)+$a($b)}]"

 

Tcl之Math

标签:

原文地址:http://www.cnblogs.com/mengdie/p/4683618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!