@@ -26,9 +26,10 @@ SOFTWARE.
26
26
Small portable arbitrary-precision integer arithmetic library in pure Lua for
27
27
computing with large integers.
28
28
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
30
30
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.
32
33
33
34
## Design goals
34
35
@@ -40,17 +41,17 @@ integer overflow warps around,
40
41
signed integers are implemented using two-complement arithmetic rules,
41
42
integer division operations rounds towards minus infinity,
42
43
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.
44
45
45
46
The library is designed to be possible to work with only unsigned integer arithmetic
46
47
when using the proper methods.
47
48
48
49
All the lua arithmetic operators (+, -, *, //, /, %) and bitwise operators (&, |, ~, <<, >>)
49
50
are implemented as metamethods.
50
51
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
52
53
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
54
55
in that specific size range.
55
56
56
57
## Usage
@@ -90,7 +91,7 @@ get the output from one of the following functions:
90
91
* @{bint.tobase} (convert to a string in any base)
91
92
* @{bint.__tostring} (convert to a string in base 10)
92
93
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}
94
95
or call `tostring` to get a string representation.
95
96
96
97
## Precautions
@@ -102,14 +103,14 @@ however the user should take care in some situations:
102
103
* Don't mix integers and float operations if you want to work with integers only.
103
104
* Don't use the regular equal operator ('==') to compare values from this library,
104
105
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.
106
107
* 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.
107
109
as this will throw assertions.
108
110
* Remember that casting back to lua integers or numbers precision can be lost.
109
111
* For dividing while preserving integers use the @{bint.__idiv} (the '//' operator).
110
112
* 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.
113
114
114
115
]]
115
116
322
323
--- Convert a bint to an unsigned integer.
323
324
-- Note that large unsigned integers may be represented as negatives in lua integers.
324
325
-- 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.
326
327
-- @param x A bint or a number to be converted into an unsigned integer.
327
328
-- @return An integer or nil in case the input cannot be represented by an integer.
328
329
-- @see bint.tointeger
341
342
--- Convert a bint to a signed integer.
342
343
-- It works by taking absolute values then applying the sign bit in case needed.
343
344
-- 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.
345
346
-- @param x A bint or value to be converted into an unsigned integer.
346
347
-- @return An integer or nil in case the input cannot be represented by an integer.
347
348
-- @see bint.touinteger
@@ -369,7 +370,7 @@ local function bint_assert_tointeger(x)
369
370
end
370
371
371
372
--- 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,
373
374
-- but digits precision are lost in the process of converting to a float.
374
375
-- @param x A bint or value to be converted into a lua number.
375
376
-- @return A lua number or nil in case the input cannot be represented by a number.
398
399
-- @param x The bint to be converted from.
399
400
-- @param [opt] base Base to be represented, defaults to 10.
400
401
-- 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.
402
403
-- Defaults to false for base 10 and true for others.
403
404
-- When unsigned is false the symbol '-' is prepended in negative values.
404
405
-- @return A string representing the input.
@@ -536,7 +537,7 @@ local function bint_assert_convert(x)
536
537
end
537
538
538
539
--- 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.
540
541
-- @param x A value to be converted (string, number or another bint).
541
542
-- @param [opt] clone A boolean that tells if a new bint reference should be returned.
542
543
-- Defaults to false.
@@ -836,8 +837,8 @@ function bint.abs(x)
836
837
end
837
838
end
838
839
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 .
841
842
function bint .floor (x )
842
843
if isbint (x ) then
843
844
return bint .new (x )
@@ -847,7 +848,7 @@ function bint.floor(x)
847
848
end
848
849
849
850
--- 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 .
851
852
function bint .ceil (x )
852
853
if isbint (x ) then
853
854
return bint .new (x )
@@ -994,7 +995,7 @@ function bint:_sub(y)
994
995
end
995
996
996
997
--- 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.
998
999
-- @param y A bint or a lua number to subtract.
999
1000
function bint .__sub (x , y )
1000
1001
local ix , iy = bint .tobint (x ), bint .tobint (y )
@@ -1265,7 +1266,7 @@ function bint.__idiv(x, y)
1265
1266
end
1266
1267
1267
1268
--- 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}.
1269
1270
-- @param x The numerator, a bint or lua number.
1270
1271
-- @param y The denominator, a bint or lua number.
1271
1272
-- @return The quotient, a lua number.
@@ -1286,7 +1287,7 @@ function bint.__mod(x, y)
1286
1287
end
1287
1288
1288
1289
--- 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 .
1290
1291
-- @param x The base, an integer.
1291
1292
-- @param y The exponent, an integer.
1292
1293
-- @return The result of the pow operation, a bint.
@@ -1344,7 +1345,7 @@ function bint.upowmod(x, y, m)
1344
1345
end
1345
1346
1346
1347
--- 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}.
1348
1349
-- @param x The base, a bint or lua number.
1349
1350
-- @param y The exponent, a bint or lua number.
1350
1351
-- @return The result of the pow operation, a lua number.
@@ -1479,12 +1480,12 @@ function bint.__bnot(x)
1479
1480
return y
1480
1481
end
1481
1482
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.
1483
1484
function bint :_unm ()
1484
1485
return self :_bnot ():_inc ()
1485
1486
end
1486
1487
1487
- --- Negate a bint. This apply effectively apply two's complements.
1488
+ --- Negate a bint. This effectively applies two's complements.
1488
1489
-- @param x A bint to perform negation.
1489
1490
function bint .__unm (x )
1490
1491
return (~x ):_inc ()
0 commit comments