“This is the 29th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
⭐ ⭐
Difficulty rating is subjective, the highest five stars, the lowest one star, do not spray.
What is BigInt? What problem was solved?
Answer: BigInt is a basic JS data structure, similar to the Number type. Used to accurately (safely) represent, store, and compute large integers.
MDN: BigInt is a built-in object that provides a way to represent integers greater than 2^ 53-1. This was originally the largest Number that could be represented as Number in Javascript. BigInt can represent an arbitrarily large integer.
describe
You can define a BigInt by adding n to an integer literal, or you can call the function BigInt().
const num = 10
const num1 = 10n
const num2 = BigInt(10)
num1 === num2 // true
num1 == num // true
num1 === num // false
typeof num // number
typeof num1 // bigint
Copy the code
operation
The following operators can be used with BigInt: +, -, *, /, **, %. Bitwise operations other than >>> (unsigned right shift) are also supported. Since BigInt is signed, >>> (unsigned right shift) cannot be used for BigInt.
In order to understand Vue3 diff algorithm, I learned JS bit operation
const num = 32n
const num1 = num + 1 Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
const num2 = num + 1n // 33n
const num3 = num - 1n // 31n
const num4 = num * 1n // 64n
const num5 = num / 2n // 32n
const num6 = num ** 2n // The 1024n ** operator is equivalent to math.pow
const num7 = num % 30n // 2n
Copy the code
What problem does BigInt solve?
-
The intuition is big integers
-
Securely store and evaluate large integers
Large integer representation
When you enter a long number, JS always converts it to a scientific count, and even if you call the toString() method, you can’t get the real number it represents.
BigInt can solve this problem and display the entered numbers correctly, as shown below,
Calling toString() still returns the desired value, as shown below.
Securely store and evaluate large integers
MAX_SAFE_INTEGER is 2^ 53-1 (9007199254740991). If the Number exceeds this value, the computation may cause some problems.
BigInt solves this problem and calculates correctly.
Associated algorithm, with JS to achieve large integer addition
How did you add large integers before BigInt?
The idea is to draw something like this on a sketch pad like you did in elementary school when you learned how to add, add 1 in every 10, and then deal with some boundary problems.
function add(a, b) {
let i = a.length - 1;
let j = b.length - 1;
let carry = 0; // Store carry
let ret = ' ';
while (i >= 0 || j >= 0) {
let x = 0;
let y = 0;
let sum;
if (i >= 0) {
x = a[i] - '0';
i --;
}
if (j >= 0) {
y = b[j] - '0';
j --;
}
sum = x + y + carry; // Add carry
if (sum >= 10) { // Enter 1 for every 10
carry = 1;
sum -= 10;
} else {
carry = 0;
}
/ / 0 + ' '
ret = sum + ret;
}
if (carry) {
ret = carry + ret;
}
return ret;
}
add('999999999999999999999999'.'1') / / '1000000000000000000000000'
Copy the code
We used to wrap functions to perform large integer operations. Now we just call BigInt, which is convenient.
summary
BigInt can accurately represent, store, and evaluate long integers.
I believe JS will get better and better and will be able to solve similar problems like 0.1 + 0.2! == 0.3 accuracy problem!
This is the 29th day of alynn’s continuous blog post, exporting insight techniques, goodbye ~