數學函式庫教學 |
|
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(-100) 100 > = math.abs(25.67) 25.67 > = math.abs(0) 0
> = math.acos(1) 0 > = math.acos(0) 1.5707963267949 > = math.asin(0) 0 > = math.asin(1) 1.5707963267949
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.floor(0.5) 0 > = math.ceil(0.5) 1 > = math.floor(-0.5) -1 > = math.ceil(-0.5) -0
> = 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.pi) 180 > = math.deg(math.pi / 2) 90 > = math.rad(180) 3.1415926535898 > = math.rad(1) 0.017453292519943
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(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(5) 5 0 > = math.modf(5.3) 5 0.3 > = math.modf(-5.3) -5 -0.3
如果您想要模數(餘數),請改為尋找取模運算子 %
。[2]
> = math.sqrt(100) 10 > = math.sqrt(1234) 35.128336140501 > = math.sqrt(-7) -1.#IND
math.random()
產生均勻分佈的偽隨機數字。提供參數會改變其行為。math.random()
產生介於 0 與 1 之間的實數。math.random(upper)
產生介於 1 與 upper 之間的整數(包括兩者)。math.random(lower, upper)
產生介於 lower 與 upper 之間的整數(包括兩者)。> = 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
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() )
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 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 3.1415926535898