Websites Navigation: Airbit | Shop | m-shell.net
Languages: EN | DE

Module bigint: Arbitrarily Large Integers

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:

  • bigint.new creates a new big integer from a number, a string (in a given base, e.g. hexadecimal), or another big integer.
  • bigint.num converts a big integer to a number (potentially loosing significant digits).
  • bigint.str converts a big integer to a string encoded in a given base.
The big integer arguments of all functions can also be specified as a number or as a string encoding a decimal number:

a=bigint.mul("33333333333333333333333333333333333", -2);
print a, bigint.str(a)
→ bigint@414ffc -66666666666666666666666666666666666

bigint.abs

• function abs(p) → Native Object

Computes the absolute value of p as a big integer.

r=bigint.abs("-314159265358979323846264");
print bigint.str(r)
→ 314159265358979323846264

bigint.add

• function add(p, q) → Native Object

Computes the sum of p and q as a big integer.

r=bigint.add("123456789012345678901234567890",
             8765432110);
print bigint.str(r)
→ 123456789012345678910000000000

bigint.cmp

• function cmp(p, q) → Number

Compares p and q:

  • Returns -1 if p < q.
  • Returns 0 if p = q.
  • Returns 1 if p > q.

p=bigint.new("100000000", 16);
q=bigint.new(4294967296);
print bigint.cmp(p, q)
→ 0

bigint.div

• function div(p, q) → Native Object

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

bigint.mod

• function mod(p, q) → Native Object

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

bigint.mul

• function mul(p, q) → Native Object

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

bigint.neg

• function neg(p) → Native Object

Computes the value of p with sign changed.

r=bigint.neg("314159265358979323846264")
print bigint.str(r)
→ -314159265358979323846264

bigint.new

• function new(p) → Native Object
• function new(string, base=10) → Native Object

Creates a new big integer with the value of p. p can be:

  • Another big integer. In this case a copy of p is returned.
  • A number. Digits after the decimal points are ignored, and for values outside the range -263 to +263-1, the result is undefined.
  • A string encoding an integer in the given base. Valid bases are in the range 2 (binary) and 36 (using letters A to Z and a to z for digits 11 to 36).

    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

bigint.num

• function num(p) → Number

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

bigint.pow

• function pow(p, q) → Native Object
• function pow(p, q, m) → Native Object

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

bigint.str

• function str(p, base=10) → String

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

bigint.sub

• function sub(p, q) → Native Object

Computes the difference of p and q as a big integer.

r=bigint.sub("123456789012345678901234567890",
             -8765432110);
print bigint.str(r)
→ 123456789012345678910000000000


© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-LIB-888
mShell Home  > Documentation  > Manuals