Module bint

lua-bint - v0.5.1 - 26/Jun/2023 Eduardo Bart - edub4rt@gmail.com https://github.com/edubart/lua-bint

Small portable arbitrary-precision integer arithmetic library in pure Lua for computing with large integers.

Different from most arbitrary-precision integer libraries in pure Lua out there this one uses an array of lua integers as underlying data-type in its implementation instead of using strings or large tables, this make it efficient for working with fixed width integers and to make bitwise operations.

Design goals

The main design goal of this library is to be small, correct, self contained and use few resources while retaining acceptable performance and feature completeness.

The library is designed to follow recent Lua integer semantics, this means that integer overflow warps around, signed integers are implemented using two-complement arithmetic rules, integer division operations rounds towards minus infinity, any mixed operations with float numbers promotes the value to a float, and the usual division/power operation always promotes to floats.

The library is designed to be possible to work with only unsigned integer arithmetic when using the proper methods.

All the lua arithmetic operators (+, -, *, //, /, %) and bitwise operators (&, |, ~, <<, >>) are implemented as metamethods.

The integer size must be fixed in advance and the library is designed to be more efficient when working with integers of sizes between 64-4096 bits. If you need to work with really huge numbers without size restrictions then use another library. This choice has been made to have more efficiency in that specific size range.

Usage

First on you should require the bint file including how many bits the bint module will work with, by calling the returned function from the require, for example:

local bint = require 'bint'(1024)

For more information about its arguments see newmodule. Then when you need create a bint, you can use one of the following functions:

You can also call bint as it is an alias to bint.new. In doubt use bint.new to create a new bint.

Then you can use all the usual lua numeric operations on it, all the arithmetic metamethods are implemented. When you are done computing and need to get the result, get the output from one of the following functions:

To output a very large integer with no loss you probably want to use bint.tobase or call tostring to get a string representation.

Precautions

All library functions can be mixed with lua numbers, this makes easy to mix operations between bints and lua numbers, however the user should take care in some situations:

  • Don't mix integers and float operations if you want to work with integers only.
  • Don't use the regular equal operator ('==') to compare values from this library, unless you know in advance that both values are of the same primitive type, otherwise it will always return false, use bint.eq to be safe.
  • Don't pass fractional numbers to functions that an integer is expected
  • Don't mix operations between bint classes with different sizes as this is not supported, this will throw assertions.
  • Remember that casting back to lua integers or numbers precision can be lost.
  • For dividing while preserving integers use the bint.__idiv (the '//' operator).
  • For doing power operation preserving integers use the bint.ipow function.
  • Configure the proper integer size you intend to work with, otherwise large integers may wrap around.

Functions

newmodule (bits[, wordbits]) Create a new bint module representing integers of the desired bit size.
zero () Create a new bint with 0 value.
one () Create a new bint with 1 value.
fromuinteger (x) Create a bint from an unsigned integer.
frominteger (x) Create a bint from a signed integer.
frombase (s[, base]) Create a bint from a string of the desired base.
fromstring (s) Create a new bint from a string.
fromle (buffer) Create a new bint from a buffer of little-endian bytes.
frombe (buffer) Create a new bint from a buffer of big-endian bytes.
new (x) Create a new bint from a value.
tobint (x[, clone]) Convert a value to a bint if possible.
parse (x[, clone]) Convert a value to a bint if possible otherwise to a lua number.
touinteger (x) Convert a bint to an unsigned integer.
tointeger (x) Convert a bint to a signed integer.
tonumber (x) Convert a bint to a lua float in case integer would wrap around or lua integer otherwise.
tobase (x[, base[, unsigned]]) Convert a bint to a string in the desired base.
tole (x[, trim]) Convert a bint to a buffer of little-endian bytes.
tobe (x[, trim]) Convert a bint to a buffer of big-endian bytes.
iszero (x) Check if a number is 0 considering bints.
isone (x) Check if a number is 1 considering bints.
isminusone (x) Check if a number is -1 considering bints.
isbint (x) Check if the input is a bint.
isintegral (x) Check if the input is a lua integer or a bint.
isnumeric (x) Check if the input is a bint or a lua number.
type (x) Get the number type of the input (bint, integer or float).
isneg (x) Check if a number is negative considering bints.
ispos (x) Check if a number is positive considering bints.
iseven (x) Check if a number is even considering bints.
isodd (x) Check if a number is odd considering bints.
maxinteger () Create a new bint with the maximum possible integer value.
mininteger () Create a new bint with the minimum possible integer value.
_shlone () Bitwise left shift a bint in one bit (in-place).
_shrone () Bitwise right shift a bint in one bit (in-place).
_inc () Increment a bint by one (in-place).
inc (x) Increment a number by one considering bints.
_dec () Decrement a bint by one (in-place).
dec (x) Decrement a number by one considering bints.
_assign (y) Assign a bint to a new value (in-place).
_abs () Take absolute of a bint (in-place).
abs (x) Take absolute of a number considering bints.
floor (x) Take the floor of a number considering bints.
ceil (x) Take ceil of a number considering bints.
bwrap (x, y) Wrap around bits of an integer (discarding left bits) considering bints.
brol (x, y) Rotate left integer x by y bits considering bints.
bror (x, y) Rotate right integer x by y bits considering bints.
trunc (x) Truncate a number to a bint.
max (x, y) Take maximum between two numbers considering bints.
min (x, y) Take minimum between two numbers considering bints.
_add (y) Add an integer to a bint (in-place).
__add (x, y) Add two numbers considering bints.
_sub (y) Subtract an integer from a bint (in-place).
__sub (x, y) Subtract two numbers considering bints.
__mul (x, y) Multiply two numbers considering bints.
__eq (x, y) Check if bints are equal.
eq (x, y) Check if numbers are equal considering bints.
udivmod (x, y) Perform unsigned division and modulo operation between two integers considering bints.
udiv (x, y) Perform unsigned division between two integers considering bints.
umod (x, y) Perform unsigned integer modulo operation between two integers considering bints.
tdivmod (x, y) Perform integer truncate division and modulo operation between two numbers considering bints.
tdiv (x, y) Perform truncate division between two numbers considering bints.
tmod (x, y) Perform integer truncate modulo operation between two numbers considering bints.
idivmod (x, y) Perform integer floor division and modulo operation between two numbers considering bints.
__idiv (x, y) Perform floor division between two numbers considering bints.
__div (x, y) Perform division between two numbers considering bints.
__mod (x, y) Perform integer floor modulo operation between two numbers considering bints.
ipow (x, y) Perform integer power between two integers considering bints.
upowmod (x, y, m) Perform integer power between two unsigned integers over a modulus considering bints.
__pow (x, y) Perform numeric power between two numbers considering bints.
__shl (x, y) Bitwise left shift integers considering bints.
__shr (x, y) Bitwise right shift integers considering bints.
_band (y) Bitwise AND bints (in-place).
__band (x, y) Bitwise AND two integers considering bints.
_bor (y) Bitwise OR bints (in-place).
__bor (x, y) Bitwise OR two integers considering bints.
_bxor (y) Bitwise XOR bints (in-place).
__bxor (x, y) Bitwise XOR two integers considering bints.
_bnot () Bitwise NOT a bint (in-place).
__bnot (x) Bitwise NOT a bint.
_unm () Negate a bint (in-place).
__unm (x) Negate a bint.
ult (x, y) Compare if integer x is less than y considering bints (unsigned version).
ule (x, y) Compare if bint x is less or equal than y considering bints (unsigned version).
__lt (x, y) Compare if number x is less than y considering bints and signs.
__le (x, y) Compare if number x is less or equal than y considering bints and signs.
__tostring () Convert a bint to a string on base 10.

Fields

bits Number of bits representing a bint instance.


Functions

newmodule (bits[, wordbits])
Create a new bint module representing integers of the desired bit size. This is the returned function when require 'bint' is called.

Parameters:

  • bits Number of bits for the integer representation, must be multiple of wordbits and at least 64.
  • wordbits Number of the bits for the internal word, defaults to half of Lua's integer size. (optional)
zero ()
Create a new bint with 0 value.
one ()
Create a new bint with 1 value.
fromuinteger (x)
Create a bint from an unsigned integer. Treats signed integers as an unsigned integer.

Parameters:

  • x A value to initialize from convertible to a lua integer.

Returns:

    A new bint or nil in case the input cannot be represented by an integer.

See also:

frominteger (x)
Create a bint from a signed integer.

Parameters:

  • x A value to initialize from convertible to a lua integer.

Returns:

    A new bint or nil in case the input cannot be represented by an integer.

See also:

frombase (s[, base])
Create a bint from a string of the desired base.

Parameters:

  • s The string to be converted from, must have only alphanumeric and '+-' characters.
  • base Base that the number is represented, defaults to 10. Must be at least 2 and at most 36. (optional)

Returns:

    A new bint or nil in case the conversion failed.
fromstring (s)
Create a new bint from a string. The string can by a decimal number, binary number prefixed with '0b' or hexadecimal number prefixed with '0x'.

Parameters:

  • s A string convertible to a bint.

Returns:

    A new bint or nil in case the conversion failed.

See also:

fromle (buffer)
Create a new bint from a buffer of little-endian bytes.

Parameters:

  • buffer Buffer of bytes, extra bytes are trimmed from the right, missing bytes are padded to the right.

Returns:

    A bint.

Raises:

An assert is thrown in case buffer is not an string.
frombe (buffer)
Create a new bint from a buffer of big-endian bytes.

Parameters:

  • buffer Buffer of bytes, extra bytes are trimmed from the left, missing bytes are padded to the left.

Returns:

    A bint.

Raises:

An assert is thrown in case buffer is not an string.
new (x)
Create a new bint from a value.

Parameters:

  • x A value convertible to a bint (string, number or another bint).

Returns:

    A new bint, guaranteed to be a new reference in case needed.

Raises:

An assert is thrown in case x is not convertible to a bint.

See also:

tobint (x[, clone])
Convert a value to a bint if possible.

Parameters:

  • x A value to be converted (string, number or another bint).
  • clone A boolean that tells if a new bint reference should be returned. Defaults to false. (optional)

Returns:

    A bint or nil in case the conversion failed.

See also:

parse (x[, clone])
Convert a value to a bint if possible otherwise to a lua number. Useful to prepare values that you are unsure if it's going to be an integer or float.

Parameters:

  • x A value to be converted (string, number or another bint).
  • clone A boolean that tells if a new bint reference should be returned. Defaults to false. (optional)

Returns:

    A bint or a lua number or nil in case the conversion failed.

See also:

touinteger (x)
Convert a bint to an unsigned integer. Note that large unsigned integers may be represented as negatives in lua integers. Note that lua cannot represent values larger than 64 bits, in that case integer values wrap around.

Parameters:

  • x A bint or a number to be converted into an unsigned integer.

Returns:

    An integer or nil in case the input cannot be represented by an integer.

See also:

tointeger (x)
Convert a bint to a signed integer. It works by taking absolute values then applying the sign bit in case needed. Note that lua cannot represent values larger than 64 bits, in that case integer values wrap around.

Parameters:

  • x A bint or value to be converted into an unsigned integer.

Returns:

    An integer or nil in case the input cannot be represented by an integer.

See also:

tonumber (x)
Convert a bint to a lua float in case integer would wrap around or lua integer otherwise. Different from bint.tointeger the operation does not wrap around integers, but digits precision are lost in the process of converting to a float.

Parameters:

  • x A bint or value to be converted into a lua number.

Returns:

    A lua number or nil in case the input cannot be represented by a number.

See also:

tobase (x[, base[, unsigned]])
Convert a bint to a string in the desired base.

Parameters:

  • x The bint to be converted from.
  • base Base to be represented, defaults to 10. Must be at least 2 and at most 36. (optional)
  • unsigned Whether to output as an unsigned integer. Defaults to false for base 10 and true for others. When unsigned is false the symbol '-' is prepended in negative values. (optional)

Returns:

    A string representing the input.

Raises:

An assert is thrown in case the base is invalid.
tole (x[, trim])
Convert a bint to a buffer of little-endian bytes.

Parameters:

  • x A bint or lua integer.
  • trim If true, zero bytes on the right are trimmed. (optional)

Returns:

    A buffer of bytes representing the input.

Raises:

Asserts in case input is not convertible to an integer.
tobe (x[, trim])
Convert a bint to a buffer of big-endian bytes.

Parameters:

  • x A bint or lua integer.
  • trim If true, zero bytes on the left are trimmed. (optional)

Returns:

    A buffer of bytes representing the input.

Raises:

Asserts in case input is not convertible to an integer.
iszero (x)
Check if a number is 0 considering bints.

Parameters:

  • x A bint or a lua number.
isone (x)
Check if a number is 1 considering bints.

Parameters:

  • x A bint or a lua number.
isminusone (x)
Check if a number is -1 considering bints.

Parameters:

  • x A bint or a lua number.
isbint (x)
Check if the input is a bint.

Parameters:

  • x Any lua value.
isintegral (x)
Check if the input is a lua integer or a bint.

Parameters:

  • x Any lua value.
isnumeric (x)
Check if the input is a bint or a lua number.

Parameters:

  • x Any lua value.
type (x)
Get the number type of the input (bint, integer or float).

Parameters:

  • x Any lua value.

Returns:

    Returns "bint" for bints, "integer" for lua integers, "float" from lua floats or nil otherwise.
isneg (x)
Check if a number is negative considering bints. Zero is guaranteed to never be negative for bints.

Parameters:

  • x A bint or a lua number.
ispos (x)
Check if a number is positive considering bints.

Parameters:

  • x A bint or a lua number.
iseven (x)
Check if a number is even considering bints.

Parameters:

  • x A bint or a lua number.
isodd (x)
Check if a number is odd considering bints.

Parameters:

  • x A bint or a lua number.
maxinteger ()
Create a new bint with the maximum possible integer value.
mininteger ()
Create a new bint with the minimum possible integer value.
_shlone ()
Bitwise left shift a bint in one bit (in-place).
_shrone ()
Bitwise right shift a bint in one bit (in-place).
_inc ()
Increment a bint by one (in-place).
inc (x)
Increment a number by one considering bints.

Parameters:

  • x A bint or a lua number to increment.
_dec ()
Decrement a bint by one (in-place).
dec (x)
Decrement a number by one considering bints.

Parameters:

  • x A bint or a lua number to decrement.
_assign (y)
Assign a bint to a new value (in-place).

Parameters:

  • y A value to be copied from.

Raises:

Asserts in case inputs are not convertible to integers.
_abs ()
Take absolute of a bint (in-place).
abs (x)
Take absolute of a number considering bints.

Parameters:

  • x A bint or a lua number to take the absolute.
floor (x)
Take the floor of a number considering bints.

Parameters:

  • x A bint or a lua number to perform the floor operation.
ceil (x)
Take ceil of a number considering bints.

Parameters:

  • x A bint or a lua number to perform the ceil operation.
bwrap (x, y)
Wrap around bits of an integer (discarding left bits) considering bints.

Parameters:

  • x A bint or a lua integer.
  • y Number of right bits to preserve.
brol (x, y)
Rotate left integer x by y bits considering bints.

Parameters:

  • x A bint or a lua integer.
  • y Number of bits to rotate.
bror (x, y)
Rotate right integer x by y bits considering bints.

Parameters:

  • x A bint or a lua integer.
  • y Number of bits to rotate.
trunc (x)
Truncate a number to a bint. Floats numbers are truncated, that is, the fractional port is discarded.

Parameters:

  • x A number to truncate.

Returns:

    A new bint or nil in case the input does not fit in a bint or is not a number.
max (x, y)
Take maximum between two numbers considering bints.

Parameters:

  • x A bint or lua number to compare.
  • y A bint or lua number to compare.

Returns:

    A bint or a lua number. Guarantees to return a new bint for integer values.
min (x, y)
Take minimum between two numbers considering bints.

Parameters:

  • x A bint or lua number to compare.
  • y A bint or lua number to compare.

Returns:

    A bint or a lua number. Guarantees to return a new bint for integer values.
_add (y)
Add an integer to a bint (in-place).

Parameters:

  • y An integer to be added.

Raises:

Asserts in case inputs are not convertible to integers.
__add (x, y)
Add two numbers considering bints.

Parameters:

  • x A bint or a lua number to be added.
  • y A bint or a lua number to be added.
_sub (y)
Subtract an integer from a bint (in-place).

Parameters:

  • y An integer to subtract.

Raises:

Asserts in case inputs are not convertible to integers.
__sub (x, y)
Subtract two numbers considering bints.

Parameters:

  • x A bint or a lua number to be subtracted from.
  • y A bint or a lua number to subtract.
__mul (x, y)
Multiply two numbers considering bints.

Parameters:

  • x A bint or a lua number to multiply.
  • y A bint or a lua number to multiply.
__eq (x, y)
Check if bints are equal.

Parameters:

  • x A bint to compare.
  • y A bint to compare.
eq (x, y)
Check if numbers are equal considering bints.

Parameters:

  • x A bint or lua number to compare.
  • y A bint or lua number to compare.
udivmod (x, y)
Perform unsigned division and modulo operation between two integers considering bints. This is effectively the same of bint.udiv and bint.umod.

Parameters:

  • x The numerator, must be a bint or a lua integer.
  • y The denominator, must be a bint or a lua integer.

Returns:

    The quotient following the remainder, both bints.

Raises:

Asserts on attempt to divide by zero or if inputs are not convertible to integers.

See also:

udiv (x, y)
Perform unsigned division between two integers considering bints.

Parameters:

  • x The numerator, must be a bint or a lua integer.
  • y The denominator, must be a bint or a lua integer.

Returns:

    The quotient, a bint.

Raises:

Asserts on attempt to divide by zero or if inputs are not convertible to integers.
umod (x, y)
Perform unsigned integer modulo operation between two integers considering bints.

Parameters:

  • x The numerator, must be a bint or a lua integer.
  • y The denominator, must be a bint or a lua integer.

Returns:

    The remainder, a bint.

Raises:

Asserts on attempt to divide by zero or if the inputs are not convertible to integers.
tdivmod (x, y)
Perform integer truncate division and modulo operation between two numbers considering bints. This is effectively the same of bint.tdiv and bint.tmod.

Parameters:

  • x The numerator, a bint or lua number.
  • y The denominator, a bint or lua number.

Returns:

    The quotient following the remainder, both bint or lua number.

Raises:

Asserts on attempt to divide by zero or on division overflow.

See also:

tdiv (x, y)
Perform truncate division between two numbers considering bints. Truncate division is a division that rounds the quotient towards zero.

Parameters:

  • x The numerator, a bint or lua number.
  • y The denominator, a bint or lua number.

Returns:

    The quotient, a bint or lua number.

Raises:

Asserts on attempt to divide by zero or on division overflow.
tmod (x, y)
Perform integer truncate modulo operation between two numbers considering bints. The operation is defined as the remainder of the truncate division (division that rounds the quotient towards zero).

Parameters:

  • x The numerator, a bint or lua number.
  • y The denominator, a bint or lua number.

Returns:

    The remainder, a bint or lua number.

Raises:

Asserts on attempt to divide by zero or on division overflow.
idivmod (x, y)
Perform integer floor division and modulo operation between two numbers considering bints. This is effectively the same of bint.__idiv and bint.__mod.

Parameters:

  • x The numerator, a bint or lua number.
  • y The denominator, a bint or lua number.

Returns:

    The quotient following the remainder, both bint or lua number.

Raises:

Asserts on attempt to divide by zero.

See also:

__idiv (x, y)
Perform floor division between two numbers considering bints. Floor division is a division that rounds the quotient towards minus infinity, resulting in the floor of the division of its operands.

Parameters:

  • x The numerator, a bint or lua number.
  • y The denominator, a bint or lua number.

Returns:

    The quotient, a bint or lua number.

Raises:

Asserts on attempt to divide by zero.
__div (x, y)
Perform division between two numbers considering bints. This always casts inputs to floats, for integer division only use bint.__idiv.

Parameters:

  • x The numerator, a bint or lua number.
  • y The denominator, a bint or lua number.

Returns:

    The quotient, a lua number.
__mod (x, y)
Perform integer floor modulo operation between two numbers considering bints. The operation is defined as the remainder of the floor division (division that rounds the quotient towards minus infinity).

Parameters:

  • x The numerator, a bint or lua number.
  • y The denominator, a bint or lua number.

Returns:

    The remainder, a bint or lua number.

Raises:

Asserts on attempt to divide by zero.
ipow (x, y)
Perform integer power between two integers considering bints. If y is negative then pow is performed as an unsigned integer.

Parameters:

  • x The base, an integer.
  • y The exponent, an integer.

Returns:

    The result of the pow operation, a bint.

Raises:

Asserts in case inputs are not convertible to integers.

See also:

upowmod (x, y, m)
Perform integer power between two unsigned integers over a modulus considering bints.

Parameters:

  • x The base, an integer.
  • y The exponent, an integer.
  • m The modulus, an integer.

Returns:

    The result of the pow operation, a bint.

Raises:

Asserts in case inputs are not convertible to integers.

See also:

__pow (x, y)
Perform numeric power between two numbers considering bints. This always casts inputs to floats, for integer power only use bint.ipow.

Parameters:

  • x The base, a bint or lua number.
  • y The exponent, a bint or lua number.

Returns:

    The result of the pow operation, a lua number.

See also:

__shl (x, y)
Bitwise left shift integers considering bints.

Parameters:

  • x An integer to perform the bitwise shift.
  • y An integer with the number of bits to shift.

Returns:

    The result of shift operation, a bint.

Raises:

Asserts in case inputs are not convertible to integers.
__shr (x, y)
Bitwise right shift integers considering bints.

Parameters:

  • x An integer to perform the bitwise shift.
  • y An integer with the number of bits to shift.

Returns:

    The result of shift operation, a bint.

Raises:

Asserts in case inputs are not convertible to integers.
_band (y)
Bitwise AND bints (in-place).

Parameters:

  • y An integer to perform bitwise AND.

Raises:

Asserts in case inputs are not convertible to integers.
__band (x, y)
Bitwise AND two integers considering bints.

Parameters:

  • x An integer to perform bitwise AND.
  • y An integer to perform bitwise AND.

Raises:

Asserts in case inputs are not convertible to integers.
_bor (y)
Bitwise OR bints (in-place).

Parameters:

  • y An integer to perform bitwise OR.

Raises:

Asserts in case inputs are not convertible to integers.
__bor (x, y)
Bitwise OR two integers considering bints.

Parameters:

  • x An integer to perform bitwise OR.
  • y An integer to perform bitwise OR.

Raises:

Asserts in case inputs are not convertible to integers.
_bxor (y)
Bitwise XOR bints (in-place).

Parameters:

  • y An integer to perform bitwise XOR.

Raises:

Asserts in case inputs are not convertible to integers.
__bxor (x, y)
Bitwise XOR two integers considering bints.

Parameters:

  • x An integer to perform bitwise XOR.
  • y An integer to perform bitwise XOR.

Raises:

Asserts in case inputs are not convertible to integers.
_bnot ()
Bitwise NOT a bint (in-place).
__bnot (x)
Bitwise NOT a bint.

Parameters:

  • x An integer to perform bitwise NOT.

Raises:

Asserts in case inputs are not convertible to integers.
_unm ()
Negate a bint (in-place). This effectively applies two's complements.
__unm (x)
Negate a bint. This effectively applies two's complements.

Parameters:

  • x A bint to perform negation.
ult (x, y)
Compare if integer x is less than y considering bints (unsigned version).

Parameters:

  • x Left integer to compare.
  • y Right integer to compare.

Raises:

Asserts in case inputs are not convertible to integers.

See also:

ule (x, y)
Compare if bint x is less or equal than y considering bints (unsigned version).

Parameters:

  • x Left integer to compare.
  • y Right integer to compare.

Raises:

Asserts in case inputs are not convertible to integers.

See also:

__lt (x, y)
Compare if number x is less than y considering bints and signs.

Parameters:

  • x Left value to compare, a bint or lua number.
  • y Right value to compare, a bint or lua number.

See also:

__le (x, y)
Compare if number x is less or equal than y considering bints and signs.

Parameters:

  • x Left value to compare, a bint or lua number.
  • y Right value to compare, a bint or lua number.

See also:

__tostring ()
Convert a bint to a string on base 10.

See also:

Fields

bits
Number of bits representing a bint instance.
generated by LDoc 1.5.0 Last updated 2023-06-26 09:09:48