JavaScript curiosity question, simple substraction

Hello. I have a simple question out of curiosity, it isn’t something complicated really and it may be dumb but I really want to know about it - and if there is an answer for this already (which I’m sure there is), I haven’t looked for it because I don’t exactly know how to ask about this.

I had just finished the Conditionals topic on the “Learning JavaScript” course, and on the review page I made a switch statement, just to practice it a bit more. I tried to incorporate more into the logs, from the first topic lessons for example. The point is, I was substracting 0.10 out of “apple”, defined as 0.80, which equals 0.70 - now, what I’m wondering is: why did the console print out “0.7000000000000001” as a result instead of the value mentioned above - 0.70? I was not expecting this and I’m a bit confused.

Python has a good explanation on this matter:

https://docs.python.org/3/tutorial/floatingpoint.html

which also applies to JavaScript. Its a tough read, the TL;DR version is that computers store numbers differently (base 2) then we humans (base 10).

1 Like

Thank you! That’s very interesting, I certainly wasn’t aware. :slight_smile: