Hello, I decided to write a program, I need to add two double numbers in it ... And what, when adding a double 0.7 + 9 = 9.699999999. Here is the code:

g++; for (int k = 0; k < g; k++) { r = r * 0.1; } r1 = r1 + r; 

r is the same 0.7, a r1 = 9 ... What is the error ??

1 answer 1

You have reduced r by g times it does not work out 0.7, and that’s exactly 0.69999999, this is due to the fact that the computer is running on a binary system and there 10 is 1010 - not a round number. There is no error here, and if you need to check equality later, you can check it like this:

 eps=0.00001; if (abs(r1-r2)<eps) { //... }