当前位置:网站首页>数值 - 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 403. Frog crossing the river daily
- Proxmox VE重装后,如何无损挂载原有的数据盘?
- 【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
- NeRF:DeepFake的最终替代者?
- PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
- SlashData开发者工具榜首等你而定!!!
- Jenkins发布uniapp开发的H5遇到的问题
- Flask build API service SQL configuration file
- Sator launched Web3 game "satorspace" and launched hoobi
- Localstorage and sessionstorage
猜你喜欢

Seaborn data visualization

麒麟信安中标国网新一代调度项目!
![[video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial](/img/14/4e7ebfb1ed5b99f8377af9d17d2177.jpg)
[video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial

With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore

麒麟信安加入宁夏商用密码协会

Biped robot controlled by Arduino

Skimage learning (1)

QML初学

MRS离线数据分析:通过Flink作业处理OBS数据

【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
随机推荐
MRS离线数据分析:通过Flink作业处理OBS数据
LeetCode 152. 乘积最大子数组 每日一题
第二十四届中国科协湖南组委会调研课题组一行莅临麒麟信安调研考察
Lex & yacc of Pisa proxy SQL parsing
邮件服务器被列入黑名单,如何快速解封?
麒麟信安中标国网新一代调度项目!
QT视频传输
LeetCode 1696. 跳跃游戏 VI 每日一题
The server is completely broken and cannot be repaired. How to use backup to restore it into a virtual machine without damage?
《产品经理必读:五种经典的创新思维模型》的读后感
【源码解读】| LiveListenerBus源码解读
Solidity函数学习
LeetCode 1654. 到家的最少跳跃次数 每日一题
Solidity 开发环境搭建
LeetCode 1031. 两个非重叠子数组的最大和 每日一题
QT 图片背景色像素处理法
LeetCode 1049. Weight of the last stone II daily question
LeetCode 1981. Minimize the difference between the target value and the selected element one question per day
DevOps 的运营和商业利益指南
Sator a lancé le jeu web 3 "satorspace" et a lancé huobi