7

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?

rsc
  • 10,348
  • 5
  • 39
  • 36
  • 2
    If you just want to store the numbers, use strings. – lhf Jul 24 '17 at 12:50
  • 1
    Thank you for the comment professor. On my case that huge number is result of a series of sum operations, so just storing as string was not option. I was able to successfully get that number correctly using the BigNum library. – rsc Jul 24 '17 at 18:12

1 Answers1

8

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.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56