@eryx/number Module

This library provides four numeric domains backed by GMP and MPFR:

Use Integer when exact whole-number arithmetic matters, such as counters, combinatorics, bitwise work, cryptography, IDs, or number theory. Use Rational when you need exact fractions and want to avoid the rounding behavior of floating point. Use Float when you want explicit control over precision and floating-point rounding. Use Number when you want the most ergonomic interface and are happy with Python-like promotion between integer and float.

Number is intentionally only a union of Integer and Float. It does not automatically enter the Rational domain, because exact fractions can grow in size quickly and that cost is easy to miss if promotion happens implicitly.

There are a few important behavior rules to keep in mind:

Common patterns:

local number = require("@eryx/number")
local integer = number.integer
local float = number.float
local dynamic = number.number

local huge = integer.fromString("123456789012345678901234567890")
local exact = huge * 9

local ratio = number.rational.new(1, 3)
local approx = ratio:toFloat(128)

local mixed = dynamic.fromInteger(10) / 4
print(mixed:kind()) -- "float"
print(mixed) -- 2.5

local whole = mixed:floor()
print(whole:kind()) -- "integer"
print(whole) -- 2

If you already know which numeric domain you want, prefer the explicit module for clarity. Reach for number.number when you want convenience first and explicit domain control second.

Summary

Classes

Integer:modPow(exponent: Integer | number, modulus: Integer | number)Integer
Integer:toFloat(precision: number?)Float
Rational:toFloat(precision: number?, mode: RoundingMode?)Float
Float:toString(base: number?, digits: number?)string
Number:kind()"integer" | "float"
Number:toString(base: number?, digits: number?)string
Number:toFloat(precision: number?, mode: RoundingMode?)Float

Functions

number.integer.isInteger(value: unknown)boolean
number.rational.new(numerator: RationalLike?, denominator: RationalLike?)Rational
number.float.new(value: FloatLike?, precision: number?)Float
number.float.fromString(value: string, base: number?, precision: number?)Float
number.float.fromNumber(value: number, precision: number?)Float
number.float.zero(precision: number?)Float
number.float.one(precision: number?)Float
number.float.isFloat(value: unknown)boolean
number.number.new(value: NumberLike?, precision: number?)Number
number.number.fromString(value: string, base: number?, precision: number?)Number
number.number.fromNumber(value: number, precision: number?)Number
number.number.fromFloat(value: Float | number, precision: number?)Number
number.number.isNumber(value: unknown)boolean

API Reference

Classes

Integer

Properties

Integer:clone

Returns a new Integer with the same value. This is useful when you want an explicit copy before passing the value around or transforming it.

Integer:clone()Integer

Returns

A new Integer equal to self.

Integer:abs

Returns the absolute value of the integer. Negative inputs become positive; zero and positive values are unchanged.

Integer:abs()Integer

Returns

A non-negative Integer with the same magnitude as self.

Integer:sign

Returns the sign of the integer. The result is -1 for negative values, 0 for zero, and 1 for positive values.

Integer:sign()Sign

Returns

The sign of self.

Integer:cmp

Compares this integer with another integer-like value. This is the explicit comparison method behind the ordering operators.

Integer:cmp(other: Integer | number)number

Parameters

other: Integer | number

The value to compare against.

Returns

A negative number if self < other, zero if self == other, or a positive number if self > other.

Integer:isZero

Checks whether the integer is exactly zero. This is a convenience predicate for branchy arithmetic code.

Integer:isZero()boolean

Returns

true if self is zero, otherwise false.

Integer:isOne

Checks whether the integer is exactly one. This is often handy in algorithms with multiplicative identities.

Integer:isOne()boolean

Returns

true if self is one, otherwise false.

Integer:isEven

Checks whether the integer is even. Evenness is determined by the low bit, so this works efficiently even for very large values.

Integer:isEven()boolean

Returns

true if self is evenly divisible by two, otherwise false.

Integer:isOdd

Checks whether the integer is odd. This is the complement of isEven().

Integer:isOdd()boolean

Returns

true if self is not evenly divisible by two, otherwise false.

Integer:divTrunc

Divides two integers using truncating division. The quotient is rounded toward zero.

Integer:divTrunc(other: Integer | number)Integer

Parameters

other: Integer | number

The divisor.

Returns

The truncated quotient.

Integer:divFloor

Divides two integers using floor division. The quotient is rounded toward negative infinity.

Integer:divFloor(other: Integer | number)Integer

Parameters

other: Integer | number

The divisor.

Returns

The floored quotient.

Integer:divCeil

Divides two integers using ceiling division. The quotient is rounded toward positive infinity.

Integer:divCeil(other: Integer | number)Integer

Parameters

other: Integer | number

The divisor.

Returns

The ceiled quotient.

Integer:divmod

Divides two integers and returns both the quotient and remainder. The quotient rounding rule is controlled by mode, and the remainder follows that choice.

Integer:divmod(other: Integer | number, mode: IntegerDivMode?)(Integer, Integer)

Parameters

other: Integer | number

The divisor.

The rounding mode to use: "trunc", "floor", or "ceil".

Returns

The quotient under the chosen rounding rule.

The corresponding remainder.

Integer:gcd

Computes the greatest common divisor of two integers. The result is always non-negative.

Integer:gcd(other: Integer | number)Integer

Parameters

other: Integer | number

The other operand.

Returns

The greatest common divisor of self and other.

Integer:lcm

Computes the least common multiple of two integers. This is useful for normalising periods, denominators, or modular steps.

Integer:lcm(other: Integer | number)Integer

Parameters

other: Integer | number

The other operand.

Returns

The least common multiple of self and other.

Integer:extendedGcd

Computes the extended greatest common divisor. The returned coefficients satisfy g = self * x + other * y.

Integer:extendedGcd(other: Integer | number)(Integer, Integer, Integer)

Parameters

other: Integer | number

The other operand.

Returns

The greatest common divisor.

The coefficient multiplying self.

The coefficient multiplying other.

Integer:pow

Raises the integer to a non-negative integral power. Use this when you want exact integer exponentiation without leaving the integer domain.

Integer:pow(exponent: Integer | number)Integer

Parameters

exponent: Integer | number

The exponent to raise self by.

Returns

self raised to exponent.

Integer:modPow

Computes modular exponentiation. This evaluates self^exponent mod modulus efficiently, even for very large values.

Integer:modPow(exponent: Integer | number, modulus: Integer | number)Integer

Parameters

exponent: Integer | number

The non-negative exponent.

modulus: Integer | number

The non-zero modulus.

Returns

The modular power result.

Integer:modInverse

Computes the modular multiplicative inverse. If no inverse exists because the values are not coprime, this returns nil.

Integer:modInverse(modulus: Integer | number)Integer?

Parameters

modulus: Integer | number

The non-zero modulus.

Returns

The modular inverse of self modulo modulus, or nil if none exists.

Integer:isProbablePrime

Performs a probabilistic primality test. More repetitions increase confidence for composite detection.

Integer:isProbablePrime(reps: number?)boolean

Parameters

reps: number?

The number of primality test rounds to run.

Returns

true if the value is probably prime, otherwise false.

Integer:nextPrime

Finds the next probable prime greater than this integer. This is useful for search-based number theory and cryptographic helpers.

Integer:nextPrime()Integer

Returns

The next probable prime after self.

Integer:bitLength

Returns the number of significant bits required to represent the integer. Zero has a bit length of 0.

Integer:bitLength()number

Returns

The bit length of self.

Integer:popcount

Counts the number of set bits in the integer. This is also known as the population count or Hamming weight.

Integer:popcount()number

Returns

The number of 1 bits in self.

Integer:testBit

Checks whether a specific bit is set. Bit positions are zero-based, where bit 0 is the least significant bit.

Integer:testBit(bit: number)boolean

Parameters

bit: number

The zero-based bit index to inspect.

Returns

true if the bit is set, otherwise false.

Integer:setBit

Returns a copy of the integer with the given bit set to 1. Bit positions are zero-based.

Integer:setBit(bit: number)Integer

Parameters

bit: number

The zero-based bit index to set.

Returns

A new Integer with the selected bit set.

Integer:clearBit

Returns a copy of the integer with the given bit cleared to 0. Bit positions are zero-based.

Integer:clearBit(bit: number)Integer

Parameters

bit: number

The zero-based bit index to clear.

Returns

A new Integer with the selected bit cleared.

Integer:flipBit

Returns a copy of the integer with the given bit toggled. Bit positions are zero-based.

Integer:flipBit(bit: number)Integer

Parameters

bit: number

The zero-based bit index to flip.

Returns

A new Integer with the selected bit inverted.

Integer:band

Computes the bitwise AND of two integers. This uses two's-complement style infinite-width semantics, matching GMP's integer bit operations.

Integer:band(other: Integer | number)Integer

Parameters

other: Integer | number

The other operand.

Returns

The bitwise AND of self and other.

Integer:bor

Computes the bitwise OR of two integers. This uses two's-complement style infinite-width semantics.

Integer:bor(other: Integer | number)Integer

Parameters

other: Integer | number

The other operand.

Returns

The bitwise OR of self and other.

Integer:bxor

Computes the bitwise XOR of two integers. This uses two's-complement style infinite-width semantics.

Integer:bxor(other: Integer | number)Integer

Parameters

other: Integer | number

The other operand.

Returns

The bitwise XOR of self and other.

Integer:bnot

Computes the bitwise complement of the integer. This uses two's-complement style infinite-width semantics.

Integer:bnot()Integer

Returns

The bitwise complement of self.

Integer:shl

Shifts the integer left by the given number of bits. Negative shift counts reverse direction and behave like a right shift.

Integer:shl(bits: number)Integer

Parameters

bits: number

The number of bit positions to shift.

Returns

The shifted Integer.

Integer:shr

Shifts the integer right by the given number of bits. Negative shift counts reverse direction and behave like a left shift.

Integer:shr(bits: number)Integer

Parameters

bits: number

The number of bit positions to shift.

Returns

The shifted Integer.

Integer:toString

Formats the integer as a string in the chosen base. Base 10 is the default, and other bases are useful for binary, hexadecimal, and compact serialisation.

Integer:toString(base: number?)string

Parameters

base: number?

The radix to use when formatting the integer.

Returns

A string representation of self.

Integer:toNumber

Converts the integer to a Luau number. This conversion is intended for ergonomic interop with ordinary numeric code.

Integer:toNumber()number

Returns

The value of self as a Luau number.

Integer:toRational

Converts the integer into an exact Rational with denominator 1. This is useful when you want to move from integer arithmetic into rational arithmetic without losing precision.

Integer:toRational()Rational

Returns

A Rational equal to self / 1.

Integer:toFloat

Promotes the integer into the floating-point domain. This is useful when you want MPFR-backed operations starting from an exact integer value.

Integer:toFloat(precision: number?)Float

Parameters

precision: number?

The precision to use for the resulting Float, in bits.

Returns

A Float equal to self.

Integer:toI64

Converts the integer to a signed 64-bit integer. Use this when you explicitly need a fixed-width signed representation.

Integer:toI64()integer

Returns

The value of self as a signed 64-bit integer.

Integer:toU64

Converts the integer to an unsigned 64-bit integer. Use this when you explicitly need a fixed-width unsigned representation.

Integer:toU64()integer

Returns

The value of self as an unsigned 64-bit integer.

Integer:fitsNumber

Checks whether the integer can be represented as a Luau number. This is useful before crossing back into APIs that expect ordinary numbers.

Integer:fitsNumber()boolean

Returns

true if self fits within the supported Luau number representation, otherwise false.

Integer:fitsI64

Checks whether the integer fits in a signed 64-bit integer. This is a non-throwing guard for toI64().

Integer:fitsI64()boolean

Returns

true if self fits in signed 64-bit range, otherwise false.

Integer:fitsU64

Checks whether the integer fits in an unsigned 64-bit integer. This is a non-throwing guard for toU64().

Integer:fitsU64()boolean

Returns

true if self fits in unsigned 64-bit range, otherwise false.

Rational

Properties

Rational:clone

Returns a new Rational with the same value. This is useful when you want an explicit copy before passing the value into other code.

Rational:clone()Rational

Returns

A new Rational equal to self.

Rational:abs

Returns the absolute value of the rational. Negative inputs become positive; zero and positive values are unchanged.

Rational:abs()Rational

Returns

A non-negative Rational with the same magnitude as self.

Rational:sign

Returns the sign of the rational. The result is -1 for negative values, 0 for zero, and 1 for positive values.

Rational:sign()Sign

Returns

The sign of self.

Rational:cmp

Compares this rational with another rational-like value. This is the explicit comparison method behind the ordering operators.

Rational:cmp(other: Rational | number)number

Parameters

other: Rational | number

The value to compare against.

Returns

A negative number if self < other, zero if self == other, or a positive number if self > other.

Rational:inverse

Returns the reciprocal of the rational. This swaps the numerator and denominator, preserving the sign.

Rational:inverse()Rational

Returns

A new Rational equal to 1 / self.

Rational:canonical

Returns the rational in canonical reduced form. This normalises the sign and reduces the numerator and denominator by their greatest common divisor.

Rational:canonical()Rational

Returns

A canonical Rational equivalent to self.

Rational:numerator

Returns the numerator of the rational. The returned Integer reflects the current exact value of self.

Rational:numerator()Integer

Returns

The numerator of self.

Rational:denominator

Returns the denominator of the rational. In canonical form, this is always positive.

Rational:denominator()Integer

Returns

The denominator of self.

Rational:isZero

Checks whether the rational is exactly zero. This is a convenience predicate for branchy arithmetic code.

Rational:isZero()boolean

Returns

true if self is zero, otherwise false.

Rational:isOne

Checks whether the rational is exactly one. This is often useful in code that works with multiplicative identities.

Rational:isOne()boolean

Returns

true if self is one, otherwise false.

Rational:isInteger

Checks whether the rational is a whole integer value. This is true when the denominator is 1 after canonicalisation.

Rational:isInteger()boolean

Returns

true if self represents an integer, otherwise false.

Rational:floor

Rounds the rational down to the nearest integer. This uses floor semantics, rounding toward negative infinity.

Rational:floor()Integer

Returns

The floored Integer value.

Rational:ceil

Rounds the rational up to the nearest integer. This uses ceiling semantics, rounding toward positive infinity.

Rational:ceil()Integer

Returns

The ceiled Integer value.

Rational:trunc

Truncates the rational toward zero. This is useful when you want integer projection without floor/ceiling behaviour.

Rational:trunc()Integer

Returns

The truncated Integer value.

Rational:round

Rounds the rational to the nearest integer using the chosen mode. This supports explicit tie-breaking strategies such as nearest-even.

Rational:round(mode: RoundingMode?)Integer

Parameters

The rounding mode to use.

Returns

The rounded Integer value.

Rational:toString

Formats the rational as a string in the chosen base. Whole-valued rationals may render without a slash; fractional values render as numerator/denominator.

Rational:toString(base: number?)string

Parameters

base: number?

The radix to use when formatting the rational.

Returns

A string representation of self.

Rational:toDecimal

Formats the rational as a decimal string. Exact terminating decimals can be emitted directly; otherwise this rounds to the requested number of digits.

Rational:toDecimal(digits: number?, mode: RoundingMode?)string

Parameters

digits: number?

The number of digits to keep after the decimal point.

The rounding mode to use when rounding is needed.

Returns

A decimal string representation of self.

Rational:toNumber

Converts the rational to a Luau number. This is intended for ergonomic interop with ordinary numeric code when exact rational arithmetic is no longer required.

Rational:toNumber()number

Returns

The value of self as a Luau number.

Rational:toInteger

Converts the rational to an Integer using the chosen rounding mode. This is useful when you want an explicit and exact integer projection step.

Rational:toInteger(mode: RoundingMode?)Integer

Parameters

The rounding mode to use for the conversion.

Returns

The resulting Integer value.

Rational:toFloat

Converts the rational to a Float using the chosen precision and rounding mode. This is useful when you want to leave the exact rational domain with explicit floating-point control.

Rational:toFloat(precision: number?, mode: RoundingMode?)Float

Parameters

precision: number?

The precision to use for the resulting Float, in bits.

The rounding mode to use when conversion is inexact.

Returns

The resulting Float approximation of self.

Float

Properties

Float:clone

Returns a new Float with the same numeric value. This is useful when you want an explicit copy, optionally at the same precision as the original.

Float:clone()Float

Returns

A new Float equal to self.

Float:abs

Returns the absolute value of the float. Negative inputs become positive; zero and positive values are unchanged.

Float:abs()Float

Returns

A non-negative Float with the same magnitude as self.

Float:sign

Returns the sign of the float. The result is -1 for negative values, 0 for zero, and 1 for positive values. @param self The Float whose sign should be inspected.

Float:sign()Sign

Returns

The sign of self.

Float:cmp

Compares this float with another float-like value. This is the explicit comparison method behind the ordering operators.

Float:cmp(other: Float | number)number

Parameters

other: Float | number

The value to compare against.

Returns

A negative number if self < other, zero if self == other, or a positive number if self > other.

Float:sqrt

Computes the square root of the float. This stays in the floating-point domain and uses MPFR's rounding behaviour.

Float:sqrt()Float

Returns

The square root of self.

Float:floor

Rounds the float down to the nearest integer. This uses floor semantics, rounding toward negative infinity.

Float:floor()Integer

Returns

The floored Integer value.

Float:ceil

Rounds the float up to the nearest integer. This uses ceiling semantics, rounding toward positive infinity.

Float:ceil()Integer

Returns

The ceiled Integer value.

Float:trunc

Truncates the float toward zero. This is useful when you want integer projection without floor/ceiling behaviour.

Float:trunc()Integer

Returns

The truncated Integer value.

Float:round

Rounds the float to an Integer using the chosen rounding mode. This supports explicit tie-breaking strategies such as nearest-even.

Float:round(mode: RoundingMode?)Integer

Parameters

The rounding mode to use.

Returns

The rounded Integer value.

Float:precision

Returns the precision of the float in bits. This is the stored MPFR precision for the current value.

Float:precision()number

Returns

The precision of self, in bits.

Float:withPrecision

Returns a copy of the float at a different precision. The value is rounded to the requested precision using the chosen rounding mode.

Float:withPrecision(bits: number, mode: RoundingMode?)Float

Parameters

bits: number

The destination precision in bits.

The rounding mode to use when precision changes.

Returns

A new Float with the requested precision.

Float:isZero

Checks whether the float is exactly zero. This is a convenience predicate for branchy arithmetic code.

Float:isZero()boolean

Returns

true if self is zero, otherwise false.

Float:isFinite

Checks whether the float is finite. This distinguishes ordinary numeric values from nan, inf, and -inf.

Float:isFinite()boolean

Returns

true if self is finite, otherwise false.

Float:isNaN

Checks whether the float is nan. This is useful when you need to distinguish invalid or undefined floating-point results.

Float:isNaN()boolean

Returns

true if self is nan, otherwise false.

Float:isInfinite

Checks whether the float is positive or negative infinity. This is useful when you need to handle overflow-style or unbounded results explicitly.

Float:isInfinite()boolean

Returns

true if self is infinite, otherwise false.

Float:toString

Formats the float as a string in the chosen base. When digits is omitted, the formatter emits a compact representation suitable for display.

Float:toString(base: number?, digits: number?)string

Parameters

base: number?

The radix to use when formatting the float.

digits: number?

The number of digits to emit for the significand.

Returns

A string representation of self.

Float:toNumber

Converts the float to a Luau number. This is intended for ergonomic interop with ordinary numeric code.

Float:toNumber()number

Returns

The value of self as a Luau number.

Float:toInteger

Converts the float to an Integer using the chosen rounding mode. This is useful when you want an explicit and exact integer projection step.

Float:toInteger(mode: RoundingMode?)Integer

Parameters

The rounding mode to use for the conversion.

Returns

The resulting Integer value.

Number

Properties

Number:clone

Returns a new Number with the same value and kind. This is useful when you want an explicit copy before passing it into other code.

Number:clone()Number

Returns

A new Number equal to self.

Number:kind

Reports whether the dynamic number is currently storing an integer or a float. This is useful when you need to branch on the exact runtime representation.

Number:kind()"integer" | "float"

Returns

"integer" | "float"

"integer" if self is backed by an Integer-like value, otherwise "float".

Number:isInteger

Checks whether the dynamic number is currently backed by an integer value. This is a convenience predicate for representation-sensitive code.

Number:isInteger()boolean

Returns

true if self is integer-kind, otherwise false.

Number:isFloat

Checks whether the dynamic number is currently backed by a float value. This is a convenience predicate for representation-sensitive code.

Number:isFloat()boolean

Returns

true if self is float-kind, otherwise false.

Number:integer

Returns the exact Integer view when this Number is integer-kind. If the value is float-kind, this returns nil instead of rounding implicitly.

Number:integer()Integer?

Returns

The stored Integer value, or nil if self is float-kind.

Number:float

Returns the exact Float view when this Number is float-kind. If the value is integer-kind, this returns nil instead of promoting implicitly.

Number:float()Float?

Returns

The stored Float value, or nil if self is integer-kind.

Number:abs

Returns the absolute value of the dynamic number. The result stays in the same numeric domain as the underlying representation.

Number:abs()Number

Returns

A non-negative Number with the same magnitude as self.

Number:sign

Returns the sign of the dynamic number. The result is -1 for negative values, 0 for zero, and 1 for positive values.

Number:sign()Sign

Returns

The sign of self.

Number:cmp

Compares this dynamic number with another dynamic or plain numeric value. This is the explicit comparison method behind the ordering operators.

Number:cmp(other: Number | number)number

Parameters

other: Number | number

The value to compare against.

Returns

A negative number if self < other, zero if self == other, or a positive number if self > other.

Number:floor

Rounds the dynamic number down to the nearest integer-valued Number. This uses floor semantics and always returns integer-kind Number storage.

Number:floor()Number

Returns

The floored Number value.

Number:ceil

Rounds the dynamic number up to the nearest integer-valued Number. This uses ceiling semantics and always returns integer-kind Number storage.

Number:ceil()Number

Returns

The ceiled Number value.

Number:trunc

Truncates the dynamic number toward zero. This always returns integer-kind Number storage.

Number:trunc()Number

Returns

The truncated Number value.

Number:round

Rounds the dynamic number to the nearest integer-valued Number using the chosen mode. This supports explicit tie-breaking strategies such as nearest-even.

Number:round(mode: RoundingMode?)Number

Parameters

The rounding mode to use.

Returns

The rounded Number value, stored as integer-kind.

Number:toString

Formats the dynamic number as a string in the chosen base. Integer-kind values use integer formatting, while float-kind values use MPFR formatting rules.

Number:toString(base: number?, digits: number?)string

Parameters

base: number?

The radix to use when formatting the value.

digits: number?

The number of digits to emit for float-kind values.

Returns

A string representation of self.

Number:toNumber

Converts the dynamic number to a Luau number. This is intended for ergonomic interop with ordinary numeric code.

Number:toNumber()number

Returns

The value of self as a Luau number.

Number:toInteger

Converts the dynamic number to an Integer using the chosen rounding mode. This is useful when you want an explicit and exact integer projection step.

Number:toInteger(mode: RoundingMode?)Integer

Parameters

The rounding mode to use for the conversion.

Returns

The resulting Integer value.

Number:toFloat

Converts the dynamic number to a Float, optionally changing its precision. Integer-kind values are promoted into the floating-point domain, and float-kind values may be rounded to a new precision.

Number:toFloat(precision: number?, mode: RoundingMode?)Float

Parameters

precision: number?

The destination precision in bits.

The rounding mode to use when rounding is needed.

Returns

The resulting Float value.

integer

number.integer.new

Creates a new Integer from an existing integer-like value. Numbers are treated as whole-number inputs, and strings may be parsed in the given base.

number.integer.new(value: IntegerLike?, base: number?)Integer

Parameters

value: IntegerLike?

The source value to convert into an Integer.

base: number?

The radix to use when value is a string.

Returns

A new Integer instance.

number.integer.fromString

Parses an Integer from a string. This is the most explicit way to construct very large values without going through a Luau number first.

number.integer.fromString(value: string, base: number?)Integer

Parameters

value: string

The source string to parse.

base: number?

The radix to use when parsing the string.

Returns

A new Integer parsed from value.

number.integer.fromNumber

Creates an Integer from a Luau number. This is convenient for ordinary whole-number code paths.

number.integer.fromNumber(value: number)Integer

Parameters

value: number

The numeric value to convert.

Returns

A new Integer equal to value.

number.integer.fromI64

Creates an Integer from a signed 64-bit integer. Use this when you want to make the fixed-width signed boundary explicit.

number.integer.fromI64(value: integer)Integer

Parameters

value: integer

The signed 64-bit input.

Returns

A new Integer equal to value.

number.integer.fromU64

Creates an Integer from an unsigned 64-bit integer. Use this when you want to make the fixed-width unsigned boundary explicit.

number.integer.fromU64(value: integer)Integer

Parameters

value: integer

The unsigned 64-bit input.

Returns

A new Integer equal to value.

number.integer.zero

Returns the additive identity. This is equivalent to constructing an Integer with value 0.

number.integer.zero()Integer

Returns

The Integer zero.

number.integer.one

Returns the multiplicative identity. This is equivalent to constructing an Integer with value 1.

number.integer.one()Integer

Returns

The Integer one.

number.integer.factorial

Computes the factorial of a non-negative integer. This is exact and can produce very large results.

number.integer.factorial(n: Integer | number)Integer

Parameters

The non-negative input value.

Returns

n! as an Integer.

number.integer.binomial

Computes a binomial coefficient. This is the exact value of "n choose k".

number.integer.binomial(n: Integer | number, k: Integer | number)Integer

Parameters

The upper value in the binomial coefficient.

The selection count.

Returns

The binomial coefficient C(n, k) as an Integer.

number.integer.isInteger

Checks whether a value is an Integer userdata produced by this module. This is useful for validation and dynamic dispatch.

number.integer.isInteger(value: unknown)boolean

Parameters

value: unknown

The value to inspect.

Returns

true if value is an Integer, otherwise false.

rational

number.rational.new

Creates a new Rational from rational-like input. Passing both numerator and denominator constructs the exact quotient of those values.

number.rational.new(numerator: RationalLike?, denominator: RationalLike?)Rational

Parameters

numerator: RationalLike?

The source value or numerator component.

denominator: RationalLike?

The optional denominator component.

Returns

A new Rational instance.

number.rational.fromString

Parses a Rational from a string. This accepts integer and fractional forms, including slash-separated values.

number.rational.fromString(value: string, base: number?)Rational

Parameters

value: string

The source string to parse.

base: number?

The radix to use when parsing the string.

Returns

A new Rational parsed from value.

number.rational.fromDecimal

Parses a decimal string into an exact Rational. This is useful when you want to preserve the exact value of textual decimal data.

number.rational.fromDecimal(value: string)Rational

Parameters

value: string

The decimal string to parse.

Returns

A new Rational equal to the parsed decimal value.

number.rational.fromFloat

Creates a Rational from a Luau number. This is convenient for interop with ordinary numeric code, though it reflects the input number's floating-point value.

number.rational.fromFloat(value: number)Rational

Parameters

value: number

The numeric value to convert.

Returns

A new Rational equal to value.

number.rational.fromInteger

Promotes an Integer or whole-number value into the rational domain. The resulting Rational has denominator 1.

number.rational.fromInteger(value: Integer | number)Rational

Parameters

value: Integer | number

The integer-like value to promote.

Returns

A new Rational equal to value / 1.

number.rational.zero

Returns the additive identity in the rational domain. This is equivalent to constructing the Rational value 0/1.

number.rational.zero()Rational

Returns

The Rational zero.

number.rational.one

Returns the multiplicative identity in the rational domain. This is equivalent to constructing the Rational value 1/1.

number.rational.one()Rational

Returns

The Rational one.

number.rational.isRational

Checks whether a value is a Rational userdata produced by this module. This is useful for validation and dynamic dispatch.

number.rational.isRational(value: unknown)boolean

Parameters

value: unknown

The value to inspect.

Returns

true if value is a Rational, otherwise false.

float

number.float.new

Creates a new Float from float-like input. Passing an existing Float preserves its value, and you may optionally override the stored precision.

number.float.new(value: FloatLike?, precision: number?)Float

Parameters

value: FloatLike?

The source value to convert into a Float.

precision: number?

The precision to use for the new Float, in bits.

Returns

A new Float instance.

number.float.fromString

Parses a Float from a string. This is the most explicit way to construct high-precision floating-point values from textual data.

number.float.fromString(value: string, base: number?, precision: number?)Float

Parameters

value: string

The source string to parse.

base: number?

The radix to use when parsing the string.

precision: number?

The precision to use for the new Float, in bits.

Returns

A new Float parsed from value.

number.float.fromNumber

Creates a Float from a Luau number. This is convenient for interop with ordinary floating-point code.

number.float.fromNumber(value: number, precision: number?)Float

Parameters

value: number

The numeric value to convert.

precision: number?

The precision to use for the new Float, in bits.

Returns

A new Float equal to value.

number.float.fromInteger

Promotes an Integer or whole-number value into the floating-point domain. This is useful when you want MPFR-backed operations on an exact integral input.

number.float.fromInteger(value: Integer | number, precision: number?)Float

Parameters

value: Integer | number

The integer-like value to promote.

precision: number?

The precision to use for the new Float, in bits.

Returns

A new Float equal to value.

number.float.fromRational

Converts a Rational into a Float using the chosen rounding mode. This is useful when you want to leave the exact rational domain with explicit precision control.

number.float.fromRational(value: Rational, precision: number?, mode: RoundingMode?)Float

Parameters

value: Rational

The Rational value to convert.

precision: number?

The precision to use for the new Float, in bits.

The rounding mode to use when conversion is inexact.

Returns

A new Float approximation of value.

number.float.defaultPrecision

Returns the module-wide default MPFR precision. Dynamic Number promotion into floats also follows this shared precision setting.

number.float.defaultPrecision()number

Returns

The default precision in bits.

number.float.setDefaultPrecision

Sets the module-wide default MPFR precision. This affects newly created floats that do not provide an explicit precision, including dynamic Number promotions.

number.float.setDefaultPrecision(bits: number)()

Parameters

bits: number

The new default precision in bits.

number.float.zero

Returns the additive identity in the floating-point domain. This is equivalent to constructing the Float value 0 at the chosen precision.

number.float.zero(precision: number?)Float

Parameters

precision: number?

The precision to use for the new Float, in bits.

Returns

The Float zero.

number.float.one

Returns the multiplicative identity in the floating-point domain. This is equivalent to constructing the Float value 1 at the chosen precision.

number.float.one(precision: number?)Float

Parameters

precision: number?

The precision to use for the new Float, in bits.

Returns

The Float one.

number.float.isFloat

Checks whether a value is a Float userdata produced by this module. This is useful for validation and dynamic dispatch.

number.float.isFloat(value: unknown)boolean

Parameters

value: unknown

The value to inspect.

Returns

true if value is a Float, otherwise false.

number

number.number.new

Creates a new dynamic Number from number-like input. Whole values stay in the integer domain when possible, while fractional values use the floating-point domain.

number.number.new(value: NumberLike?, precision: number?)Number

Parameters

value: NumberLike?

The source value to convert into a Number.

precision: number?

The precision to use when a float-backed Number must be created.

Returns

A new Number instance.

number.number.fromString

Parses a dynamic Number from a string. Integer-looking strings stay in the integer domain, while fractional strings create float-backed values.

number.number.fromString(value: string, base: number?, precision: number?)Number

Parameters

value: string

The source string to parse.

base: number?

The radix to use when parsing the string.

precision: number?

The precision to use when a float-backed Number must be created.

Returns

A new Number parsed from value.

number.number.fromNumber

Creates a dynamic Number from a Luau number. Whole numeric inputs stay integer-kind when possible; fractional inputs become float-kind.

number.number.fromNumber(value: number, precision: number?)Number

Parameters

value: number

The numeric value to convert.

precision: number?

The precision to use when a float-backed Number must be created.

Returns

A new Number equal to value.

number.number.fromInteger

Promotes an Integer or whole-number value into the dynamic Number domain. The resulting value is stored as integer-kind Number.

number.number.fromInteger(value: Integer | number)Number

Parameters

value: Integer | number

The integer-like value to promote.

Returns

A new integer-kind Number equal to value.

number.number.fromFloat

Promotes a Float or plain numeric value into the dynamic Number domain. The resulting value is stored as float-kind Number.

number.number.fromFloat(value: Float | number, precision: number?)Number

Parameters

value: Float | number

The float-like value to promote.

precision: number?

The precision to use when a new float-backed Number must be created.

Returns

A new float-kind Number equal to value.

number.number.zero

Returns the additive identity in the dynamic Number domain. This is represented as integer-kind zero.

number.number.zero()Number

Returns

The Number zero.

number.number.one

Returns the multiplicative identity in the dynamic Number domain. This is represented as integer-kind one.

number.number.one()Number

Returns

The Number one.

number.number.isNumber

Checks whether a value is a Number userdata produced by this module. This is useful for validation and dynamic dispatch.

number.number.isNumber(value: unknown)boolean

Parameters

value: unknown

The value to inspect.

Returns

true if value is a Number, otherwise false.

Types

RoundingMode

type RoundingMode = "trunc" | "floor" | "ceil" | "nearest" | "nearest_even" | "nearest_away"

IntegerDivMode

type IntegerDivMode = "trunc" | "floor" | "ceil"

Sign

type Sign = number
Implements: number

IntegerLike

RationalLike

FloatLike

NumberLike