-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path47_big_int.js
93 lines (83 loc) · 3.21 KB
/
47_big_int.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* ========================================================
* BigInt
* ========================================================
* BigInt is a built-in object that provides a way to represent whole numbers larger
* than 2^53 - 1, which is the largest number JavaScript can reliably represent using the Number primitive.
*/
/**
* ========================================================
* 1. Basic Syntax
* ========================================================
* BigInt can be defined either by appending 'n' to the end of an integer literal or
* by using the BigInt constructor function.
*/
const bigIntLiteral = 1234567890123456789012345678901234567890n;
const bigIntObject = BigInt("1234567890123456789012345678901234567890");
/**
* ========================================================
* 2. Arithmetic Operations
* ========================================================
* Arithmetic operations are supported on BigInts much like they are for Numbers,
* but you can't mix BigInts and Numbers in these operations.
*/
const sum = bigIntLiteral + 1n;
// const invalidSum = bigIntLiteral + 1; // This will throw a TypeError
/**
* ========================================================
* 3. Comparison
* ========================================================
* BigInt can be compared using all the regular comparison operators.
* Just like arithmetic operations, you can't directly compare a BigInt with a Number.
*/
const isTrue = bigIntLiteral > 1000n; // Evaluates to true
/**
* ========================================================
* Nuances and Advanced Techniques
* ========================================================
*/
/**
* 1. No Auto-Type Conversion
* ---------------------------
* BigInt does not automatically convert into a string or a number.
* Explicit conversion is often required when you're working with mixed types.
*/
const bigIntStr = String(bigIntLiteral);
// const invalidOperation = bigIntLiteral + " is big."; // This will throw a TypeError
/**
* 2. Divisions Are Floored
* ------------------------
* When performing division using BigInt, the division is floored,
* meaning it rounds towards zero.
*/
const flooredDivision = 11n / 3n; // Result will be 3n
/**
* 3. Bitwise Operations
* ---------------------
* You can perform bitwise operations with BigInt.
* Both operands must be BigInts to perform these operations.
*/
const bitwiseAnd = 16n & 5n; // Output will be 0n
/**
* 4. No Support for Math Object
* -----------------------------
* BigInt cannot be used with the JavaScript Math object's methods.
*/
// const sqrtBigInt = Math.sqrt(bigIntLiteral); // This will throw a TypeError
/**
* 5. JSON Serialization
* ----------------------
* JSON.stringify() can't serialize BigInt values,
* you'll need to manually convert them to strings before serialization.
*/
// const bigIntJSON = JSON.stringify({value: bigIntLiteral}); // This will throw a TypeError
/**
* 6. Compatibility
* ----------------
* BigInt is not yet universally supported across all environments,
* so you may need to check for compatibility before using it.
*/
if (typeof BigInt !== "undefined") {
const bigIntSupport = BigInt(10);
console.log(`BigInt supported: ${bigIntSupport}`);
}