How to handle precision in floating point arithmetic in C

In programming contests, floating point arithmetic questions say "the error must be less than 1e-6" or "the answer must be correct up to six decimal places." Does this mean that I can do calculations on FP variables without worrying about precision and only during printing should I write, for example,

printf("%.6lf",a);

      

Am I getting it right? And these two quotes above mean the same thing? In one question, when I used a double array and did some calculations and printed one of the elements of the array. He printed "-0.000000". What does it mean? But when I used vectors in C ++ like

vector<double> arr(10,0.0);

      

the same calculations are printed "0.000000". Why is there such a difference?

+2


source to share


6 answers


Blockquote, do these two quotes above mean the same thing?



Obviously not. 1-e6 says that your answer should be within .000001 of the correct one. 6 points of precision means 12345678 is correct even if the answer is 12345689. The difference is much larger than .0000001. Now if your answer is .100001 (with the correct answer being .100000) then they are the same thing.

0


source


If you need exactly 6 decimal places, use at least a double. depending on how many floating point calculations you do, the butterfly effect can easily affect your answer.



The IEEE floating point standard has zero values. see here

+2


source


The 1e-06 error is 0.000001 and affects the 6th decimal place, but I think only a pedant would insist on the difference between the two quotes. (I am alone and he alone knows)

The requirement to preserve precision to six decimal places should probably remind you that the precision of the calculation is only the smallest exact digit used in the calculation.

So you couldn't calculate the circumference of a circle to six decimal places if you used 3.1416 as PI, no matter how accurately you measured the radius

+2


source


The accuracy depends not only on the type you use, but also on how you calculate things.

For example, you want to calculate this:

1e9 + 1e-9 - 1e9

The correct answer should be 1e-9, but in that order, 1e-9 is lost when added to 1e9 and gives 0.

Using float or double is not enough to get the correct 6 digits. You must evaluate what the possible error is at each step.

You should read about Numerical Analysis

+2


source


The floating point arithmetic sign is not part of the number, so there are both -0 and 0.

+1


source


There is no way to tell if float is good enough or if you need to use bool or even better precision; some algorithms can quickly destroy precision. For example, by calculating values ​​by summing asymptotic series, you can reach the point at which any precision that is too low (be it 5 or 15 digits) just explodes. You can read about such things for example in this blog post .

And in this blog post, why is his "float" (Python float = double precision) failing, but his own routines? Not only can its routines use arbitrary precision, but they track the calculation to add precision as the error increases. This is the only way you can be sure.

+1


source







All Articles