The BigInt class is an Electric Imp agent or device side library aimed at managing arbitrarily large integers, including the addition and subtraction of BigInt's and the conversion to and from various other formats.
To add this library to your project, add #require "BigInt.class.nut:1.0.0"
to the top of your agent/device code.
NOTE: If you need the cmp()
, add()
or sub
functions, you will also need include the add #require "BigIntLib.class.nut:1.0.0"
to the top of your agent/device code.
You can view the library's source code on GitHub.
A BigInt object must be instantiated with an initial value. The initial value can be an integer (e.g. 12), a float (e.g. 12.34, will be truncated), a hex string ("-0x1234") or another BigInt.
#require "BigInt.class.nut:0.0.1"
// Instantiate a new BigInt with an initial value of 100
local val1 = BigInt(100);
// Instantiate a new BigInt with an initial value of 123.45 which will be truncated to 123 internally
local val2 = BigInt(123.45);
// Instantiate a new BigInt with an initial value of -100 billion
local val3 = BigInt("-0x174876E800");
// Instantiate a new BigInt with another BigInt
local val4 = BigInt(val3);
Returns a hex string of the BigInt value. Positive integers will look like "0x01AB" and negative integers will looks like "-0x01AB". There will be a maximum of one leading zero to ensure the number of hex digits is always even.
server.log( BigInt(-12345678).tostring() ); // Should output "-0xBC614E"
Returns a blob of the BigInt value. The first byte is the sign, with 0x00 being positive and 0xFF being negative. The remaining bytes are LSB first. This is mostly used internally but can also be useful for streaming to a storage media, like SPIFlash.
// Write a BigInt to spi flash prefixed by the length
local data = BigInt("-0x123456").toblob();
spiflash.write(data.len());
spiflash.write(data);
Returns another BigInt holding the absolute value of the current BigInt (the sign set to positive).
// Instantiate a new BigInt with an initial negative value and then retrieve the absolute value
val1 <- BigInt("-0x123456").abs(); // Should be equal to BigInt("0x123456")
A helper function for the BigIntLib.add()
function. Returns a new BigInt with the value of the operand added to the current value.
// Instantiate a new BigInt and add a value to it
val2 <- BigInt(-10).add(20); // Should be equal to BigInt("0x0A")
A helper function for the BigIntLib.sub()
function. Returns a new BigInt with the value of the operand subtracted from the current value.
// Instantiate a new BigInt and subtract a value from it
val3 <- BigInt(10).sub(BigInt(20)); // Should be equal to BigInt("-0x0A")
A helper function for the BigIntLib.cmp()
function. Returns -1 when the current value is less than the operand's value, 0 when the they are equal and +1 when the current value is greater than the operand's value.
// Compare two BigInt's
local cmp = BigInt(-10).cmp( BigInt(20) ); // Should be equal to -1 as -10 < 20
The BigInt class is licensed under the MIT License.