- Lua Quick Start Guide
- Gabor Szauer
- 385字
- 2021-08-05 10:30:41
Arithmetic operators
Arithmetic operators do math; these operators work on numbers to perform addition, subtraction, multiplication, and division. Operators to find the remainder of a division, negate a number, or raise a number to a power are also available in Lua. The negation operator uses a minus sign and is a unary operator; otherwise, all other arithmetic operators are binary operators (that is, they work on two operands).
The addition operator (+) adds two operands together:
x = 7 + 10
y = x + 3
z = x + y
The subtraction operator (-) will subtract the second operand from the first:
x = 8 - 3
y = 10 - x
z = x - y
The multiplication operator (*) will multiply the given operands:
x = 2 * 2
y = x * 3
z = x * y
The division operator (/) will divide the numerator (first operand) by the denominator (second operand):
x = 20 / 10
y = 5 / x
z = x / y
The modulus operator (%) returns the remainder of an integer division. This means both the numerator (first operand) and denominator (second operand) are cast to be integers, divided, and the result is returned. Numbers are cast to integers, not rounded. This means that any decimal numbers are discarded, so 5.1, 5.5, and 5.9 would all simply become 5. Here is an example:
x = 5 % 2 -- result is 1
y = 5.7 % 2 -- 5.7 is treated as 5, result is 1.
z = 5.3 % 2.9 -- result is 1
The negation operator (-) negates a number. This is the only unary arithmetic operator. Here is an example:
x = -5 -- x = -5
y = -x -- y = 5
y = -y -- y = -5
The exponent operator (^) will take the base (first operand) and raise it to the power of the exponent (second operand):
x = 10 ^ 2
y = x ^ 2
z = 3 ^ 3
- 微服務與事件驅動架構
- C++ Builder 6.0下OpenGL編程技術
- ASP.NET動態網頁設計教程(第三版)
- Spring Cloud、Nginx高并發核心編程
- 深度強化學習算法與實踐:基于PyTorch的實現
- C程序設計實踐教程
- Getting Started with Greenplum for Big Data Analytics
- 低代碼平臺開發實踐:基于React
- Python語言實用教程
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- Flowable流程引擎實戰
- SQL Server 2016 從入門到實戰(視頻教學版)
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- 3ds Max 2018從入門到精通
- Java EE實用教程