AptInt is THE fastest luau implementation of BigInteger, designed for working with numbers over 10,000 digits long.
This library provides arbitrary precision arithmetic capabilities implemented in just Luau. It uses a 2^24 word size to store numbers and can easily be configured to your liking.
This library provides:
- Functionality for all arithmetic operations (
:AddRaw():SubtractRaw():MultiplyRaw():DivideRaw():PowRaw():ModRaw():sqrt()) - Comparison functions (
:LowerThanRaw():EqualsRaw():LowerOrEqualToRaw()) - Metamethod support (
+ - * / ^ % < <= == >= =) - Limb-wise Left shift and right shift
- Conversion functions (
:ToString():ToNumber()) - Extra utilities such as (
:Abs():Min():Max())
The implementations are as performant as possible, so they have no safety checks, with the exception of the metamethods.
Usage Documentation can be found here.
Algorithm choice can be found here.
AptInt uses many asymptotically fast algorithms and dispatches between them based on input size. Alongside this, tables are recycled to their fullest potential, bringing the allocation count low. It also utilizes the full power of native code generation. This makes real use be very fast.
Note that these were done on an i7-10750H CPU. The benchmark script can be found in /bench/.
The results are updated every time the performance gets improved. They are also generally ~1.3x faster if running on the roblox server.
For division, we do a 2NxN division. (2N = digit count)
| Digit count | Addition | Subtraction | Multiplication | Division | Square root |
|---|---|---|---|---|---|
| 1 | 1μs | 1μs | 1μs | 2μs | 29μs |
| 50 | 1μs | 3μs | 4μs | 11μs | 31μs |
| 100 | 2μs | 7μs | 6μs | 15μs | 73μs |
| 500 | 5μs | 9μs | 56μs | 28μs | 250μs |
| 1,000 | 7μs | 13μs | 173μs | 58μs | 357μs |
| 5,000 | 23μs | 30μs | 1ms | 195μs | 796μs |
| 10,000 | 57μs | 74μs | 4ms | 1ms | 2ms |
| 50,000 | 209μs | 243μs | 46ms | 38ms | 35ms |
| 100,000 | 389μs | 396μs | 137ms | 114ms | 89ms |
