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

Jon Ericson picture Jon Ericson · Nov 14, 2008 · Viewed 8.7k times · Source

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?

Answer

lhf picture lhf · May 14, 2009

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

local bc = 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)