当前位置 : 主页 > 网络编程 > lua >

如何在保持精度的同时限制Lua中的数字?

来源:互联网 收集:自由互联 发布时间:2021-06-23
原始信息: I need to multiply two 64 bit numbers, but Lua is losing precision with big numbers. (for example 99999999999999999 is shown as 100000000000000000) After multiplying I need a 64 bit solution , so I need a way to limit the so
原始信息:

I need to multiply two 64 bit numbers, but Lua is losing precision
with big numbers. (for example 99999999999999999 is shown as
100000000000000000) After multiplying I need a 64 bit solution,
so I need a way to limit the solution to 64 bits. (I know, if the
solution would be precise, I could just use % 0x10000000000000000,
so that would work too)

编辑:使用Lua 5.3和新的64位整数支持,此问题不再存在.整齐.

Lua对所有数学使用双精度浮点数,包括整数运算(见 http://lua-users.org/wiki/FloatingPoint).这为您提供了大约53位的精度,(正如您所注意到的)低于您的需要.

有几种不同的方法可以在Lua中获得更好的精度.你最好的选择是找到最积极的努力并捎带它.在这种情况下,您的问题已经得到了回答;查看What is the standard (or best supported) big number (arbitrary precision) library for Lua?

如果你的Lua发行版有它的包,那么简单的答案就是lmapm.

网友评论