Skip to content

Commit debf103

Browse files
committed
Release v0.3.1
1 parent d649b95 commit debf103

File tree

5 files changed

+75
-71
lines changed

5 files changed

+75
-71
lines changed

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ computing with large integers.
55

66
Different from most arbitrary-precision integer libraries in pure Lua out there this one
77
uses an array of lua integers as underlying data-type in its implementation instead of
8-
using strings or tables of digits, so regarding that aspect this library should be more efficient.
8+
using strings or large tables, this make it efficient for working with fixed width integers
9+
and to make bitwise operations.
10+
11+
Bint stands for Big Integer.
912

1013
The library implementation was highly inspired by
1114
[tiny-bignum-c](https://github.com/kokke/tiny-bignum-c).
1215

13-
Bint stands for Big Integer.
14-
1516
## Design goals
1617

1718
The main design goal of this library is to be small, correct, self contained and use few
@@ -22,35 +23,36 @@ integer overflow warps around,
2223
signed integers are implemented using two-complement arithmetic rules,
2324
integer division operations rounds towards minus infinity,
2425
any mixed operations with float numbers promotes the value to a float,
25-
and the usual division/power operation always promote to floats.
26+
and the usual division/power operation always promotes to floats.
2627

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

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

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

3839
## Features
3940

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

5153
## Documentation
5254

53-
The full API reference and documentation can viewed in the
55+
The full API reference and documentation can be viewed in the
5456
[documentation website](https://edubart.github.io/lua-bint/).
5557

5658
## Install

bint.lua

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ SOFTWARE.
2626
Small portable arbitrary-precision integer arithmetic library in pure Lua for
2727
computing with large integers.
2828
29-
Different from most arbitrary precision integer libraries in pure Lua out there this one
29+
Different from most arbitrary-precision integer libraries in pure Lua out there this one
3030
uses an array of lua integers as underlying data-type in its implementation instead of
31-
using strings or large tables, so regarding that aspect this library should be more efficient.
31+
using strings or large tables, this make it efficient for working with fixed width integers
32+
and to make bitwise operations.
3233
3334
## Design goals
3435
@@ -40,17 +41,17 @@ integer overflow warps around,
4041
signed integers are implemented using two-complement arithmetic rules,
4142
integer division operations rounds towards minus infinity,
4243
any mixed operations with float numbers promotes the value to a float,
43-
and the usual division/power operation always promote to floats.
44+
and the usual division/power operation always promotes to floats.
4445
4546
The library is designed to be possible to work with only unsigned integer arithmetic
4647
when using the proper methods.
4748
4849
All the lua arithmetic operators (+, -, *, //, /, %) and bitwise operators (&, |, ~, <<, >>)
4950
are implemented as metamethods.
5051
51-
The integer size must be fixed in advance and the library is designed to be efficient only when
52+
The integer size must be fixed in advance and the library is designed to be more efficient when
5253
working with integers of sizes between 64-4096 bits. If you need to work with really huge numbers
53-
without size restrictions then use other library. This choice has been made to have more efficiency
54+
without size restrictions then use another library. This choice has been made to have more efficiency
5455
in that specific size range.
5556
5657
## Usage
@@ -90,7 +91,7 @@ get the output from one of the following functions:
9091
* @{bint.tobase} (convert to a string in any base)
9192
* @{bint.__tostring} (convert to a string in base 10)
9293
93-
To output very large integer with no loss you probably want to use @{bint.tobase}
94+
To output a very large integer with no loss you probably want to use @{bint.tobase}
9495
or call `tostring` to get a string representation.
9596
9697
## Precautions
@@ -102,14 +103,14 @@ however the user should take care in some situations:
102103
* Don't mix integers and float operations if you want to work with integers only.
103104
* Don't use the regular equal operator ('==') to compare values from this library,
104105
unless you know in advance that both values are of the same primitive type,
105-
otherwise it will always returns false, use @{bint.eq} to be safe.
106+
otherwise it will always return false, use @{bint.eq} to be safe.
106107
* Don't pass fractional numbers to functions that an integer is expected
108+
* Don't mix operations between bint classes with different sizes as this is not supported.
107109
as this will throw assertions.
108110
* Remember that casting back to lua integers or numbers precision can be lost.
109111
* For dividing while preserving integers use the @{bint.__idiv} (the '//' operator).
110112
* For doing power operation preserving integers use the @{bint.ipow} function.
111-
to the proper size you intend to work with, otherwise large integers may wraps around.
112-
* Never mix operations between bint classes of different bit sizes
113+
* Configure the proper integer size you intend to work with, otherwise large integers may wrap around.
113114
114115
]]
115116

@@ -322,7 +323,7 @@ end
322323
--- Convert a bint to an unsigned integer.
323324
-- Note that large unsigned integers may be represented as negatives in lua integers.
324325
-- Note that lua cannot represent values larger than 64 bits,
325-
-- in that case integer values wraps around.
326+
-- in that case integer values wrap around.
326327
-- @param x A bint or a number to be converted into an unsigned integer.
327328
-- @return An integer or nil in case the input cannot be represented by an integer.
328329
-- @see bint.tointeger
@@ -341,7 +342,7 @@ end
341342
--- Convert a bint to a signed integer.
342343
-- It works by taking absolute values then applying the sign bit in case needed.
343344
-- Note that lua cannot represent values larger than 64 bits,
344-
-- in that case integer values wraps around.
345+
-- in that case integer values wrap around.
345346
-- @param x A bint or value to be converted into an unsigned integer.
346347
-- @return An integer or nil in case the input cannot be represented by an integer.
347348
-- @see bint.touinteger
@@ -369,7 +370,7 @@ local function bint_assert_tointeger(x)
369370
end
370371

371372
--- Convert a bint to a lua float in case integer would wrap around or lua integer otherwise.
372-
-- Different from @{bint.tointeger} the operation does not wraps around integers,
373+
-- Different from @{bint.tointeger} the operation does not wrap around integers,
373374
-- but digits precision are lost in the process of converting to a float.
374375
-- @param x A bint or value to be converted into a lua number.
375376
-- @return A lua number or nil in case the input cannot be represented by a number.
@@ -398,7 +399,7 @@ end
398399
-- @param x The bint to be converted from.
399400
-- @param[opt] base Base to be represented, defaults to 10.
400401
-- Must be at least 2 and at most 36.
401-
-- @param[opt] unsigned Whether to output as unsigned integer.
402+
-- @param[opt] unsigned Whether to output as an unsigned integer.
402403
-- Defaults to false for base 10 and true for others.
403404
-- When unsigned is false the symbol '-' is prepended in negative values.
404405
-- @return A string representing the input.
@@ -536,7 +537,7 @@ local function bint_assert_convert(x)
536537
end
537538

538539
--- Convert a value to a bint if possible otherwise to a lua number.
539-
-- Useful to prepare values that you are unsure if its going to be a integer or float.
540+
-- Useful to prepare values that you are unsure if it's going to be an integer or float.
540541
-- @param x A value to be converted (string, number or another bint).
541542
-- @param[opt] clone A boolean that tells if a new bint reference should be returned.
542543
-- Defaults to false.
@@ -836,8 +837,8 @@ function bint.abs(x)
836837
end
837838
end
838839

839-
--- Take floor of a number considering bints.
840-
-- @param x A bint or a lua number to perform the floor.
840+
--- Take the floor of a number considering bints.
841+
-- @param x A bint or a lua number to perform the floor operation.
841842
function bint.floor(x)
842843
if isbint(x) then
843844
return bint.new(x)
@@ -847,7 +848,7 @@ function bint.floor(x)
847848
end
848849

849850
--- Take ceil of a number considering bints.
850-
-- @param x A bint or a lua number to perform the ceil.
851+
-- @param x A bint or a lua number to perform the ceil operation.
851852
function bint.ceil(x)
852853
if isbint(x) then
853854
return bint.new(x)
@@ -994,7 +995,7 @@ function bint:_sub(y)
994995
end
995996

996997
--- Subtract two numbers considering bints.
997-
-- @param x A bint or a lua number to be subtract from.
998+
-- @param x A bint or a lua number to be subtracted from.
998999
-- @param y A bint or a lua number to subtract.
9991000
function bint.__sub(x, y)
10001001
local ix, iy = bint.tobint(x), bint.tobint(y)
@@ -1265,7 +1266,7 @@ function bint.__idiv(x, y)
12651266
end
12661267

12671268
--- Perform division between two numbers considering bints.
1268-
-- This always cast inputs to floats, for integer division only use @{bint.__idiv}.
1269+
-- This always casts inputs to floats, for integer division only use @{bint.__idiv}.
12691270
-- @param x The numerator, a bint or lua number.
12701271
-- @param y The denominator, a bint or lua number.
12711272
-- @return The quotient, a lua number.
@@ -1286,7 +1287,7 @@ function bint.__mod(x, y)
12861287
end
12871288

12881289
--- Perform integer power between two integers considering bints.
1289-
-- If y is negative then pow is performed as unsigned integers.
1290+
-- If y is negative then pow is performed as an unsigned integer.
12901291
-- @param x The base, an integer.
12911292
-- @param y The exponent, an integer.
12921293
-- @return The result of the pow operation, a bint.
@@ -1344,7 +1345,7 @@ function bint.upowmod(x, y, m)
13441345
end
13451346

13461347
--- Perform numeric power between two numbers considering bints.
1347-
-- This always cast inputs to floats, for integer power only use @{bint.ipow}.
1348+
-- This always casts inputs to floats, for integer power only use @{bint.ipow}.
13481349
-- @param x The base, a bint or lua number.
13491350
-- @param y The exponent, a bint or lua number.
13501351
-- @return The result of the pow operation, a lua number.
@@ -1479,12 +1480,12 @@ function bint.__bnot(x)
14791480
return y
14801481
end
14811482

1482-
--- Negate a bint (in-place). This apply effectively apply two's complements.
1483+
--- Negate a bint (in-place). This effectively applies two's complements.
14831484
function bint:_unm()
14841485
return self:_bnot():_inc()
14851486
end
14861487

1487-
--- Negate a bint. This apply effectively apply two's complements.
1488+
--- Negate a bint. This effectively applies two's complements.
14881489
-- @param x A bint to perform negation.
14891490
function bint.__unm(x)
14901491
return (~x):_inc()

0 commit comments

Comments
 (0)