Floating Point Arithmetic
Open up your browser, and press F12, in most browsers it should give you a developer console(if not goto settings and then look for developer console). Type in the following:
1 2 | 0.1+0.2==0.3 false |
The summation of 0.1 and 0.2 does not equal 0.3, that seems absurd to many who are noobs at programming. For someone who knows just enough about this concept will quickly say that the programming language is implementing a IEEE-754 Floating point Arithmetic.
Many a teacher(books,person,online training.etc..) often say that discussing further into this topic is not in the scope of their current curriculum and often give a online resource like this to go and do a more thorough understanding by the student themselves. I can somewhat agree to that argument, but the onus is on the teacher to atleast ease out the initial understanding of the concept by giving a more relatable and simple explanation, which can serve as a stepping stone for a more formal mathematical proof.
Here is one such explanation taken from this website:
When you have a base 10 system, it can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be expressed cleanly because the denominators all use prime factors of 10. In contrast, 1/3, 1/6, and 1/7 are all repeating decimals because their denominators use a prime factor of 3 or 7. In binary (or base 2), the only prime factor is 2. So you can only express fractions cleanly which only contain 2 as a prime factor. In binary, 1/2, 1/4, 1/8 would all be expressed cleanly as decimals. While, 1/5 or 1/10 would be repeating decimals. So 0.1 and 0.2 (1/10 and 1/5) while clean decimals in a base 10 system, are repeating decimals in the base 2 system the computer is operating in. When you do math on these repeating decimals, you end up with leftovers which carry over when you convert the computer’s base 2 (binary) number into a more human readable base 10 number.
There are quite a few answers covering this topic on stackoverflow which will serve you well, irrespective of your current understanding on this topic.
Type in the following in the developer console to goto an appropriately named fun & informative website for Floating Point Arithmetic:
1 | window.location=`https://${0.1+0.2}.com` |
-Albert Einstein