2. Special Numbers
3. How to represent big numbers?
Sign — 1Bit
Exponent — 11Bits
Significand — 52Bits
This representation is called the double precision floating point format for representing numbers
- 2⁵³-1 to +2⁵³-1
-9007199254740991 to +9007199254740991
Any number above that will give unpredicted results during comparisons and arithmetic operations such as
2⁵³ + 4 == 2⁵³ + 5
The double precision floating point format for numbers cannot represent some decimal numbers accurately. The reason is that all the numbers in this format are represented as two to the power of some whole number. Even the decimal parts of it. Because of this some decimal values such as 0.1 which is 1/10 whose denominator is not a power of two cannot be represented accurately in binary representation.
Because of this the following operation like
0.1 + 0.2 === 0.3
As I read an article about solving the 8-queen problem storing the board in a 64bit integer (French) I wanted to test…
The Number web API is an object that contains all the basic funtionality required to perform some operation on numbers. Just type Number in your browser terminal to access it. It has some special number declaration in it. Some of them which can be useful in certain situations are as follows
Number.MAX_SAFE_INTEGER = 2⁵³ -1
Number.MIN_SAFE_INTEGER = -2⁵³ -1
How to represent big numbers
So now that we know we can’t represent numbers greater than 2⁵³-1 without breaking the arithmetic and comparable functionality over them, is there a way we can represent big numbers?
The BigInt spec was introduced to address this problem. This specification is still in the draft stage so do not use it in production environment. Some important things to note with respect to BigInt are
2. You can typecast between BigInt and Number but it will lead to loss in precision.
3. BigInt supports +, -, *, /, **, Bitwise operators except(>>>)
4. The division of a BigInt with another BigInt will not give you fractional values. It will be truncated.
The details info on BigInt is available in the following link.
Thank you :-)