4.18. Lua operator

发布时间 : 2025-10-25 13:34:08 UTC      

Page Views: 10 views

An operator is a special symbol that tells the interpreter to perform specific mathematical or logical operations. Lua provides the following types of operators:

  • Arithmetic operator

  • Relational operator

  • Logical operator

  • Other operators

4.18.1. Arithmetic operator #

The following table lists the common arithmetic operators in the Lua language, setting A to 10 and B to 20:

Operator

Description

Example

+

Addition

A + B output result 30

-

Subtraction

A-B output result-10

*

Multiplication

A * B output result 200

/

Division

B / A output result 2

%

Take the remainder

B An output result 0

^

Multiplying power

A^2 output result 100

-

Minus sign

-A output result-10

//

Integer division operator (> = lua5.3)

5Compact 2 output result 2

4.18.2. Example #

We can understand the application of arithmetic operators more thoroughly through the following examples:

Example #

a = 21 b = 10 c = a + b print("The value of Line 1- c is ", c ) c = a - b print("The value of Line 2- c is ", c ) c = a * b print("The value of Line 3- c is ", c ) c = a / b print("The value of Line 4- c is ", c ) c = a % b print("The value of Line 5- c is ", c ) c = a^2 print("The value of Line 6- c is ", c ) c = -a print("The value of Line 7-c is ", c ) 

The execution results of the above procedures are as follows:

The value of Line 1- c is 31 The value of Line 2- c is 11 The value of Line 3- c is 210 The value of Line 4- c is 2.1 The value of Line 5- c is 1 The value of Line 6- c is 441 The value of Line 7- c is -21 

In lua, / is used as a division operation, the result contains the decimal part, // is used as an integral division operation, and the result does notinclude the decimal part:

Example #

a = 5 b = 2 print("The value of division operation - a/b is ", a / b ) print("The value of integer division - a/b is ", a // b ) 

The execution results of the above procedures are as follows:

Division operation - the value of a/b is 2.5 The value of integer division - a/b is 2 

4.18.3. Relational operator #

The following table lists the common relational operators in the Lua language, setting the value of A to 10 and the value of B to 20:

Operator

Description

Example

==

Equals, checks whether two values are equal, returns true, otherwise returnsfalse

(A == B) is false.

~=

Not equal. Check whether two values are equal. Return true if they are not equal, otherwise return false.

(A ~= B) is true.

>

Greater than, if the value on the left is greater than the value on the right, return true, otherwise return false

(A > B) is false.

<

Less than, if the value on the left is greater than the value on the right, return false, otherwise return true

(A < B) is true.

>=

Greater than or equal to, if the value on the left is greater than or equal to the value on the right, return true, otherwise return false

(A >= B) returns false.

<=

Less than or equal to, if the value on the left is less than or equal to thevalue on the right, return true, otherwise return false

(A <= B) returns true.

4.18.4. Example #

We can understand the application of relational operators more thoroughly through the following examples:

Example #

a = 21 b = 10 if( a == b ) then print("Line 1 - a = b" ) else print("Line 1 - a ≠ b" ) end if( a ~= b ) then print("Line 2 - a ≠ b" ) else print("Line 2 - a = b" ) end if ( a < b ) then print("Line 3 - a < b" ) else print("Line 3 - a ≥ b" ) end if ( a > b ) then print("Line 4 - a > b" ) else print("Line 5 - a ≤ b" ) end -- Modify the values of a and b a = 5 b = 20 if ( a <= b ) then print("Line 5 - a ≤ b" ) end if ( b >= a ) then print("Line 6 - b ≥ a" ) end 

The execution results of the above procedures are as follows:

Line 1 - a ≠ b Line 2 - a ≠ b Line 3 - a ≥ b Line 4 - a > b Line 5 - a ≤ b Line 6 - b ≥ a 

4.18.5. Logical operator #

The following table lists the common logical operators in the Lua language, setting the value of A to true , the value of B is false :

Operator

Description

Example

and

Logic and operators. If A is false, return A, otherwise return B.

(A and B) is false.

or

Logic or operator. If A is true, return A, otherwise return B.

(A or B) is true.

not

Logical non-operator. Contrary to the logic operation result, if the condition is true, the logic is not false.

not (A and B) is true.

4.18.6. Example #

We can understand the application of logical operators more thoroughly through the following examples:

Example #

a = true b = true if ( a and b ) then print("a and b - Condition is true" ) end if ( a or b ) then print("a or b - Condition is true" ) end print("---------Divider---------" ) -- Modify the values of a and b a = false b = true if ( a and b ) then print("a and b - Condition is true" ) else print("a and b - Condition is false" ) end if ( not( a and b) ) then print("not( a and b) - Condition is true" ) else print("not( a and b) - Condition is false" ) end 

The execution results of the above procedures are as follows:

a and b - Condition is true a or b - Condition is true ---------Divider--------- a and b - Condition is false not( a and b) - Condition is true 

4.18.7. Other operators #

The following table lists the concatenation operators in the Lua language and operators that calculate the length of a table or string:

Operator

Description

Example

Concatenate two strings

a..b, where an is “Hello”, b is “World”, and the output is “Hello World”.

#

Unary operator that returns the length of a string or table.

#”Hello” returns 5

4.18.8. Example #

We can better understand the application of concatenation operators and operators that calculate the length of tables or strings through the following examples:

Example #

a = "Hello " b = "World" print("Connection strings a and b ", a..b ) print("B String length ",#b ) print("String Test Length ",#"Test" ) print("Newbie tutorial website length ",#"www.runoob.com" ) 

The execution results of the above procedures are as follows:

Connection strings a and b Hello World B String length 5 String Test Length 4 Newbie tutorial website length 14 

4.18.9. Operator precedence #

The order from high to low:

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

Except ^ and .. all binary operators outside are left concatenated.

a+i < b/2+1 <--> (a+i) < ((b/2)+1) 5+x^2*8 <--> 5+((x^2)*8) a < y and y <= z <--> (a < y) and (y <= z) -x^2 <--> -(x^2) x^y^z <--> x^(y^z)     

4.18.10. Example #

We can get a more thorough understanding of the precedence of Lua language operators through the following examples:

Example #

a = 20 b = 10 c = 15 d = 5 e = (a + b) * c / d;-- ( 30 * 15 ) / 5 print("(a + b) * c / d operation value is :",e ) e = ((a + b) * c) / d; -- (30 * 15 ) / 5 print("((a + b) * c) / d operation value is :",e ) e = (a + b) * (c / d);-- (30) * (15/5) print("(a + b) * (c / d) operation value is :",e ) e = a + (b * c) / d; -- 20 + (150/5) print("a + (b * c) / d operation value is :",e ) 

The execution results of the above procedures are as follows:

(a + b) * c / d operation value is : 90.0 ((a + b) * c) / d operation value is : 90.0 (a + b) * (c / d) operation value is : 90.0 a + (b * c) / d operation value is : 50.0 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。