Floating point pressure error
console.log(0.1 * 0.2) // Returns 0.020000000000000004 // Should return 0.02
How to handle binary numbers differently
In base 10, which is the basis of the routine we use in our daily lives, we can represent numbers such as 0.1 As a limited number; This means that it can be represented by a decimal place. If we try to write it as binary, we find something strange.
0.0001100110011001101 ... repeats to infinity
You, 0.1 In binary a Decimal is repeating. This means that if we put this number in four, ten point four, we get 00001, which when we convert back to base 10, 1/16 Slightly different from our original value 1/10.
What is a floating point?
In computing, there is a balance between accuracy and performance. To try to strike a balance, a group came up with a standard type called “named” Floating point, Which is defined by the standard IEE 754.
It takes a number, e.g. 0.1, And represents it in binary representation. As you may have guessed, since then 0.1 Repeat decimals in binary, floating point representation will be limited 0.1 Decimal in a certain number. This is represented below:
SIGN | EXPONENT | MANTISSA 0 01111011 10011001100110011001101
The 32-bit floating point type represents our number in the scientific indicator, which you may have seen 1 x 10-4 in the past. For binary, the difference we use x2 Since this is base 2, there will be an example 1 x 2-4. Therefore, the floating point binary representation is divided into a few pieces:
- The first digit is SymbolSo, if it’s negative, it’s 1.
- The next 8 digits are AtonementThat is, the number we increase x 2 To.
- The next 23 digits are Montessori, Which is the number multiplied by x 2.
This gives us a balance of numbers that we can refer to, while still performing well.
Back to 0.1
In our floating point indicator, we limit 0.1 in binary to a certain number of numeric places. The mathematical indicators we have given in binary are shown below:
SIGN | EXPONENT | MANTISSA 0 01111011 10011001100110011001101 # is the equivalent in mathematical notation of ... 1.600000023841858 * 2^-4
If you try to put 1.600000023841858 x 2-4 In Google, you will find a price 0.10000000149. As we have said, since binary numbers are represented differently, that is, certain numbers are repeating decimals when they were not normally in the 10’s. When we try to represent them in binary, we have to take them up to a certain amount of decimals.
When we go back to base 10, then this round appears, Which means that once we have the exact base number 10, 0.10000000149. Now, if we try to use it in calculations, we end up with an error, which is why
console.log(0.1 * 0.2) Returns 0.0200000000000000004.
Can I avoid it?
Some languages are of the decimal type, Which is one way to solve this problem. However, you don’t necessarily need it. For such simple mistakes, you can reduce the number to your desired decimal length. Overall, this is an interesting example of how computers try to balance speed with performance.