Tcl之Math

  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)}]"
时间: 2025-01-13 02:13:57

Tcl之Math的相关文章

Linux R Server: Error [tcl] unknown math function &quot;min&quot;.

问题: 在linux上的部署完成R server后,无法再使用sqldf包了,之前在win7上运行没有问题 如下: > install.packages("sqldf") > library(sqldf) 载入需要的程辑包:gsubfn 载入需要的程辑包:proto 载入需要的程辑包:RSQLite 载入需要的程辑包:DBI 载入需要的程辑包:RSQLite.extfuns 警告信息: In fun(libname, pkgname) : 没有DISPLAY变量,因此没有T

A Math Problem

A Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 237    Accepted Submission(s): 117 Problem Description You are given a positive integer n, please count how many positive integers

数组、Math、JOSN总结

json对象: 1.数组有length属性[尽量使用for循环] 2.而json没有length属性[可以使用for...in...循环] 3.for in 不能遍历页面中的节点对象. for ( var key in json4 ) { alert( key ); alert( json4[key] );}//使用方括号[]取键名,for和for in 可以相互嵌套. delete objName.key : [对象名.要删除的属性名] 在数据传输流程中,json是以文本,即字符串的形式传递的

常用工具类(System,Runtime,Date,Calendar,Math)

一.Sy 一个java.lang包中的静态工具类. 三大字段: static PrintStream err "标准"错误输出流. static InputStream in "标准"输入流. static PrintStream out "标准"输出流. 其他常用方法: 描述系统信息: 获取系统属性信息: static Properties getProperties(): (Properties是Hashtable的子类,也就是Map 的子类

JavaScript中的Math

//math() //Pl document.write(Math.PI+'<br>'); var math=10.1; var math1=10.9; //四舍五入round() document.write(Math.round(math)+'<br>'); //进一法  ceil document.write(Math.ceil(math)+"<br>"); //退一法  floor document.write(Math.floor(math

javascript类型系统——Math对象

× 目录 [1]常量 [2]函数 前面的话 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是一个静态对象,而并没有Math()构造函数.实际上,Math只是一个由Javascript设置的对象命名空间,用于存储数学常量和函数.本文将详细介绍Math对象 new Math();//Uncaught TypeError: Math is not a constructor 常量 Math对象一

LeetCode 50 Pow(x, n)(Math、Binary Search)(*)

翻译 实现pow(x, n). 原文 Implement pow(x, n). 分析 首先给大家推荐维基百科: zh.wikipedia.org/wiki/二元搜尋樹 en.wikipedia.org/wiki/Binary_search_tree 其次,大家也可以看看类似的一道题: LeetCode 69 Sqrt(x)(Math.Binary Search)(*) 然而这题我还是没有解出来,看看别人的解法-- class Solution { private: double myPowHel

Tcl学习之--列表|字典

[列表|字典] Tcl使用列表来处理各种集合,比如一个文件夹中的所有文件,以及一个组件的所有选项.最简单的列表就是包含由任意个空格.制表符.换行符.分隔的任意多个元素的字符串.比如: JerryAlice Mandy David l  lindex命令: --> 获取元素 至少需要两个参数,一个列表和一个索引值,返回取得的元素 如果列表中包含列表的话,访问子列表中的元素就要多个参数 l  llength命令: --> 获取长度 列表命令中可以把一个含有空白符的元素括在括号中,还可以使用反斜线来

Math类

java提供了基本的+,-,*,/算数运算符,同时也提供了更复杂的运算符,比如三角函数,对数元,指数运算 Math是一个工具类.它的构造器被定义为private,因此无法创建Math类的对象,Math类中的所有方法都是 类方法,可以直接通过类名来调用,Math除了提供了大量的静态方法,还提供了两个类变量PI和E public class MathTest{ public static void main(String[] args){ //将弧度转化成角度 System.out.println(