Numbers are just the numeric values like:
1
23
4534
3.23
-34, 53
0
-0.42
Let’s start off with a quiz.
What is the bit pattern if given a number?
Example 1
11
Here you need to understand how binary bit patterns work. This is basically the reverse quiz of a previous article on what are bits in programming I’ve made here so check it out for some pretty basic practice examples.
By the way, whole numbers are also known as integers.
Basically, we understand that you want to read binary bits from right to left increasing by a factor of 2. So the first bit on the right is equal to 1, while the one to the left of it is 2, and to the left of that is 4, then 16, then 32, and so on until it reaches the 8th bit which is equal to 128.
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
The answer is:
00001011
This is exactly equal to the number 11 because 8 + 2 + 1 = 11.
These 0s and 1s are actually stored in the computer’s memory.
Understanding JavaScript’s 64-bit
JavaScript only has 64 bits and that limits the number of possibilities that exist. That means it’s 2 to the power of 64 (N^64). The reason for N equaling 2 is because there are only 2 possibilities which are either 0 or 1. This ultimately results in 18 quintillion possibilities!
Out of the 18 quintillion bits, within that, includes negative numbers with 1 bit that represents the negative sign. There’re also bits that represent the decimal sign. So realistically, the actual amount of whole numbers really only makes up about 9 quadrillions.
How are decimal/fractional numbers written in JavaScript?
Normal decimal numbers are written just like in math. Check out the example below.
123.23
12.3
9.04
-39.40
-0.10
If the number is huge, you can use the e for the exponent then follow by the actual power such as so:
4.123e3
This is basically the same as 4.123 x 10^3 which is equal to:
4123
JavaScript numbers are not precise after using up 64-bits
What this means, is that as mentioned above, there are really only 9 quadrillion integers that are guaranteed in JavaScript.
It turns out fractional numbers (not whole numbers) are not precise compared to whole number integers.
Just make sure you understand that fractions in JavaScript are approximations and not exact values.