What is the standard (or best supported) big number (arbitrary precision) library for Lua?

孤街浪徒 提交于 2019-11-26 09:39:38

问题


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:

  1. http://oss.digirati.com.br/luabignum/
  2. http://www.tc.umn.edu/~ringx004/mapm-main.html
  3. http://lua-users.org/lists/lua-l/2002-02/msg00312.html (might be identical to #2)
  4. 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?


回答1:


The lmapm library by Luiz Figueiredo, one of the authors of the Lua language.




回答2:


Using lbc instead of lmapm would be easier because lbc is self-contained.

require"bc"
s=bc.pow(2,1000):tostring()
z=0
for i=1,#s do
        z=z+s:byte(i)-("0"):byte(1)
end
print(z)



回答3:


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:

  1. Lua needs to be built with dynamic loading enabled. I use Cygwin, but I changed PLAT in src/Makefile to be linux. The default, none, doesn't enable dynamic loading.

  2. The MAMP needs to be built and installed somewhere that your C compiler can find it. I put libmapm.a in /usr/local/lib/. Next m_apm.h and m_apm_lc.h went to /usr/local/include/.

  3. 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, and LUABIN and editing the declaration of MAMP.

  4. 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!




回答4:


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?




回答5:


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.



来源:https://stackoverflow.com/questions/288707/what-is-the-standard-or-best-supported-big-number-arbitrary-precision-librar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!