Giter VIP home page Giter VIP logo

lua-bint's Introduction

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

lua-bint's People

Contributors

edubart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

lua-bint's Issues

Test of integer size in a loop is really necessary?

Hi.
Not really an issue, just a question.
On luainteger_bitsize, you run a loop with a step of 1.
From what I understand, in Lua, an Integer can be only 32 or 64 bits. Why not use a step of 32?
Or even:

local function luainteger_bitsize()
  if -1>>32 == 0 then
    return 32
  end
  return 64
end

Cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.