How do I limit numbers in Lua while keeping precision?

limit, lua, numbers

Solution

Lua uses double-precision floating points for all math, including integer arithmetic (see http://lua-users.org/wiki/FloatingPoint). This gives you about 53 bits of precision, which (as you've noticed) is less than you need.

There are a couple of different ways to get better precision in Lua. Your best bet is to find the most active such effort and piggy-back off it. In that case, your question has already been answered; check out What is the standard (or best supported) big number (arbitrary precision) library for Lua?

If your Lua distribution has packages for it, the easy answer is lmapm.

Problem

Original Message: I need to multiply two 64 bit numbers, but Lua is losing precision with big numbers. (for example 99999999999999999 is shown as 100000000000000000) After multiplying I need a 64 bit solution, so I need a way to limit the solution to 64 bits. (I know, if the solution would be precise, I could just use % 0x10000000000000000, so that would work too) EDIT: With Lua 5.3 and the new 64 bit integer support, this problem doesn't exist anymore. Neat.

Original source

Related problems