在 Lua 中处理大数字 [英] Dealing with big numbers in Lua

查看:31
本文介绍了在 Lua 中处理大数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在需要存储在Lua大量,例如数63680997318088143281752740767766707563546963464218564507450892460763521488675430192536461.

I am in need to store a large number in Lua, for example the number 63680997318088143281752740767766707563546963464218564507450892460763521488675430192536461.

如果我简单地分配给一个变量,我不会得到实际的数字:

If I simple assign to a variable, I don't get the actual number:

local n = 63680997318088143281752740767766707563546963464218564507450892460763521488675430192536461
print(string.format("%.0f",n)) -- prints 63680997318088143929455344863959288468423333130904105158115881995380577784972357899649024

处理大数的可能方法是什么?

What would be the possible turn arounds to handle large numbers?

推荐答案

Lua 数字的精度有限,但您尝试存储的数字超出了可以存储的范围.您需要使用不同的机制来存储它们并对这些数字进行操作.

Lua numbers have limited precision, but you are trying to store a number that exceeds what can be stored. You'll need to use a different mechanism to store them and operate on these numbers.

关键词是bignum"和任意精度数".快速谷歌搜索返回几个纯 Lua 模块(bignumlua-nums) 和一个基于 C 的 (lmapm).另见这个SO答案 其他选项.

The key words are "bignum" and "arbitrary precision numbers". Quick google search returns several pure-Lua modules (bignum and lua-nums) and one C-based one (lmapm). Also see this SO answer for other options.

这篇关于在 Lua 中处理大数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆