Handling large numbers in Lua

I need to store a lot of numbers in Lua, for example the number is 63680997318088143281752740767766707563546963464218564507450892460763521488675430192536461.

If I simply assign to a variable, I won’t get the actual number:

local n = 63680997318088143281752740767766707563546963464218564507450892460763521488675430192536461
print(string.format("%.0f",n)) - prints 63680997318088143929455344863959288357899577577899380784972784prev What might happen to the numbers?

The accuracy of Lua numbers is limited, but the number you are trying to store exceeds the number that can be stored. You need to use different mechanisms to store them and manipulate these numbers.

The keywords are "bignum" and "arbitrarily precise numbers". A quick Google search returns several pure Lua modules (bignum And lua-nums) and a C-based module (lmapm). Also see this SO answer for other options.

I need to store a lot of numbers in Lua, such as The number is 63680997318088143281752740767766707563546963464218564507450892460763521488675430192536461.

If I simply assign to a variable, I won’t get the actual number:

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

What changes might happen when dealing with large numbers?

The accuracy of Lua numbers is limited, but the number you are trying to store exceeds the number that can be stored. You need to use a different mechanism to store them and Number operations.

Keywords are "bignum" and "any precise number". A quick Google search returns several pure Lua modules (bignum and lua-nums) and a C-based module ( lmapm). Also see this SO answer for other options.

Leave a Comment

Your email address will not be published.