
指數(shù).jpg
前言#
lua數(shù)學(xué)函數(shù)中的另一大類(lèi)就是和指數(shù)相關(guān)的,當(dāng)然也包括對(duì)數(shù),我把它們放到了一起,很顯然指數(shù)和對(duì)數(shù)是互逆的,放到一起來(lái)總結(jié)便于我們牢記一些問(wèn)題。
內(nèi)容#
math.exp()##
- 原型:math.exp(x)
- 解釋?zhuān)悍祷豦的x次冪,即公式e^x的值(e約等于2.17828)。
math.pow()##
- 原型:math.pow(x, y)
- 解釋?zhuān)悍祷?code>x的
y次冪,即x^y的值。
math.sqrt()##
- 原型:math.sqrt(x)
- 解釋?zhuān)悍祷財(cái)?shù)
x的平方根,也可使用math.pow(x, 0.5)代替。
math.ldexp()##
- 原型:math.ldexp(m, n)
- 解釋?zhuān)悍匠淌剑?code>x = m * 2^n,已知尾數(shù)
m和指數(shù)n,返回?cái)?shù)字x。
math.frexp()##
- 原型:math.frexp(x)
- 解釋?zhuān)悍匠淌剑?code>x = m * 2^n,返回?cái)?shù)字
x的尾數(shù)m和指數(shù)n,其中n為一個(gè)整數(shù),而m的取值范圍是[0.5, 1),比如32 = 0.5 * 2 ^ 6;
math.log()##
- 原型:math.log(x)
- 解釋?zhuān)悍祷匾粋€(gè)數(shù)
x的自然對(duì)數(shù)。
math.log10()##
- 原型:math.log10(x)
- 解釋?zhuān)悍祷匾粋€(gè)數(shù)
x的以10為底的對(duì)數(shù)。
Usage##
- 首先新建一個(gè)文件將文件命名為exponentfunctest.lua然后編寫(xiě)如下代碼:
-- 指數(shù)相關(guān)
local x = 0
print("\nmath.exp("..x..") = "..math.exp(x))
local x = 100
print("math.exp("..x..") = "..math.exp(x))
local x = 2
local y = 8
print("\nmath.pow("..x..", "..y..") = "..math.pow(x, y))
local x = 16
print("\nmath.sqrt("..x..") = "..math.sqrt(x))
-- 科學(xué)計(jì)數(shù)法
local m = 4
local n = 8
print("\nmath.ldexp("..m..", "..n..") = "..math.ldexp(m, n))
x= 32
local m2, n2 = math.frexp(x)
print("\nmath.frexp("..x..") = "..m2.." "..n2)
-- 對(duì)數(shù)相關(guān)
x = 100
print("\nmath.log("..x..") = "..math.log(x))
x = 1000
print("\nmath.log10("..x..") = "..math.log10(x))
- 運(yùn)行結(jié)果

math_exponent.png
總結(jié)#
- 注意科學(xué)計(jì)數(shù)法的使用方法,其中返回值
m的范圍是[0.5,1),其實(shí)在有些版本或者領(lǐng)域,這個(gè)取值范圍志不同的,我們只要知道有區(qū)別就好。 - 注意對(duì)數(shù)的底,因?yàn)楹瘮?shù)長(zhǎng)得比較像,使用時(shí)要注意不要手誤寫(xiě)錯(cuò)了。
- 注意開(kāi)根號(hào)和指數(shù)的關(guān)系,實(shí)際上開(kāi)n次根號(hào)就相當(dāng)于求一個(gè)數(shù)的1/n次冪。