Can anyone tell me why 0.1+0.2==0.3 gives false answer? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone tell me why 0.1+0.2==0.3 gives false answer?

28th May 2020, 6:59 PM
Heramb Shrawge
Heramb Shrawge - avatar
3 Answers
+ 1
Well it's much interesting. Listen, We can only express fractions that denominater have prime factors of the base. Otherwise we get repeating decimals. Like in base 10, 1/8 1/4 can be expressed cleanly whereas 1/7 1/3 can't Same in base 2, 1/10(0.1) and 1/5(0.2) are repeating decimals When we add them they get leftover which get carried up when converted to base 10(decimal). Thus, 0.1+0.2 != 0.3 They actually Equals 0.30000000000000004 Thank You ☺️
28th May 2020, 7:18 PM
Hacker Badshah
Hacker Badshah - avatar
0
The problem is that floating point numbers don't have unlimited precision. Therefore, instead of checking for equality, you should check, if they are almost equal. For more information: http://0.30000000000000004.com/
28th May 2020, 7:20 PM
Michi
Michi - avatar
0
Because computers can't count
2nd Jun 2020, 8:42 AM
Atharva Shrawge
Atharva Shrawge - avatar