r/lua • u/Foreign_Bar3477 • Nov 20 '25
Looking for a better pure Lua library for handling large numbers
Hey everyone, I’m looking for a pure Lua library to handle really large numbers, something easier to use.
The old library I was using (int) sometimes loses precision when dividing large numbers, so I need something more reliable.
Anyone has recommendations or alternatives?
10
Upvotes
4
u/TomatoCo Nov 20 '25
It might be worth porting one of the JavaScript big number libraries. Like the ones mentioned here https://www.reddit.com/r/AntimatterDimensions/s/R8Op1tlGb0
2
u/minforth Nov 22 '25
What is PURE Lua when Lua is designed to interact with C? You didn't tell for what purpose you need higher precision, so one the straightforward answer would be to use a C library like LibTomMath:
https://www.libtom.net/LibTomMath/
Or use a pipe and call bc
https://www.gnu.org/software/bc/
1
u/ewmailing Nov 20 '25
I don't have any personal experience with any of the pure Lua bignum libraries out there, but looking at the one you linked to, it supports pre-Lua 5.3, which implies to me that it is going through heroics to do integers using Lua floating types. And I speculate this is also why you are unhappy with its behavior sometimes.
My suggestion is that you look for a Lua 5.3+ bignum implementation which takes advantage of native Lua integer types introduced in 5.3. (This was in fact one of the use cases to justify adding native integers to Lua in the first place.)
1
u/EvilBadMadRetarded Nov 21 '25 edited Nov 21 '25
You need not big integer, but rational, for accurate division. But to express rational accurately, you need big integer :D
from google
1
u/Lilyflier Jul 09 '26
Did you happen find or create a good library for this? I'm a noob programmer hoping to make a Love2D project that could really use this.
1
u/collectgarbage Nov 21 '25
Google: lua bignum library
0
u/[deleted] Nov 20 '25
[deleted] — view deleted comment
1
u/[deleted] Nov 23 '25
[removed] — view removed comment