+ 2

Why this happens?

When I add floats and get the sum of them, the result is inaccurate. https://code.sololearn.com/cz0swy0QVg0X/?ref=app

8th Jan 2020, 7:16 AM
B.virus
B.virus - avatar
6 Answers
+ 1
What output did you expect actually?
8th Jan 2020, 7:38 AM
Ipang
+ 1
As I understand it, floating point calculation accuracy is a tricky problem, maybe someone can explain about that better. Although this may not be a definite solution (nor even work), you can try to use double in place of float.
8th Jan 2020, 8:23 AM
Ipang
+ 1
B.virus Write this after for loop, if you mean to truncate after 72.87000 printf("%.5f\n",sum);
8th Jan 2020, 8:24 AM
Jayakrishna 🇼🇳
+ 1
U can take double in place of float
9th Jan 2020, 8:07 PM
Light â™Ș
Light â™Ș - avatar
0
I writed printf outside the loop, and the result is 72.87003.
8th Jan 2020, 7:29 AM
B.virus
B.virus - avatar
0
72.87000 is what I expect
8th Jan 2020, 8:12 AM
B.virus
B.virus - avatar