如何在Lua中精确限制数字大小,同时避免精度损失?

2026-04-01 19:421阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计322个文字,预计阅读时间需要2分钟。

如何在Lua中精确限制数字大小,同时避免精度损失?

原始信息:起始信息: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...

改写内容:我需要计算两个64位数的乘积,但Lua在处理大数时精度丢失。(例如,99999999999999999显示为100000000000000000)乘积后,我需要一个64位的结果,所以需要一种限制方法来...

原始信息:

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

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

如何在Lua中精确限制数字大小,同时避免精度损失?

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

本文共计322个文字,预计阅读时间需要2分钟。

如何在Lua中精确限制数字大小,同时避免精度损失?

原始信息:起始信息: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...

改写内容:我需要计算两个64位数的乘积,但Lua在处理大数时精度丢失。(例如,99999999999999999显示为100000000000000000)乘积后,我需要一个64位的结果,所以需要一种限制方法来...

原始信息:

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

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

如何在Lua中精确限制数字大小,同时避免精度损失?

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