Does 0.7 + 1.4 === 2.1 is false in programming??

0.7 + 1.4 === 2.1 Is False
Last Updated on

In programming languages, does 0.7 + 1.4 equal to 2.1?

For most people, you probably would answer this question just like a normal math calculation. The 0.7 + 1.4 will be 2.1, just as simple as that. But wait… Let’s try it out!

Now we open the chrome browser and try it out with JavaScript developer console.

JavaScript

Hmm… Okay… round it up will be equal to 2.1 …

Python 3.6

Python 3.6.1

It seems like python 3.6 result is same with JavaScript as well.
Michael: Okay… but it can’t say 0.7 + 1.4 is not 2.1 right?
Jorcus: Let’s check it out in the JavaScript developer console!

JavaScript

You see that 0.7 + 1.4 is not equal to 2.1. It’s basically it has discussed a question in the past. Just like a question of “Why 0.1 + 0.2 !== 0.3?”.

This is a kind of tricky questions if you didn’t aware that the floating-point numbers can be a problem sometimes when you debug. This is because most of the programming languages can’t represent the floating-point numbers accurately. So that, if you are calculating the numbers in floating-point, you should be careful because it may not get the result as you expected.

There is a great table for all the floating decimal points in almost all programming languages! Check this out https://0.30000000000000004.com/ Also, I would recommend check a useful tools to check the exact value of a floating-point value from http://pages.cs.wisc.edu/~rkennedy/exact-float

Further readings:
Floating Point Arithmetic: Issues and Limitations
StackOverflow: Is floating point math broken?
What Every Computer Scientist Should Know About Floating-Point Arithmetic
https://0.30000000000000004.com/
WikiPedia: Floating-point arithmetic