Lua 的标准(或最好支持的)大数(任意精度)库是什么?
我正在处理大量无法四舍五入的数字。 使用 Lua 的标准数学库,似乎没有方便的方法来保持精度超过某些内部限制。 我还看到有几个库可以加载以处理大数字:
- http://oss.digirati .com.br/luabignum/
- http://www.tc .umn.edu/~ringx004/mapm-main.html
- http://lua-users.org/lists/lua-l/2002-02/msg00312.html(可能与 #2 相同)
- http://www.gammon.com.au/scripts/doc.php?general=lua_bc (但我可以'找不到任何来源)
此外,C 中有许多库可以如果绑定已建立,则可以从 Lua 调用。
您是否有使用过其中一个或多个库的经验?
I'm working with large numbers that I can't have rounded off. Using Lua's standard math library, there seem to be no convenient way to preserve precision past some internal limit. I also see there are several libraries that can be loaded to work with big numbers:
- http://oss.digirati.com.br/luabignum/
- http://www.tc.umn.edu/~ringx004/mapm-main.html
- http://lua-users.org/lists/lua-l/2002-02/msg00312.html (might be identical to #2)
- http://www.gammon.com.au/scripts/doc.php?general=lua_bc (but I can't find any source)
Further, there are many libraries in C that could be called from Lua, if the bindings where established.
Have you had any experience with one or more of these libraries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 lbc 代替 lmapm会更容易,因为 lbc 是独立的。
Using lbc instead of lmapm would be easier because lbc is self-contained.
我使用 Norman Ramsey 的建议来解决 Project Euler 问题 #16。 我认为问题的关键在于准确计算 303 位整数,这并不是剧透。
以下是我安装和使用该库所需的步骤:
Lua 需要在启用动态加载的情况下构建。 我使用 Cygwin,但我将
src/Makefile
中的PLAT
更改为linux
。 默认值none
不启用动态加载。MAMP 需要构建并安装在您的位置C编译器可以找到它。 我将
libmapm.a
放在/usr/local/lib/
中。 接下来m_apm.h
和m_apm_lc.h
转到/usr/local/include/
。lmamp 的 makefile 需要更改到 Lua 和 MAMP 库的正确位置。 对我来说,这意味着取消注释
LUA
、LUAINC
、LUALIB
和LUABIN
的第二个声明并编辑声明MAMP
。最后,
mapm.so
需要放置在Lua可以找到的地方。 我把它放在/usr/local/lib/lua/5.1/
。谢谢大家的建议!
I used Norman Ramsey's suggestion to solve Project Euler problem #16. I don't think it's a spoiler to say that the crux of the problem is calculating a 303 digit integer accurately.
Here are the steps I needed to install and use the library:
Lua needs to be built with dynamic loading enabled. I use Cygwin, but I changed
PLAT
insrc/Makefile
to belinux
. The default,none
, doesn't enable dynamic loading.The MAMP needs to be built and installed somewhere that your C compiler can find it. I put
libmapm.a
in/usr/local/lib/
. Nextm_apm.h
andm_apm_lc.h
went to/usr/local/include/
.The makefile for lmamp needs to be altered to the correct location of the Lua and MAMP libraries. For me, that means uncommenting the second declaration of
LUA
,LUAINC
,LUALIB
, andLUABIN
and editing the declaration ofMAMP
.Finally,
mapm.so
needs to be placed somewhere that Lua will find it. I put it at/usr/local/lib/lua/5.1/
.Thank you all for the suggestions!
作者之一 Luiz Figueiredo 的 lmapm 库 Lua 语言的。
The lmapm library by Luiz Figueiredo, one of the authors of the Lua language.
我无法真正回答,但我会添加 LGMP,GMP 绑定。 不曾用过。
不是我的专业领域,但我希望 GNU 多精度算术库成为这里的标准,不是吗?
I can't really answer, but I will add LGMP, a GMP binding. Not used.
Not my field of expertise, but I would expect the GNU multiple precision arithmetic library to be quite a standard here, no?
虽然不是任意精度,Lua decNumber,IBM decNumber 的 Lua 5.1 包装器,实现了提议的通用十进制算术标准 IEEE 754r。 它具有 Lua 5.1 算术运算符等,可以完全控制舍入模式,工作精度高达 69 位十进制数字。
Though not arbitrary precision, Lua decNumber, a Lua 5.1 wrapper for IBM decNumber, implements the proposed General Decimal Arithmetic standard IEEE 754r. It has the Lua 5.1 arithmetic operators and more, full control over rounding modes, and working precision up to 69 decimal digits.
有几个库可以解决这个问题,每个库都有你的优点
和缺点,最好的选择取决于您的要求。 我会说
lbc 是一个不错的首选,如果它
满足您的要求或 Luiz Figueiredo 的任何其他要求。 对于最有效的,我想是使用 GMP 绑定的任何一个,因为 GMP 是一个用于处理大整数的标准 C 库,并且经过了很好的优化。
不过,如果您正在寻找纯 Lua 版本,lua-bint
库可能是处理大整数的一个选项,
我不会说这是最好的,因为有更有效的
以及上面提到的完整的,但通常它们需要编译C代码
或者设置起来可能很麻烦。 然而,当比较纯Lua大整数库时
根据您的用例,它可能是一个有效的选择。 图书馆有记录,
代码完全被测试覆盖并且有很多例子。 但接受这个建议并给予批准
盐,因为我是图书馆的作者。
如果您的计算机中已有 luarocks,则可以使用 luarocks 进行安装,或者只需下载
bint.lua
文件在您的项目中,因为它除了需要 Lua 5.3+ 之外没有其他依赖项。
这是一个小例子,使用它来解决 Project Euler 中的问题 #16
(在之前的回答中提到过):
There are several libraries for the problem, each one with your advantages
and disadvantages, the best choice depends on your requeriments. I would say
lbc is a good first pick if it
fulfills your requirements or any other by Luiz Figueiredo. For the most efficient one I guess would be any using GMP bindings as GMP is a standard C library for dealing with large integers and is very well optimized.
Nevertheless in case you are looking for a pure Lua one, lua-bint
library could be an option for dealing with big integers,
I wouldn't say it's the best because there are more efficient
and complete ones such the ones mentioned above, but usually they requires compiling C code
or can be troublesome to setup. However when comparing pure Lua big integer libraries
and depending in your use case it could perhaps be an efficient choice. The library is documented,
code fully covered by tests and have many examples. But take this recommendation with grant of
salt because I am the library author.
To install you can use luarocks if you already have it in your computer or simply download the
bint.lua
file in your project, as it has no other dependencies other than requiring Lua 5.3+.
Here is a small example using it to solve the problem #16 from Project Euler
(mentioned in previous answers):