Floating point value is inaccurate.
This is largely the answer to the question. There is ultimate accuracy, which means that some numbers cannot be represented accurately.
Some languages support arbitrary precision numeric types / rational / complex numbers at the language level, etc., but not Javascript. Neither C nor Java.
The standard value of the floating point IEEE 754 cannot represent, for example. 0.1 sure. That is why numerical calculations with cents, etc. Must be performed very carefully. Sometimes the solution is to store the values in cents as integers, and not in dollars, like floating point values.
The concept of a "floating" point, analogue in base 10
To understand why floating point values are inaccurate, consider the following counterpart:
- You have enough memory to store 5 digits
- You want to be able to represent values in a wide range as practical as possible.
When representing integers, you can represent values in the range from -99999 to +99999 . Values outside this range will require you to remember more than 5 digits that (for this example) you cannot do.
Now you can consider a fixed-point view, something like abc.de Now you can represent values in the range from -999.99 to +999.99 , up to two digits of accuracy, for example. 3.14 , -456.78 , etc.
Now consider the floating point version. In your resourcefulness, you have come to the following scheme:
n = abc x 10 de
Now you can only remember 5 digits a , b , c , d , e , but now you can imagine a much wider range of numbers, even non-integer ones. For instance:
123 x 10 0 = 123.0
123 x 10 3 = 123,000.0
123 x 10 6 = 123,000,000.0
123 x 10 -3 = 0.123
123 x 10 -6 = 0.000123
This is how the name "floating point" appeared: the decimal point "floats around" in the above examples.
Now you can imagine a wide range of numbers, but note that you cannot imagine 0.1234 . You also cannot submit 123,001.0 . In fact, there are many meanings that you cannot imagine.
This is pretty much why floating point values are inaccurate. They can represent a wide range of values, but since you are limited by a fixed amount of memory, you must sacrifice accuracy in magnitude.
Additional specifications
abc is called significant , the so-called coefficient / mantissa. de is an exponent as a scale / characteristic. As usual, base 2 is used instead. 10. In addition to remembering “numbers” (a bit, really), it should also remember signs of significance and indicator.
A single-precision floating-point type typically uses 32 bits. Double precision typically uses 64 bits.
see also