当前位置:网站首页>数值 - number(Lua)
数值 - number(Lua)
2022-07-07 15:38:00 【有趣就行】
数值 - number
在 Lua 5.3 之前版本,number 只有一种数值形式,就是双精度浮点类型。从Lua 5.3 版本后,Lua 的 number 数据类型引入了整形,从而出现了两种数值表示,一种是 64位 整型 integer 和 双精度浮点型 float。
--如下表示
a = 1 --integer
b = 1.0 --float
c = 1e4 --float
当数值常量为 小数 或 指数时,number会当作 float,其余为整数
算术运算
number由于区分了整型和浮点型,所以在算术运算要注意区分其区别。
| 操作符 | 描述 |
|---|---|
| + | 加法 |
| - | 减法 |
| * | 乘法 |
| / | 除法,保留小数位 |
| % | 取余 |
| ^ | 乘幂 |
| - | 负号 |
| //(Lua5.3之后才有的) | 除法,不保留小数位(无论整数还是浮点数) |
+ 加法 整数相加为整数,除此之外为浮点数
- 减法 整数相减为整数,除此之外为浮点数
* 乘法 整数相乘为整数,除此之外为浮点数
\ 除法(保留小数位),无论整数还是浮点数,结果都是浮点数
^ 乘幂,无论整数还是浮点数,结果为浮点数
\\ 除法(不保留小数位),整数相除为整数,其余为浮点数
数学库
- 判断类型
math.type:判断number类型是 integer 还是 float (Lua 5.3)
- 取整
math.floor:向下取整
math.ceil: 向上取整
math.modf:向零取整,返回两个值,一个为整数,一个为小数(小数部分)
a, b = 1.5, -1.5
print(math.floor(a), math.floor(b)) --1 -2
print(math.ceil(a), math.ceil(b)) -- 2 -1
print(math.modf(a)) -- 1 0.5
print(math.modf(b)) -- -1 -0.5
math.maxinteger和math.mininteger分别是 最大整数 和 最小整数,如果整数超出表达(数值溢出),则会出现回环。一般情况下不会溢出,但还是需要注意。
边栏推荐
- Leetcode brush questions day49
- 如何在博客中添加Aplayer音乐播放器
- mysql使用笔记一
- Repair method of firewall system crash and file loss, material cost 0 yuan
- Reflections on "product managers must read: five classic innovative thinking models"
- Sator推出Web3遊戲“Satorspace” ,並上線Huobi
- Module VI
- [Fantan] how to design a test platform?
- Proxmox VE重装后,如何无损挂载原有的数据盘?
- LeetCode 213. 打家劫舍 II 每日一题
猜你喜欢
随机推荐
让保险更“保险”!麒麟信安一云多芯云桌面中标中国人寿, 助力金融保险信息技术创新发展
Read PG in data warehouse in one article_ stat
Seaborn数据可视化
Smart logistics platform: make overseas warehouses smarter
防火墙系统崩溃、文件丢失的修复方法,材料成本0元
QT 图片背景色像素处理法
LeetCode 300. 最长递增子序列 每日一题
Sator推出Web3游戏“Satorspace” ,并上线Huobi
LeetCode 213. 打家劫舍 II 每日一题
The top of slashdata developer tool is up to you!!!
Module VI
mysql使用笔记一
麒麟信安中标国网新一代调度项目!
MySQL implements the query of merging two fields into one field
Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching
Lowcode: four ways to help transportation companies enhance supply chain management
蓝桥杯 决赛 异或变换 100分
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism
Sator推出Web3游戏“Satorspace” ,并上线Huobi








