Package Exports
- @rbxts/bint
- @rbxts/bint/src/init.luau
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rbxts/bint) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
bint
Arbitrary-precision signed integer library for Luau.
bint stores integers as little-endian base-2^24 limbs, so values can grow without fixed-width overflow. It supports idiomatic operators (+, -, *, //, %, ^, comparisons), plus a lower-level core API with mutating and non-mutating functions.
Initial release: 0.1.0.
Features
- Arbitrary-precision signed integers.
- Constructors from Lua numbers, strings (base 2-36), and raw limb arrays.
- Conversion to/from strings and byte strings (little/big endian).
- Floor and truncated division APIs.
- Integer exponentiation and integer square root.
- Multiple multiplication/division algorithms chosen by operand size.
- Luau + TypeScript declaration file support (
src/index.d.ts).
Usage
local mod = require(path.to.bint)
local bint = mod.bint
local core = mod.core
local a = bint("123456789012345678901234567890")
local b = bint.from_int(42)
local sum = a + b
local prod = a * b
local q, r = core.divmod(a, b) -- floor division
print(sum:tostring()) -- method form
print(bint.tostring(prod)) -- function form
print(q, r)bint(v) is an alias for bint.new(v).
For roblox-ts / TypeScript users, the standard non-mutating arithmetic/comparison operations are also available as instance methods (for example a:add(b), a:mul(b), a:divmod(b)), which avoids relying on Luau operator metamethod syntax in typed code. Mutating operations remain on core.
API summary
Constructors
bint.from_int(n: number): Bint(converts the current Luaunumbervalue)bint.from_string(s: string, base?: number): Bintbint.from_limbs(limbs: {number}, signum?: -1 | 0 | 1): Bintbint.zero(): Bintbint.one(): Bintbint.new(v: Bint | number | string): Bintbint(v)(callable module alias)
Conversion helpers
bint.tostring(a: Bint, base?: number): stringbint.tonumber(a: Bint): numberbint.tole(a: Bint, trim?: boolean): stringbint.tobe(a: Bint, trim?: boolean): stringbint.fromle(s: string): Bintbint.frombe(s: string): Bint
core operations
core provides low-level signed arithmetic on canonical Bint values. No input coercion or validation is performed — arguments must already be Bint instances. Reach for core when you need mutating operations (_mut variants) or want to avoid repeated allocation; use the bint class for everyday ergonomic use.
- Comparison:
cmp,eq,lt,le - Sign:
abs,abs_mut,neg,neg_mut - Arithmetic:
add,add_mut,sub,sub_mut,mul,mul_mut - Division (floor):
divmod,idiv,mod - Division (truncated):
tdivmod,tdiv,tmod - Other:
pow,sqrt,lshift,rshift,lshift_words,rshift_words
Operator behavior
Supported metamethods:
+,-,*,//,%,^, unary-==,<,<=tostring(x)andx:tostring()#xreturns decimal digit count of magnitude
/ is intentionally unsupported and throws an error. Use // for integer division.
Semantics and caveats
bint.from_int(n)converts the already-represented Luaunumberexactly; Luau numbers are IEEE-754 doubles, so integer literals above2^53may already be rounded. Usebint.from_string(...)for exact large integer literals.bint.from_string/bint.tostringsupport bases2..36.core.divmodand//use floor division semantics.core.tdivmoduses truncated (toward zero) semantics.core.rshift(a, n)uses arithmetic (sign-preserving) semantics, equivalent tofloor(a / 2^n).core.lshift_words/core.rshift_wordsare limb-count shifts (base-2^24words), not bit shifts.core.sqrt(a)returns0fora <= 0.bint.tole/bint.tobeserialize magnitude only (sign is not encoded).bint.tonumberis exact only up to magnitude2^53.
Running tests
lune run libs/specsChangelog format
This repository uses the Keep a Changelog structure.