All Projects → edubart → Lua Bint

edubart / Lua Bint

Licence: mit
Arbitrary precision integer arithmetic library in pure Lua

Programming Languages

lua
6591 projects

Projects that are alternatives of or similar to Lua Bint

apint
Arbitrary precision integers library.
Stars: ✭ 23 (+15%)
Mutual labels:  arbitrary-precision
decimal
An object-oriented implementation of basic math operation with arbitrary precision, using BC Math if available.
Stars: ✭ 14 (-30%)
Mutual labels:  arbitrary-precision
Decimal
A high-performance, arbitrary-precision, floating-point decimal library.
Stars: ✭ 363 (+1715%)
Mutual labels:  arbitrary-precision
hebimath
arbitrary precision arithmetic library
Stars: ✭ 37 (+85%)
Mutual labels:  arbitrary-precision
intx
intx – extended precision integer library
Stars: ✭ 83 (+315%)
Mutual labels:  arbitrary-precision
wide-integer
Wide-Integer implements a generic C++ template for uint128_t, uint256_t, uint512_t, uint1024_t, etc.
Stars: ✭ 83 (+315%)
Mutual labels:  arbitrary-precision
Decimal.js Light
The light version of decimal.js, an arbitrary-precision Decimal type for JavaScript.
Stars: ✭ 237 (+1085%)
Mutual labels:  arbitrary-precision
Mpmath
Python library for arbitrary-precision floating-point arithmetic
Stars: ✭ 511 (+2455%)
Mutual labels:  arbitrary-precision
bcmath-extended
Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions.
Stars: ✭ 59 (+195%)
Mutual labels:  arbitrary-precision
Big.js
A small, fast JavaScript library for arbitrary-precision decimal arithmetic.
Stars: ✭ 3,730 (+18550%)
Mutual labels:  arbitrary-precision
decimal
An arbitrary-precision decimal floating-point arithmetic package for Go
Stars: ✭ 28 (+40%)
Mutual labels:  arbitrary-precision
cldr-engine
Internationalization and localization in Typescript with Unicode CLDR, batteries included
Stars: ✭ 34 (+70%)
Mutual labels:  arbitrary-precision
Angourimath
Open-source symbolic algebra library for C# and F#. One of the most powerful in .NET
Stars: ✭ 266 (+1230%)
Mutual labels:  arbitrary-precision
math
Complex special functions and common mathematical operations in JavaScript
Stars: ✭ 42 (+110%)
Mutual labels:  arbitrary-precision
Decimal.js
An arbitrary-precision Decimal type for JavaScript
Stars: ✭ 4,585 (+22825%)
Mutual labels:  arbitrary-precision
Measurements.jl
Error propagation calculator and library for physical measurements. It supports real and complex numbers with uncertainty, arbitrary precision calculations, operations with arrays, and numerical integration.
Stars: ✭ 248 (+1140%)
Mutual labels:  arbitrary-precision
imath
Arbitrary precision integer and rational arithmetic library
Stars: ✭ 92 (+360%)
Mutual labels:  arbitrary-precision
Rpn
Math functional language, inspired by Hewlett-Packard Reverse Polish Notation (RPL) language
Stars: ✭ 17 (-15%)
Mutual labels:  arbitrary-precision
Bignumber.js
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
Stars: ✭ 5,174 (+25770%)
Mutual labels:  arbitrary-precision
Arb
C library for arbitrary-precision ball arithmetic
Stars: ✭ 280 (+1300%)
Mutual labels:  arbitrary-precision

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.

Bint stands for Big Integer.

The library implementation was highly inspired by tiny-bignum-c.

This library was created to be used in the Nelua programming language compiler. It is successfully used there to handle compile time operations on signed and unsigned integers.

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.

Features

  • Small, simple and self contained.
  • Efficient (for a pure Lua integer library).
  • Works with fixed width integers.
  • Follows Lua 5.3+ integer arithmetic semantics by default.
  • All integer overflows wraps around.
  • Can work with large integer widths with reasonable speed (such as 1024bit integers).
  • Implements all lua arithmetic metamethods.
  • Provide methods to work with unsigned arithmetic only.
  • Supports signed integers by default using two-complement arithmetic rules on unsigned operations.
  • Allow to mix any operation with lua numbers, promoting to lua floats where needed.
  • Can perform bitwise operations.

Documentation

The full API reference and documentation can be viewed in the documentation website.

Install

You can use luarocks to install quickly:

luarocks install bint

Or just copy the bint.lua file, the library is self contained in this single file with no dependencies.

Examples

local bint = require 'bint'(256) -- use 256 bits integers
local x = bint(1)
x = x << 128
print(x) -- outputs: 340282366920938463463374607431768211456
assert(tostring(x) == '340282366920938463463374607431768211456')

For more usage examples check the examples directory.

Some interesting examples there:

  • factorial.lua - calculate factorial of 100
  • fibonacci.lua - calculate the 1001th number of the Fibonacci sequence
  • pi.lua - calculate the first 100 digits of Pi
  • e.lua - calculate the first 100 digits of Euler's number
  • rsa.lua - simple RSA example for encrypting/decrypting messages

Tests

To check if everything is working as expected under your machine run lua tests.lua or make test.

Limitations

It is intended only to be used in Lua 5.3+. The library can theoretically be backported to Lua 5.1/LuaJIT but there is no plan at the moment. The integer size is limited in advance, this is a design choice.

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].