Блог пользователя Nas

Автор Nas, 2 года назад, По-английски

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?

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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.

»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Just use long doubles and don't care :)