rsc
rsc

Reputation: 10679

Dealing with big numbers in Lua

I am in need to store a large number in Lua, for example the number 63680997318088143281752740767766707563546963464218564507450892460763521488675430192536461.

If I simple assign to a variable, I don't get the actual number:

local n = 63680997318088143281752740767766707563546963464218564507450892460763521488675430192536461
print(string.format("%.0f",n)) -- prints 63680997318088143929455344863959288468423333130904105158115881995380577784972357899649024

What would be the possible turn arounds to handle large numbers?

Upvotes: 8

Views: 9151

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26774

Lua numbers have limited precision, but you are trying to store a number that exceeds what can be stored. You'll need to use a different mechanism to store them and operate on these numbers.

The key words are "bignum" and "arbitrary precision numbers". Quick google search returns several pure-Lua modules (bignum and lua-nums) and one C-based one (lmapm). Also see this SO answer for other options.

Upvotes: 10

Related Questions