views:

502

answers:

6

In programming contests, floating point arithmetic related questions say "the error is answer must be less than 1e-6" or "The answer must be correct upto 6 decimal places". Does this mean that I can perform calculations on FP variables without worrying about the precision and only while printing I should write like

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

Am I understanding it correctly? And do the above 2 quotes mean the same thing? In one of the questions, when I used a double array and performed some calculations and printed one of the array elements. It printed "-0.000000". What does this mean? But when I used vectors in C++ like

vector<double> arr(10,0.0);

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

+2  A: 

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

The IEEE standard for floating point numbers have signed zeroes. see here

Charles Ma
+1  A: 

In floating point arithmetic sign is not a part of the number so there is both -0 and 0.

Cem Kalyoncu
But why did it show differnece with just a simple array and vector
avd
There shouldnt be any differences.
Cem Kalyoncu
We need to see a bit more of your code plus know whet your compiler is to check what the printf does exactly when showing 6 decimal places
Mark
+2  A: 

An error of 1e-06 is 0.000001, and affects the 6th decimal place, but I think only a pedant would insist there's a difference between the two quotes. (I'm one, and it takes one to know one)

The requirement about preserving accuracy to 6 decimal places is probably meant to remind you that the accuracy of a calculation is only as much as the least accurate figure used in the calculation.

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

pavium
A: 

Blockquote do the above 2 quotes mean the same thing?

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

stonemetal
So I just do the calculations in double data type and to report the answer in 6 decimal places precsion I use %.6lf. Is that correct or is there any rounding function to round it to 6 decimal places
avd
The wording is 6 decimal places which usually means correct to 6 places after the decimal point
Mark
The precision that stonemetal describe is 6 significant figures
Mark
@aditya It really depends on the numbers you are working with. If your answer is in the thousands then no using a double isn't going to get you six decimal places of precision. Is only going to get you three decimal places of precision, and you will need to use an arbitrary percision math lib. If your answer is less than one yes use doubles.
stonemetal
+1  A: 

The accuracy does not only depend on the the type used, but also on how you compute things.

For instance you want to compute this :

1e9 + 1e-9 - 1e9

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

Using float or double is not enough to be sure to get the right 6 digits. You have to estimate what is the possible error at each step.

You should read about Numerical analysis

fa.
+1  A: 

There is no way to know if float is sufficient or if you have to use a bool or even better precision; some algorithms can destroy precision rapidly. For example computing values by summing up asymptotic series you can reach a point where any too low precision (be it 5 or 15 digits) simply blows up. You can read about such things for example in this blog post.

And in that blog post, why does his "float" (Python float = double precision) fail but hist own routines make it? His routines not only can use arbitrary precision, but they monitor the calculation to add precision when the error goes up. Only that way can you be sure.

kaizer.se