原始消息:
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 对所有数学运算都使用 double 浮点,包括整数运算(参见 http://lua-users.org/wiki/FloatingPoint )。这为您提供了大约 53 位的精度,这(正如您所注意到的)低于您的需要。
有几种不同的方法可以在 Lua 中获得更好的精度。最好的选择是找到最积极的此类努力并加以利用。在这种情况下,您的问题已经得到解答;查看What is the standard (or best supported) big number (arbitrary precision) library for Lua?
如果您的 Lua 发行版有相应的软件包,简单的答案是 lmapm .
关于lua - 如何在保持精度的同时限制 Lua 中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13144096/