Tcl之Math
expr is for Tcl to do math operations. It takes all of its arguments ("2 + 2" for example) and evaluates the result as a Tcl "expression". Many commands use expr
behind the scenes in order to evaluate test expressions, such as if
, while
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
set X 100 ; # 100
set Y 256 ; # 256
set Z [expr {$Y + $X}] ; # 356
set Z_LABEL "$Y plus $X is " ; # 256 plus 100 is 356 puts "$Z_LABEL $Z"
puts "The square root of $Y is [expr { sqrt($Y) }]\n" ; # The square root of 256 is 16.0
# Because of the precedence rules "5 + -3 * 4" is: -7
puts "Because of the precedence rules \"5 + -3 * 4\" is: [expr {-3 * 4 + 5}]"
# Because of the parentheses "(5 + -3) * 4" is: 8
puts "Because of the parentheses \"(5 + -3) * 4\" is: [expr {(5 + -3) * 4}]" set A 3
set B 4
14 # The hypotenuse of a triangle: 5.0
puts "The hypotenuse of a triangle: [expr {hypot($A,$B)}]"
# The trigonometric functions work with radians ...
#
set pi6 [expr {3.1415926/6.0}]
puts "The sine and cosine of pi/6: [expr {sin($pi6)}] [expr {cos($pi6)}]" #
# Working with arrays
#
set a(1) 10
set a(2) 7
set a(3) 17
set b 2
puts "Sum: [expr {$a(1)+$a($b)}]"
Tcl之Math的更多相关文章
- 用Tcl/Tk脚本计算圆周率
读了阮一峰的蒙特卡罗方法入门,用概率统计的方式求解棘手的数学问题还挺有意思的,尤其是利用正方形和它的内切圆之间的面积关系来建模求解圆周率的方法精巧又简单,比投针实验好理解也好实现多了.建模可不是M ...
- Floating Point Math
Floating Point Math Your language isn't broken, it's doing floating point math. Computers can only n ...
- JavaScript中Math对象的方法介绍
1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Chrome V8引擎系列随笔 (1):Math.Random()函数概览
先让大家来看一幅图,这幅图是V8引擎4.7版本和4.9版本Math.Random()函数的值的分布图,我可以这么理解 .从下图中,也许你会认为这是个二维码?其实这幅图告诉我们一个道理,第二张图的点的分 ...
- Math.random()
Math.random() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() //获取年份b ...
- Math.abs()方法 取绝对值
定义和用法 abs() 方法可返回数的绝对值. 语法 Math.abs(x) 参数 描述 x 必需.必须是一个数值. 返回值 x 的绝对值. 实例 在本例中,我将取得正数和负数的绝对值: <sc ...
- Tcl internal variables
Tcl internal variables eryar@163.com 在Tcl中内置了一些变量,并赋予了一定的功能.内置变量列表如下: 变量名称 功能描述 argc 指命令行参数的个数. argv ...
- C标准头文件<math.h>
定义域错误可以理解为超出了函数的适用范围,如果发生了定义域错误,设errno为EDOM 如果结果不能表示为double值,则发生值域错误,如果结果上溢,则函数返回HUGE_VAL的值,设errno为E ...
随机推荐
- Ubuntu 16.04 LTS 搭建LAMP
1. LAMP是一系列自由和开源软件的集合,包含了Linux.Web服务器(Apache).数据库服务器(MySQL)和PHP(脚本语言). Apache2 Web 服务器的安装 sudo apt i ...
- iOS_开发中遇到的那些问题_1
[自编号:60][AutoLayout中,怎样让ImageView保持固定的宽高比?比如1:1] 先将imageViewframe手动写成:宽20,高20,再勾选Aspect Ratio加入宽高比约束 ...
- Spark修炼之道(基础篇)——Linux大数据开发基础:第二节:Linux文件系统、文件夹(一)
本节主要内容 怎样获取帮助文档 Linux文件系统简单介绍 文件夹操作 訪问权限 1. 怎样获取帮助文档 在实际工作过程其中,常常会忘记命令的使用方式.比如ls命令后面能够跟哪些參数,此时能够使用ma ...
- TextView设置成仅仅读
TextView设置成仅仅读 方法一:代理 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { return NO; } 方法二:设 ...
- python爬虫【第2篇】【多进程】
一.多进程 1.fork方法(os模块,适用于Lunix系统) fork方法:调用1次,返回2次.原因:操作系统经当前进程(父进程)复制出一份进程(子进程),两个进程几乎完全相同,fork方法分别在父 ...
- 【bzoj1042】[HAOI2008]硬币购物
首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案. 为了避免重复的方案被转移,所以我们以硬币种类为第一层循环,这样阶段性的增加硬币. 一定要注意这个第一层循环要 ...
- linux用于文件解压缩的命令
1 gzip gzip -<压缩率> 压缩率用数字(1-9)来表示,越大,则压缩率越大. 2 bz2 解压bz2 bzip2 -d filename.bz2
- Coolite Toolkit介绍
Coolite Toolkit非常棒的控件 Coolite Toolkit介绍 Coolite Toolkit 是一个支持ASP.NET AJAX的Web控件. Coolite Toolkit是基 ...
- bzoj3295 洛谷P3157、1393 动态逆序对——树套树
题目:bzoj3295 https://www.lydsy.com/JudgeOnline/problem.php?id=3295 洛谷 P3157(同一道题) https://www.luogu.o ...
- HTML5移动Web开发
1. 响应式web设计 说到这个,移动开发面对的屏幕尺寸那叫一个丰富,其中安卓阵营就够让人头痛的.我们在PC端常用的两种布局方式就是固定布局和弹性布局,前者设置一个绝大多数电脑能正常显示的固定宽度居中 ...