Need help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help

0.2 + 0.2 + 0.2 == 0.6 returns False and 0.2 + 0.2 == 0.4 returns True. What's the reason for this?

14th Jul 2023, 9:24 AM
Ashly Mathew
3 Answers
+ 4
It is inherent to how computers represent data. IT DOES NOT MEAN THAT THE COMPUTER WRONG.
14th Jul 2023, 9:53 AM
Lisa
Lisa - avatar
+ 3
You should take a look at this: https://www.sololearn.com/post/1713746/?ref=app Floating point numbers can never be precise, they are only approximations constrained by the number of bits used to store the value. So comparing for equality should be done with regard to an acceptable error threshold.
14th Jul 2023, 9:49 AM
Tibor Santa
Tibor Santa - avatar
0
Are you using Python? Try below code and see the result. x = 0.2 + 0.2 + 0.2 print(x) Don't ask me detail, I don't know much. As far as i know computer use of binary system (0 and 1) is the root reason, which have an impact on how accuracy a computer can handle floating point number.
14th Jul 2023, 9:37 AM
Wong Hei Ming
Wong Hei Ming - avatar