Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 551
Jon Ericson Avatar asked Nov 13 '08 23:11

Jon Ericson


People also ask

What is arbitrary-precision calculator?

Arbitrary-precision arithmetic, also called big-num arithmetic, multiple precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are limited only by the available memory of the host system.

What is arbitrary-precision c++?

Arbitrary-Precision arithmetic, also known as "bignum" or simply "long arithmetic" is a set of data structures and algorithms which allows to process much greater numbers than can be fit in standard data types.

Does Python use arbitrary-precision arithmetic?

Some programming languages such as Lisp, Python, Perl, Haskell and Ruby use, or have an option to use, arbitrary-precision numbers for all integer arithmetic. Although this reduces performance, it eliminates the possibility of incorrect results (or exceptions) due to simple overflow.

How do computers handle large numbers?

Representation You might use an array to hold all the digits of large numbers. A more efficient way would be to use an array of 32 bit unsigned integers and store "32 bit chunks" of the large number. You can think of these chunks as individual digits in a number system with 2^32 distinct digits or characters.


1 Answers

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) 
like image 89
lhf Avatar answered Oct 02 '22 17:10

lhf