This module supports calculations with big integers. The maximum (or minimum) value for a big integer is limited only by available memory. All calculations are performed with full precision.
Big integers are native objects. Three functions convert between big integers and other representations:
|
a=bigint.mul("33333333333333333333333333333333333", -2); print a, bigint.str(a) → bigint@414ffc -66666666666666666666666666666666666
|
Computes the absolute value of p as a big integer.
|
r=bigint.abs("-314159265358979323846264"); print bigint.str(r) → 314159265358979323846264
|
Computes the sum of p and q as a big integer.
|
r=bigint.add("123456789012345678901234567890", 8765432110); print bigint.str(r) → 123456789012345678910000000000
|
Compares p and q:
|
p=bigint.new("100000000", 16); q=bigint.new(4294967296); print bigint.cmp(p, q) → 0
|
Computes the quotient of p and q as a big integer. Throws ErrDivideByZero if q=0.
|
r=bigint.div("123456789012345678901234567890", 1234567890); print bigint.str(r) → 100000000010000000001
|
Computes the remainder of p and q as a big integer. Throws ErrDivideByZero if q=0.
|
r=bigint.mod("123456789012345678901234567893", 1234567890); print bigint.str(r) → 3
|
Computes the product of p and q as a big integer.
|
p=bigint.new(333333333333333); r=bigint.mul(p, p); print bigint.str(r) → 111111111111110888888888888889
|
Computes the value of p with sign changed.
|
r=bigint.neg("314159265358979323846264") print bigint.str(r) → -314159265358979323846264
|
Creates a new big integer with the value of p. p can be:
Leading and trailing blanks in the string are ignored.
Throws ErrArgument if the base is out of range or the string contains invalid characters.
|
m=bigint.new(-18513.7); print bigint.str(m) → -18513
m=bigint.new("ffffffffffffffff", 16);print bigint.str(m, 4) → 33333333333333333333333333333333
|
Converts the big integer p to a number. If p is outside the range -263 to +263-1, the result is undefined.
|
r=bigint.div("12345678901234567890", 1234567890); print bigint.num(r) / 2 → 5000000000.5
|
Efficiently computes pq as a big integer. With three arguments, computes the remainder of dividing pq by m.
Throws ErrArgument if q<0. Throws ErrDivideByZero if m=0.
|
// perform RSA encryption with a 256-bit key e=bigint.new("7715580902129052762255348495586732516285"+ "0754331340849769128881931930089847467"); m=bigint.new("1157337135319357914338302274338009877449"+ "56524669244552124759012865929681230709"); c=bigint.pow("3695195570339388218205223153428883192073"+ "329889262155589752278898769206369823", e, m); print bigint.str(c) → 2090963726256956961627254580276511758392932630933805
1745096332980705650678328 |
Converts the big integer p to a string in the given base. Valid bases are in the range 2 (binary) and 36 (using letters a to z for digits 11 to 36).
|
// convert a large decimal to a large hexadecimal number s=bigint.str("123456789012345678901234567890", 16); print s → 18ee90ff6c373e0ee4e3f0ad2
|
Computes the difference of p and q as a big integer.
|
r=bigint.sub("123456789012345678901234567890", -8765432110); print bigint.str(r) → 123456789012345678910000000000
|