Page 1 of 1

Rounding problems

Posted: Wed Nov 10, 2004 3:40 pm
by Heartattack!
Could someone help me to round properly.
If I am to round to 3 digits after the decimal point, what would the following produce?

Code: Select all

1.0004444444444444444444
1.00050000
1.00051
1.00045
1.0006
1.00044435
Is there any short cut function in C++ to help in the rounding? Thanks in advance.

Posted: Wed Nov 10, 2004 4:47 pm
by Maniac
When I use C or C++, I use the printf function for rounding:

Code: Select all

double d = 1.00045;
printf("%.3lf", d);
Note that this function does NOT always round numbers correctly in the critical cases (although there are lots of problems where correct rounding gives WA and printf-rounding gives AC). Also, sometimes adding a very small number like 0.00000001 before rounding with printf changes WA into AC. But to me it's unclear when this should or shouldn't be done...

When I use Java, I have my own rounding function. This one works correctly but sometimes I get WA because the judge has used printf for rounding and the result differs in critical cases. So generally, whenever rounding is involved I avoid Java because of this....

Does this help a bit?

Posted: Thu Nov 11, 2004 2:27 am
by Heartattack!
Thanks for the tip. I noticed this before, though. It's quite frustrating. Yesterday I wrote a function just to round the floats like it's actually done. And guess what? I got WA. There should be some sort of standard set for all the problems. Either have us round properly or use printf("%0.3Lf",...). I checked with cin.precision. It gives the same output as printf. I've run into this problem quite a few times. I guess we can only hope and try the hit-or-miss method until the ambiguity is resolved. :lol: