当前位置:网站首页>数值 - 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
- centos7安装mysql笔记
- LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
- 服务器彻底坏了,无法修复,如何利用备份无损恢复成虚拟机?
- [Fantan] how to design a test platform?
- MRS离线数据分析:通过Flink作业处理OBS数据
- Localstorage and sessionstorage
- Problems encountered in Jenkins' release of H5 developed by uniapp
- LeetCode 1477. Find two subarrays with sum as the target value and no overlap
猜你喜欢

科普达人丨一文弄懂什么是云计算?

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

自定义View必备知识,Android研发岗必问30+道高级面试题

skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配

User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions

LeetCode刷题day49

What is cloud computing?

Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region

Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination

【图像传感器】相关双采样CDS
随机推荐
How to mount the original data disk without damage after the reinstallation of proxmox ve?
蓝桥杯 决赛 异或变换 100分
LeetCode 403. 青蛙过河 每日一题
服务器彻底坏了,无法修复,如何利用备份无损恢复成虚拟机?
麒麟信安加入宁夏商用密码协会
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
QT picture background color pixel processing method
Sator推出Web3游戏“Satorspace” ,并上线Huobi
[Seaborn] combination chart: facetgrid, jointgrid, pairgrid
Matplotlib绘图界面设置
LeetCode 300. 最长递增子序列 每日一题
A tour of grpc:03 - proto serialization / deserialization
LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
LeetCode 1477. Find two subarrays with sum as the target value and no overlap
智慧物流平台:让海外仓更聪明
SlashData开发者工具榜首等你而定!!!
【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
LeetCode 1696. Jumping game VI daily question
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅