您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx 3.3 + lua 学习笔记(01)--- math库

2016-03-31 10:34 543 查看
http://www.yiibai.com/lua/ 挺全面的一个lua教程

lua中 math库 的应用如下

Lua 常用的生成随机数需要用到两个函数:

math.randomseed(xx), math.random([n [, m]])

1. math.randomseed(n) 接收一个整数 n 作为随机序列种子。

2. math.random([n [, m]]) 有三种用法: 无参调用, 产生 (0,1) 之间的浮点随机数; 只有参数 n, 产生 1-n 之间的整数; 有两个参数 n, m, 产生 n-m 之间的随机整数

函数名描述示例结果
pi圆周率math.pi3.1415926535898
abs取绝对值math.abs(-2012)2012
ceil向上取整math.ceil(9.1)10
floor向下取整math.floor(9.9)9
max取参数最大值math.max(2,4,6,8)8
min取参数最小值math.min(2,4,6,8)2
pow计算x的y次幂math.pow(2,16)65536
sqrt开平方math.sqrt(65536)256
mod取模math.mod(65535,2)1
modf取整数和小数部分math.modf(20.12)20 0.12
randomseed设随机数种子math.randomseed(os.time())
random取随机数math.random(5,90)5~90
rad角度转弧度math.rad(180)3.1415926535898
deg弧度转角度math.deg(math.pi)180
expe的x次方math.exp(4)54.598150033144
log计算x的自然对数math.log(54.598150033144)4
log10计算10为底,x的对数math.log10(1000)3
frexp将参数拆成x * (2 ^ y)的形式math.frexp(160)0.625 8
ldexp计算x * (2 ^ y)math.ldexp(0.625,8)160
sin正弦math.sin(math.rad(30))0.5
cos余弦math.cos(math.rad(60))0.5
tan正切math.tan(math.rad(45))1
asin反正弦math.deg(math.asin(0.5))30
acos反余弦math.deg(math.acos(0.5))60
atan反正切math.deg(math.atan(1))45
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: