lua学习笔记之五(Lua中的数学库)
最后更新于:2022-04-01 10:07:12
### Lua中的数学库
Lua5.1中数学库的所有函数如下表:
math.pi 为圆周率常量 = 3.14159265358979323846
表1
<table border="1" cellspacing="0" cellpadding="0" width="574"><tbody><tr><td style="background:#99CCFF"><p align="center">数学库</p></td><td style="background:#99CCFF"><p align="center">说明</p></td><td style="background:#99CCFF"><p align="center">例子</p></td><td style="background:#99CCFF"><p align="center">方法</p></td></tr><tr><td><p align="center"><span style="color:#89C507">abs</span></p></td><td valign="bottom"><p align="left">取绝对值</p></td><td valign="bottom"><p align="left">math.abs(-15)</p></td><td valign="bottom"><p align="left">15</p></td></tr><tr><td><p align="center"><span style="color:#89C507">acos</span></p></td><td valign="bottom"><p align="left">反余弦函数</p></td><td valign="bottom"><p align="left">math.acos(0.5)</p></td><td valign="bottom"><p align="left">1.04719755</p></td></tr><tr><td><p align="center"><span style="color:#89C507">asin</span></p></td><td valign="bottom"><p align="left">反正弦函数</p></td><td valign="bottom"><p align="left">math.asin(0.5)</p></td><td valign="bottom"><p align="left">0.52359877</p></td></tr><tr><td><p align="center"><span style="color:#89C507">atan2</span></p></td><td valign="bottom"><p align="left">x / y的反正切值</p></td><td valign="bottom"><p align="left">math.atan2(90.0, 45.0)</p></td><td valign="bottom"><p align="left">1.10714871</p></td></tr><tr><td><p align="center"><span style="color:#89C507">atan</span></p></td><td valign="bottom"><p align="left">反正切函数</p></td><td valign="bottom"><p align="left">math.atan(0.5)</p></td><td valign="bottom"><p align="left">0.463647609</p></td></tr><tr><td><p align="center"><span style="color:#89C507">ceil</span></p></td><td valign="bottom"><p align="left">不小于x的最大整数</p></td><td valign="bottom"><p align="left">math.ceil(5.8)</p></td><td valign="bottom"><p align="left">6</p></td></tr><tr><td><p align="center"><span style="color:#89C507">cosh</span></p></td><td valign="bottom"><p align="left">双曲线余弦函数</p></td><td valign="bottom"><p align="left">math.cosh(0.5)</p></td><td valign="bottom"><p align="left">1.276259652</p></td></tr><tr><td><p align="center"><span style="color:#89C507">cos</span></p></td><td valign="bottom"><p align="left">余弦函数</p></td><td valign="bottom"><p align="left">math.cos(0.5)</p></td><td valign="bottom"><p align="left">0.87758256</p></td></tr><tr><td><p align="center"><span style="color:#89C507">deg</span></p></td><td valign="bottom"><p align="left">弧度转角度</p></td><td valign="bottom"><p align="left">math.deg(math.pi)</p></td><td valign="bottom"><p align="left">180</p></td></tr><tr><td><p align="center"><span style="color:#89C507">exp</span></p></td><td valign="bottom"><p align="left">计算以e为底x次方值</p></td><td valign="bottom"><p align="left">math.exp(2)</p></td><td valign="bottom"><p align="left">2.718281828</p></td></tr><tr><td><p align="center"><span style="color:#89C507">floor</span></p></td><td valign="bottom"><p align="left">不大于x的最大整数</p></td><td valign="bottom"><p align="left">math.floor(5.6)</p></td><td valign="bottom"><p align="left">5</p></td></tr><tr><td><p align="center"><span style="color:#89C507">fmod </span><span style="color:#89C507">(</span><span style="color:#89C507">mod</span><span style="color:#89C507">)</span></p></td><td valign="bottom"><p align="left">取模运算</p></td><td valign="bottom"><p align="left">math.mod(14, 5)</p></td><td valign="bottom"><p align="left">4</p></td></tr><tr><td><p align="center"><span style="color:#89C507">frexp</span></p></td><td valign="bottom"><p align="left">把双精度数val分解为数字部分(尾数)和以2为底的指数n,即val=x*2n</p></td><td valign="bottom"><p align="left">math.frexp(10.0)</p></td><td valign="bottom"><p align="left">0.625 4</p></td></tr><tr><td><p align="center"><span style="color:#89C507">ldexp</span></p></td><td valign="bottom"><p align="left">计算value * 2的n次方</p></td><td valign="bottom"><p align="left">math.ldexp(10.0, 3)</p></td><td valign="bottom"><p align="left">80 = 10 * (2 ^3)</p></td></tr><tr><td><p align="center"><span style="color:#89C507">log10</span></p></td><td valign="bottom"><p align="left">计算以10为基数的对数</p></td><td valign="bottom"><p align="left">math.log10(100)</p><p align="left"> </p></td><td valign="bottom"><p align="left">2</p></td></tr><tr><td><p align="center"><span style="color:#89C507">log</span></p></td><td valign="bottom"><p align="left">计算一个数字的自然对数</p></td><td valign="bottom"><p align="left">math.log(2.71)</p></td><td valign="bottom"><p align="left">0.9969</p></td></tr><tr><td><p align="center"><span style="color:#89C507">max</span></p></td><td valign="bottom"><p align="left">取得参数中最大值</p></td><td valign="bottom"><p align="left">math.max(2.71, 100, -98, 23)</p></td><td valign="bottom"><p align="left">100</p></td></tr><tr><td><p align="center"><span style="color:#89C507">min</span></p></td><td valign="bottom"><p align="left">取得参数中最小值</p></td><td valign="bottom"><p align="left">math.min(2.71, 100, -98, 23)</p></td><td valign="bottom"><p align="left">-98</p></td></tr><tr><td><p align="center"><span style="color:#89C507">modf</span></p></td><td valign="bottom"><p align="left">把数分为整数和小数</p></td><td valign="bottom"><p align="left">math.modf(15.98)</p></td><td valign="bottom"><p align="left">15 98</p></td></tr><tr><td><p align="center"><span style="color:#89C507">pow</span></p></td><td valign="bottom"><p align="left">得到x的y次方</p></td><td valign="bottom"><p align="left">math.pow(2, 5)</p></td><td valign="bottom"><p align="left">32</p></td></tr><tr><td><p align="center"><span style="color:#89C507">rad</span></p></td><td valign="bottom"><p align="left">角度转弧度</p></td><td valign="bottom"><p align="left">math.rad(180)</p></td><td valign="bottom"><p align="left">3.14159265358</p></td></tr><tr><td><p align="center"><span style="color:#89C507">random</span></p></td><td valign="bottom"><p align="left">获取随机数</p></td><td valign="bottom"><p align="left">math.random(1, 100)
math.random(100)</p></td><td valign="bottom"><p align="left">获取1-100的随机数</p></td></tr><tr><td><p align="center"><span style="color:#89C507">randomseed</span></p></td><td valign="bottom"><p align="left">设置随机数种子</p></td><td valign="bottom"><p align="left">math.randomseed(os.time())</p></td><td valign="bottom"><p align="left">在使用math.random函数之前必须使用此函数设置随机数种子</p></td></tr><tr><td><p align="center"><span style="color:#89C507">sinh</span></p></td><td valign="bottom"><p align="left">双曲线正弦函数</p></td><td valign="bottom"><p align="left">math.sinh(0.5)</p></td><td valign="bottom"><p align="left">0.5210953</p></td></tr><tr><td><p align="center"><span style="color:#89C507">sin</span></p></td><td valign="bottom"><p align="left">正弦函数</p></td><td valign="bottom"><p align="left">math.sin(math.rad(30))</p></td><td valign="bottom"><p align="left">0.5</p></td></tr><tr><td><p align="center"><span style="color:#89C507">sqrt</span></p></td><td valign="bottom"><p align="left">开平方函数</p></td><td valign="bottom"><p align="left">math.sqrt(16)</p></td><td valign="bottom"><p align="left">4</p></td></tr><tr><td><p align="center"><span style="color:#89C507">tanh</span></p></td><td valign="bottom"><p align="left">双曲线正切函数</p></td><td valign="bottom"><p align="left">math.tanh(0.5)</p></td><td valign="bottom"><p align="left">0.46211715</p></td></tr><tr><td><p align="center"><span style="color:#89C507">tan</span></p></td><td valign="bottom"><p align="left">正切函数</p></td><td valign="bottom"><p align="left">math.tan(0.5)</p></td><td valign="bottom"><p align="left">0.5463024</p></td></tr></tbody></table>
引用博客:http://www.cnblogs.com/whiteyun/archive/2009/08/10/1543040.html