Nas's blog

By Nas, 20 months ago, In English

Hi everyone, as far as I know double is more precise than float, and we are recommended to use double instead of float.
For this example, double produces a less accurate result than float.

cout << fixed << setprecision(1);  
cout << 0.49F * 85 << endl; // 41.7.   
cout << 0.49 * 85 << endl; // 41.6.  

The problem asks to round the answer to one decimal place.
0.49 * 85 = 41.65 ==> 41.7.
In competitions, how those cases being treated?

  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
20 months ago, # |
  Vote: I like it +1 Vote: I do not like it

First, if you can avoid working with decimal numbers, it is highly recommended to do so. And in most problems, if you need to answer with a decimal number, you are allowed to have some small precision error. So for example, outputting 0,000 0001 instead of just 0 would be considered correct. The maximal error is usually specified in the problem statement. But yes, double is more precise than float, but it takes up more memory.

»
20 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Just use long doubles and don't care :)