Lua 学习--6 运算

2.5.1 – Arithmetic Operators

Lua supports the usual arithmetic operators: the binary + (addition), - (subtraction), * (multiplication), / (division), % (modulo), and ^ (exponentiation); and unary - (negation). If the operands are numbers, or strings that can be converted to numbers (see §2.2.1), then all operations have the usual meaning. Exponentiation works for any exponent. For instance, x^(-0.5) computes the inverse of the square root of x. Modulo is defined as

     a % b == a - math.floor(a/b)*b

That is, it is the remainder of a division that rounds the quotient towards minus infinity.

算术运算
加 +-*/^%

 

2.5.2 – Relational Operators

The relational operators in Lua are

     ==    ~=    <     >     <=    >=

These operators always result in false or true.

Equality (==) first compares the type of its operands. If the types are different, then the result is false. Otherwise, the values of the operands are compared. Numbers and strings are compared in the usual way. Objects (tables, userdata, threads, and functions) are compared by reference: two objects are considered equal only if they are the same object. Every time you create a new object (a table, userdata, thread, or function), this new object is different from any previously existing object.

You can change the way that Lua compares tables and userdata by using the "eq" metamethod (see §2.8).

The conversion rules of §2.2.1 do not apply to equality comparisons. Thus, "0"==0 evaluates to false, and t[0] and t["0"] denote different entries in a table.

The operator ~= is exactly the negation of equality (==).

The order operators work as follows. If both arguments are numbers, then they are compared as such. Otherwise, if both arguments are strings, then their values are compared according to the current locale. Otherwise, Lua tries to call the "lt" or the "le" metamethod (see §2.8). A comparison a > b is translated to b < a and a >= b is translated to b <= a.

关系运算
相等  ==  如果类型不同直接返回false, 相同比较运算数,数字,字符串按照通常方法进行,对象比较要看引用是否相同。正常情况下new一个对象与之前存在的对象实例都是不相等的,
    table 和userdata可以用eq进行比较(后面介绍),在关系运算中类型自动转换不适用(在算数运算中 1+'3'=4) 所以, "0"==0 结果为: false, t[0] 和t["0"]代表不同的table入口 不相等 ~= 就是 == 的否定 小于 < 大于 > 小于等于 <= 大于等于 >=

 

2.5.3 – Logical Operators

The logical operators in Lua are andor, and not. Like the control structures (see §2.4.4), all logical operators consider both false and nil as false and anything else as true.

The negation operator not always returns false or true. The conjunction operator and returns its first argument if this value is false or nil; otherwise, and returns its second argument. The disjunction operator or returns its first argument if this value is different from nil and false; otherwise, or returns its second argument. Both and and or use short-cut evaluation; that is, the second operand is evaluated only if necessary. Here are some examples:

     10 or 20            --> 10
     10 or error()       --> 10
     nil or "a"          --> "a"
     nil and 10          --> nil
     false and error()   --> false
     false and nil       --> false
     false or nil        --> nil
     10 and 20           --> 20

(In this manual, --> indicates the result of the preceding expression.)

 

注意逻辑运算的返回值:
A and  B: 如果 A 是fasle 或 nil 则直接返回 A,否则返回B
A or B :如果A不是false 也不是 nil (也就是true)  返回 A,否则返回B

例子:
     10 or 20            --> 10
     10 or error()       --> 10
     nil or "a"          --> "a"
     nil and 10          --> nil
     false and error()   --> false
     false and nil       --> false
     false or nil        --> nil
     10 and 20           --> 20

 

 

2.5.4 – Concatenation

The string concatenation operator in Lua is denoted by two dots ('..'). If both operands are strings or numbers, then they are converted to strings according to the rules mentioned in §2.2.1. Otherwise, the "concat" metamethod is called (see §2.8).

 

连接符号 
..
用于链接两个表达式(适用于 数值 或 字符串,其它类型用 contat)

 

2.5.5 – The Length Operator

The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte).

The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is niln can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).

运算数的长度 用 # 获取
字符串的长度是 字符串字节数(一般情况下每一个字符占一个字节,中文是2个字节)
如果是一个普通的数组(所有元素值都不是nil)那么数组 长度是最后一个值所在索引。
如果数组元素中存在nil ,那么数组长度就是nil所在索引的前一个索引 这个说法经过验证是错的
print('-----------------')
ok=20
arr={1,12,5,'x',nil,10,ok}
print(arr[6])
print('array Length: '..#arr)

执行结果是:

-----------------
10
array Length: 7

可以看到第5个元素是 nil, 但是数组长度 :7 并不是官方提到的 4

 

 

2.5.6 – Precedence 

运算优先级

Operator precedence in Lua follows the table below, from lower to higher priority:

     or
     and
     <     >     <=    >=    ~=    ==
     ..
     +     -
     *     /     %
     not   #     - (unary)
     ^

As usual, you can use parentheses to change the precedences of an expression. The concatenation ('..') and exponentiation ('^') operators are right associative. All other binary operators are left associative.

 

运算优先级
默认顺序可以通过括号改变

 

 

未完待续 。。。。

 

posted on 2023-03-05 11:31  hztech  阅读(6)  评论(0编辑  收藏  举报

导航