Math.type(3.0) --> float Math.type(1) --> integer
支持十六进制浮点数
string.format("%a", 419) --> 0x1.ap+8 0xa.bP2 --> (10 + 11/16) * 2^2 = 42.75
floor运算符 //
> 3 // 2 --> 1 > 3 / 2 --> 1.5 > 6.0 // 2 --> 3.0
实数类型的模运算
x - x % 0.01 --> 保留两位小数的结果
math库
随机数发生器
math.random() math.random() --> [0, 1)伪随机实数 math.random(n) --> n是正整数,[1, n]伪随机整数 math.random(l, r) --> l, r整数,[l, r] math.randomseed(os.time()) --> 设定随机数种子
取整函数
向nearest integer取整:分类讨论整数和浮点数(先判断floor的结果是否等于自身,若非则+0.5再floor)
--> floor, ceil略 math.modf(x) --> 向零的方向取整
数据表示范围
标准Lua使用64位整数,精简Lua使用32位
ㅤ | 标准Lua | 精简Lua |
Integer | 64bits | 32bits |
Float | 64bits双精度 | 32bits单精度 |