Rounding problems

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:

Rounding problems

Post by Heartattack! » Wed Nov 10, 2004 3:40 pm

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.
We will, We will BREAK LOOP!!!!

Maniac
Experienced poster
Posts: 105
Joined: Tue Oct 14, 2003 3:24 pm
Location: Utrecht, Holland

Post by Maniac » Wed Nov 10, 2004 4:47 pm

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?

Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:

Post by Heartattack! » Thu Nov 11, 2004 2:27 am

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:
We will, We will BREAK LOOP!!!!

Post Reply

Return to “Algorithms”