数学库教程

lua-users home
wiki

数学库在《参考手册》第 6.7 节有详细说明。[1] 以下是提供的函数和变量的摘要。本页将对每个函数和变量进行描述并附带示例。
math.abs
math.acos
math.asin
math.atan
math.ceil
math.cos
math.deg
math.exp
math.floor
math.fmod
math.huge
math.log
math.max
math.maxinteger
math.min
math.mininteger
math.modf
math.pi
math.rad
math.random
math.randomseed
math.sin
math.sqrt
math.tan
math.tointeger
math.type
math.ult

math.abs

返回给定值的绝对值,即非负值。
> = math.abs(-100)
100
> = math.abs(25.67)
25.67
> = math.abs(0)
0

math.acos , math.asin

以弧度为单位返回给定值的反余弦和反正弦。
> = math.acos(1)
0
> = math.acos(0)
1.5707963267949
> = math.asin(0)
0
> = math.asin(1)
1.5707963267949

math.atan

以弧度为单位返回反正切值。我们可以自己提供 y/x,或者将 y 和 x 传递给 math.atan 来完成此操作。
> c, s = math.cos(0.8), math.sin(0.8)
> = math.atan(s/c)
0.8
> = math.atan(s,c)
0.8

通常应优先使用两个参数的版本,特别是在将直角坐标转换为极坐标时。它会使用两个参数的符号将结果置于正确的象限,并且在其中一个参数为 0 或非常接近 0 时也能产生正确的值。

> = math.atan(1, 0), math.atan(-1, 0), math.atan(0, 1), math.atan(0, -1)
1.5707963267949 -1.5707963267949        0        3.1415926535898

math.ceil , math.floor

返回不大于或不小于给定整数值(即使是负数)。
> = math.floor(0.5)
0
> = math.ceil(0.5)
1
> = math.floor(-0.5)
-1
> = math.ceil(-0.5)
-0

math.cos , math.sin , math.tan

以弧度为单位返回给定值的余弦、正弦和正切值。
> = math.cos(math.pi / 4)
0.70710678118655
> = math.sin(0.123)
0.12269009002432
> = math.tan(5/4)
3.0095696738628
> = math.tan(.77)
0.96966832796149

math.deg , math.rad

在弧度和度数之间相互转换。
> = math.deg(math.pi)
180
> = math.deg(math.pi / 2)
90
> = math.rad(180)
3.1415926535898
> = math.rad(1)
0.017453292519943

math.exp , math.log

math.exp(myval) 返回 e(自然对数的底数)的 myval 次幂。math.log() 返回其反函数。math.exp(1) 返回 e
> = math.exp(0)
1
> = math.exp(1)
2.718281828459
> = math.exp(27)
532048240601.8
> = math.log(532048240601)
26.999999999998
> = math.log(3)
1.0986122886681

math.min , math.max

从可变长度参数列表中返回最小值或最大值。
> = math.min(1,2)
1
> = math.min(1.2, 7, 3)
1.2
> = math.min(1.2, -7, 3)
-7
> = math.max(1.2, -7, 3)
3
> = math.max(1.2, 7, 3)
7

math.modf

返回给定数字的整数部分和小数部分。
> = math.modf(5)
5       0
> = math.modf(5.3)
5       0.3
> = math.modf(-5.3)
-5      -0.3

如果您想要模(余数),请查找模运算符 %[2]

math.sqrt

返回给定数字的平方根。只允许非负参数。
> = math.sqrt(100)
10
> = math.sqrt(1234)
35.128336140501
> = math.sqrt(-7)
-1.#IND

math.random , math.randomseed

math.random() 生成均匀分布的伪随机数。提供参数会改变其行为。
> = math.random()
0.0012512588885159
> = math.random()
0.56358531449324
> = math.random(100)
20
> = math.random(100)
81
> = math.random(70,80)
76
> = math.random(70,80)
75
upperlower 必须是整数。否则,Lua 会将 upper 转换为整数,有时是 math.floor(upper),有时是 math.ceil(upper),结果可能出乎意料(lower 也是如此)。

math.randomseed() 函数为伪随机数生成器设置一个 *种子*:相同的种子会产生相同的数字序列。

> math.randomseed(1234)
> = math.random(), math.random(), math.random()
0.12414929654836        0.0065004425183874      0.3894466994232
> math.randomseed(1234)
> = math.random(), math.random(), math.random()
0.12414929654836        0.0065004425183874      0.3894466994232

一个好的* '种子' 是 os.time(),但请稍等片刻再调用该函数以获得另一个序列!为了获得良好的随机数,请使用

math.randomseed( os.time() )
如果 Lua 可以从 os.time() 获取毫秒,初始化可以做得更好。另一件需要注意的事情是提供的种子的截断。math.randomseed 将调用底层的 C 函数 srand,它接受一个无符号整数值。Lua 会将种子的值转换为这种格式。如果发生溢出,种子实际上会变成一个糟糕的种子,恕不另行通知 [3](请注意,Lua 5.1 实际上转换为有符号整数 [4],这在 5.2 中已得到纠正)。

尽管如此,在某些情况下我们需要一个可控的序列,就像使用已知种子获得的序列一样。

但请注意!您得到的第一个随机数并非真正“随机”(至少在 Windows 2K 和 OS X 上是这样)。为了获得更好的伪随机数,请在使用它们之前先生成一些随机数。

-- Initialize the pseudo random number generator
math.randomseed( os.time() )
math.random(); math.random(); math.random()
-- done. :-)

-- 这并不完全正确。第一个随机数和第二个以及其他随机数一样好(或一样差)。生成器的优劣取决于其他因素。为了稍微改进内置生成器,我们可以使用如下形式的表:

-- improving the built-in pseudorandom generator
do
   local oldrandom = math.random
   local randomtable
   math.random = function ()
      if randomtable == nil then
         randomtable = {}
         for i = 1, 97 do
            randomtable[i] = oldrandom()
         end
      end
      local x = oldrandom()
      local i = 1 + math.floor(97*x)
      x, randomtable[i] = randomtable[i], x
      return x
   end
end

[5]:为什么 math.random() 在 OSX 和 FreeBSD 上可能给出奇怪的结果?

*...问题似乎是当种子非常接近时,BSD rand() 生成的第一个值也相差甚微。当 Lua 将 rand() 返回的整数转换为实数时,这种差异就会丢失,实际上只保留了结果的高位。当您从 Lua 调用 math.random(1,100) 时,低位差异会消失,您会看到相同的整数结果。

-- improve seeding on these platforms by throwing away the high part of time, 
-- then reversing the digits so the least significant part makes the biggest change
-- NOTE this should not be considered a replacement for using a stronger random function
-- ~ferrix
math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )

还有一个 lrandom[6],一个基于梅森旋转算法的随机数生成库。

math.huge

math.huge 是一个常量。它表示 +无穷大。

> = math.huge
inf
> = math.huge / 2
inf
> = -math.huge
-inf
> = math.huge/math.huge   -- indeterminate
nan
> = math.huge * 0         -- indeterminate
nan
> = 1/0
inf
> = (math.huge == math.huge)
true
> = (1/0 == math.huge)
true

请注意,对 math.huge 的某些操作会返回一个特殊的“非数字”值,显示为 nan。这有点名不副实。nan 是一种数字类型,尽管它与其他数字不同。

> = type(math.huge * 0)
number

另请参阅 FloatingPoint

math.pi

这是常量 Pi 的一部分。

> = math.pi
3.1415926535898

RecentChanges · preferences
编辑 · 历史
最后编辑于 2021 年 12 月 5 日上午 7:16 GMT (差异)