当前位置:网站首页>Number - number (Lua)

Number - number (Lua)

2022-07-07 19:14:00 Just be interesting

The number - number

stay Lua 5.3 The previous version ,number There is only one numerical form , Double precision floating point type . from Lua 5.3 After version ,Lua Of number Data types introduce shaping , Thus, there are two numerical representations , One is 64 position integer integer and Double precision floating point float.

-- As follows 
a = 1 --integer
b = 1.0 --float
c = 1e4 --float

When the numerical constant is decimal or Index time ,number I'll take it as float, The rest are integers

Arithmetic operations

number Because of the distinction between integer and floating point , Therefore, we should pay attention to distinguish the differences in arithmetic operations .

The operator describe
+ Add
- Subtraction
* Multiplication
/ division , Keep decimal places
% Remainder
^ Power
- Minus sign
//(Lua5.3 Only after that ) division , Don't keep decimal places ( Whether it's an integer or a floating point number )

+ Add Integers are added into integers , In addition, floating point numbers

- Subtraction Integers are subtracted into integers , In addition, floating point numbers

* Multiplication Multiply integers into integers , In addition, floating point numbers

\ division ( Keep decimal places ), Whether it's an integer or a floating point number , The results are all floating-point numbers

^ Power , Whether it's an integer or a floating point number , The result is a floating-point number

\\ division ( Don't keep decimal places ), Divide an integer by an integer , The rest are floating point numbers

Math library

  • Judgment type

math.type: Judge number The type is integer still float (Lua 5.3)

  • integer

math.floor: Rounding down

math.ceil: Rounding up

math.modf: Round to zero , Returns two values , One is an integer , One is a decimal ( The fractional part )

a, b = 1.5, -1.5
print(math.floor(a), math.floor(b)) --1 -2
print(math.ceil(a), math.ceil(b)) -- 2 -1
print(math.modf(a)) -- 1 0.5
print(math.modf(b)) -- -1 -0.5

math.maxinteger and math.mininteger Namely Maximum integer and Minimum integer , If the integer exceeds the expression ( Value overflow ), There will be a loop .

Generally, it will not overflow , But we still need to pay attention to .

原网站

版权声明
本文为[Just be interesting]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071515233436.html