Take the 2-minute tour ×

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


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?

share|improve this question
to preserve precision as much as possible you must optimize your calculations (for example do not add numbers with very different exponent values, do not multiply too small or big numbers. together instead multiply small number with big one if possible, sort subdivisions so that abs of sub result is around 1.0 .. and so on) –  Spektre Aug 20 '13 at 13:19

6 Answers 6

up vote 0 down vote accepted

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.

share|improve this answer
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 Sep 16 '09 at 12:47
The wording is 6 decimal places which usually means correct to 6 places after the decimal point –  Mark Sep 16 '09 at 13:25
The precision that stonemetal describe is 6 significant figures –  Mark Sep 16 '09 at 13:26
@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 Sep 16 '09 at 20:42

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer
But why did it show differnece with just a simple array and vector –  avd Sep 16 '09 at 12:39
There shouldnt be any differences. –  Cem Kalyoncu Sep 16 '09 at 14:01
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 Sep 16 '09 at 14:56

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.

share|improve this answer

Your Answer


