How to use the BigNum function from bindings

Find comprehensive JavaScript bindings.BigNum code examples handpicked from public code repositorys.

bindings.BigNum is a constructor function that creates a JavaScript object representing an arbitrarily large integer using a native C++ library.

-3
fork icon122
star icon415
watch icon12

How does bindings.BigNum work?

bindings.BigNum is a constructor function provided by the node-bigint package, which allows you to work with arbitrarily large integers in JavaScript. Internally, bindings.BigNum creates a new instance of a native C++ object that implements arithmetic operations on large integers. This object is then wrapped in a JavaScript object that provides a familiar interface for performing arithmetic operations on large integers. Because JavaScript natively supports 64-bit integers, it can only represent integers up to 2^53 - 1 without loss of precision. However, using bindings.BigNum, you can represent integers of arbitrary size and perform arithmetic operations on them just like you would with regular JavaScript numbers. bindings.BigNum provides a variety of methods for performing arithmetic operations on large integers, including addition, subtraction, multiplication, division, and modulus. It also provides methods for converting between different bases (such as hexadecimal or binary), and for performing bitwise operations like AND, OR, and XOR. Because bindings.BigNum relies on a native C++ library for its operations, it is generally faster than pure JavaScript implementations of large integer arithmetic, and is especially useful for applications that require high-performance arithmetic on large integers.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const bindings = require("bindings")("bigint.node");

// Create two BigNum objects
const a = new bindings.BigNum("12345678901234567890");
const b = new bindings.BigNum("98765432109876543210");

// Perform arithmetic operations
const sum = a.plus(b);
const difference = a.minus(b);
const product = a.times(b);
const quotient = a.dividedBy(b);
const remainder = a.modulo(b);

// Log results to the console
console.log(`a = ${a}`);
console.log(`b = ${b}`);
console.log(`a + b = ${sum}`);
console.log(`a - b = ${difference}`);
console.log(`a * b = ${product}`);
console.log(`a / b = ${quotient}`);
console.log(`a % b = ${remainder}`);

In this example, we first import the bindings module and call its BigNum method to create a new BigNum constructor function. We then use this constructor function to create two BigNum objects, a and b, each initialized with a string representation of a large integer. We then perform several arithmetic operations on these BigNum objects using methods like plus(), minus(), times(), dividedBy(), and modulo(). These methods return new BigNum objects representing the result of the operation. Finally, we log the original BigNum objects as well as the results of the arithmetic operations to the console. Note that because bindings.BigNum is a constructor function, you must call it using the new keyword to create new BigNum objects.

Other functions in bindings

Sorted by popularity

function icon

bindings.createKey is the most popular function in bindings (8616 examples)