lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello Luaphiles,

I've written a binding for GNU's GMP (multi-precision arithmetic) that supports all the functionality in the mpz_xxx() calls. Those are the calls for integers. Things like adding, raising to powers, bitwise operations, prime finding, gcd, Fibonacci, binomial coefficients, Jacobi symbol evaluation, etc. The only functionality that I did not include is import/export for raw data.

For more info on GMP, see http://www.swox.com/gmp/.

I'm wondering if anyone has done a complete binding for GMP, to include the rationals and higher-precision floating point. Googling and checking the archives revealed little, other than LHF's bc binding.

If you know of such a creation, please let me know.

If anyone is interested I'd be happy to share what I have. I've uploaded it to http://www.dkrogers.com/doug/lua/. No documentation yet. Some of the functions return extra values rather than having multiple versions of the functions that return less.

It's pretty cool to type things like what I show below. I've grabbed these from different sessions, but I *think* they'll run in order!

$ lua
Lua 5.0.2  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> n=mpz.new'0x12341234123412341234'
> print(n)
85963139076892835189300
> print(n:gcd(81234))
2       -15080  15957901091655513142953
> gcd,s,t=n:gcd(81234)
> print(s*n+t*81234)
2
> print(n:tobase(32))
28q14d0i6g9384hk
> print(mpz.new('2h',32))
81
> print(mpz.binomial(52,5))   -- different poker hands
2598960
> print(mpz.binomial(52,13))  -- different bridge hands
635013559600
> print(mpz.factorial(52))    -- different decks
80658175170943878571660636856403766975289505440883277824000000000000
> print(mpz.pow(123,123):digits())
258
> print(mpz.pow(123,123))
114374367934617190099880295228066276746218078451850229775887975052369504785666896446606568365201542169649974727730628842345343196581134895919942820874449837212099476648958359023796078549041949007807220625356526926729664064846685758382803707100766740220839267
> print(mpz.new'123456234523452345'^8)
53964009874408881677678814038234547604707449351118557650127008485923476806834554220900531693898808589424301417339043590844248778687890625
> print(n:bxor(1))
85963139076892835189301
> print(n:setbit(11))
85963139076892835191348
> print(n:setbit(12,0))
85963139076892835187252
> print(mpz.bxor(2099,1099))
3192
> print(mpz.bxor(2099,1099):tobase(-16))
C78
> n=mpz.new(1234)
> for i=n:digits(2)-1,0,-1 do io.write(n:getbit(i)) end print()
10011010010
> print(n:tobase(2))
10011010010
>

Doug