Help with Rounding (task 6)

Back to Problem Solutions forum

alidarakhshaan     2014-10-24 11:33:19

Hi, I have been trying to solve this problem but haven't solved it yet, I think I'm close but there's something wrong with code that I haven't figured out yet, please take a look at my code.

Code:

// Admin: I edited this post and removed the code, I'm sorry :)
Rodion (admin)     2014-10-24 15:15:53
User avatar

Hi!

Thanks for your message!

See, your code always returns values 1 less than expected.

You do right with floor(result + 0.5)

However your result already have floored value after integer division you perform. You should instead do it with double variables for example.

See, for example you divide 5 by 2. Since you use int everywhere, your division gives 5 / 2 = 2 (and 1 remainder is lost). You then add 0.5 to get 2.5 and again floor it down so you get 2 istead of 3.

You should instead use float (double) division - then you will get 5.0 / 2.0 = 2.5 - you will add 0.5 to get 3 and flooring will return you 3 as expected.

E.g. in C you may use anything like this:

int x, y;
int res = (int) (x / (double) y + 0.5);

// or

double x, y;
int res = floor(x / y + 0.5);

I hope my clumsy explanations may help!

alidarakhshaan     2014-10-24 15:57:20

Ahhh.... I tried using double for res instead of int. I was confused and yes, your explanation did help. Thanks a lot.

Goodwin Lu     2015-12-04 21:46:38

I keep on getting too less even though I tried to do the int thing.

include <iostream>

include <math.h>

using namespace std; int main() { int a; int b; int c; cin >>a; for (int i =0; i<a; i++) { cin >> b; cin >> c; int result = floor(b / c + 0.5); cout<< result<<" "<<endl; } } input code: 17 1278632 348 -3163976 -1920865 6194580 -3266413 13617 694 5851582 68 13271 1814 2626597 3076823 5703 1238 5840218 2298130 7149985 957 8051952 988 -7053871 -1696588 13843 624 4035998 741 5739760 280 2814714 859 6908422 491

expected: 3674 2 -2 20 86053 7 1 5 3 7471 8150 4 22 5447 20499 3277 14070 actual: 3674 1 -1 19 86052 7 0 4 2 7471 8149 4 22 5446 20499 3276 14070

Why does this keep on happening?

Quandray     2015-12-05 08:01:57
User avatar

Hi,

I see that you've solved the problem now.

In your code above, b and c are integers so b/c will also be an integer.

Prabhat_SY     2020-10-07 14:16:21

x=int(input()) l=[] for i in range(x): a,b=map(int,input().split()) l.append(round(a/b)) for i in range(x): print(l[i],end=' ')

This is my code for rounding problem but the solution says my answers are wrong. Can you please check and tell me what's wrong with my code.

crydolphin666     2020-10-07 14:49:50

Prabhat_SY, round function rounds to the nearest even number
You should make your own rounding function in Python

Prabhat_SY     2020-10-08 10:20:03

crydolphin666 can you please elaborate what you are trying to say, please. Beacuse the round function is working the same way as required.

crydolphin666     2020-10-08 10:49:24

round() function in Python rounds numbers in bank way, i.e. it rounds to the nearest even number in case of farctional part = 0.5 (n % 2 = 0)
For example:
round(2,5) = 2

But mathematical rounding should return 3 in this case.

For clarity round(3.5) will return 4.

Actually, there are a lot of another rounding methods in Python, such as math.floor(), math.ceil(), int(), // and all of them acting in different ways.

So, you are asked to implement right rounding function by yourself.

That function should round up if fractional part >= 0.5 and round down in the opposite case.

andrewdanine     2020-10-09 03:24:34

crydolphin666, why does this problem feel so unreasonably difficult? It's one of the beginning ones but it feels overly challenging, it really took me some time to think of what rounding really means.

Alexandr Milovantsev     2020-10-09 11:08:38

It is not a difficult problem in other programming languages. round() function in other languages (such as C++) acts just like it is needed for this task.

You should ask why python is using so counter intuitive choice for rounding. Obviously, we here don't know why python developers did so.

Alexandr Milovantsev     2020-10-09 11:12:19

BTW, I had also did this task in python, so I had hit hit the same problem and too was quite embarrassed.

Prabhat_SY     2020-10-09 11:42:55

Thanks for helping me out.

Please login and solve 5 problems to be able to post at forum