Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle large numbers with precision in Redis Lua

I need to handle large numbers in Lua which goes with Redis. Normally you would do that like:

require"bc"
bc.mul(...)
bc.mod(...)

etc. But unfortunately Redis Lua doesn't support "require". The only approach I've found is inserting a large numbers library written in lua itself directly into the script.

The only such library I could get my hands on: oss.digirati.com.br/luabignum/index.htm

I can strip the library for the purposes of a concrete script but it still remains huge. Is there any way to handle large numbers in Redis Lua more efficiently?

UPDATE1: What if I save the whole library into a key and then access it like:

local BigNumLib = KEYS[1];
BigNumLib.BigNum.mul(KEYS[2],KEYS[3]);

I'm not sure of the syntax and perhaps I'll need to enclose all the library in a lua table {}.

like image 907
MidnightCoder Avatar asked Jun 25 '13 17:06

MidnightCoder


1 Answers

I'm not familiar with how Redis handles Lua code, but why is inserting the library itself into your code is a problem? you should be able to do something like this:

local bc = function()
  -- insert the code of BigNum.lua here
  return BigNum
end
bc.mul(....)

The code has probably been written before Lua 5.1, so I don't know if there are any compatibility issues, but this should at least give you a start.

like image 177
Paul Kulchenko Avatar answered Oct 14 '22 16:10

Paul Kulchenko