Rounding Float in Smoothing the Weather

Back to General discussions forum

Christopher Matthews     2014-10-15 09:27:55

Hello,

I am having trouble getting the correct answer for the problem Smoothing the Weather, in beginner problems. My language of choice is Python 3. I would like some advice as to how to correctly do the rounding. I am not looking for somebody else to solve the problem for me, just a push in the right direction.

Thanks! Christopher P. Matthews

Rodion (admin)     2014-10-15 09:35:24
User avatar

Hi! Thanks for your message!

I'll check your code (probably bit later but hope soon enough) and write back my ideas!

UPD: well, I see the problem:

It is not about the rounding! But you slightly changed the algorithm itself: when calculating average of three, you use the data[i - 1] which already was updated at previous step, instead of its original value!

Hope this helps!

Christopher Matthews     2014-10-15 10:39:51

I see! To fix the problem, I simply created a new list to hold the new values. Thank you very much!

Evil Spawn     2014-12-04 14:49:21

I have a similar problem. The language I am using is C and I use ideone as the online compiler. My answer only differs in rounding the values. Otherwise my logic and solution is correct. Could you direct me as to where I might me wrong?

Rodion (admin)     2014-12-04 17:08:33
User avatar

Hi! Thanks for your message!

Probably your case is somewhat simpler: try using double instead of float.

The problem with floats in the languages which support them is that they have quite few reliable digits - only 24 in binary form which corresponds to at most 7 in decimal. So usually after very few operations last digits are spoiled.

Doubles instead provide about 15 reliable digits in "mantissa".

Evil Spawn     2014-12-05 19:46:54

Thank you so much for your response! That was new for me! Unfortunately, it is not working either. :(

Maximization     2014-12-05 20:26:22

Did you make sure the first and last digit remain unchanged? Are you printing the values to a minimum of 1e-7 precision?

Evil Spawn     2014-12-05 20:38:34

Yes.Kept in mind both. This is the output I get for my test data:

32.6000000000 32.7333333333 35.1111111111 39.1370370370 42.0456790123 42.7485596708 44.1000000000

Evil Spawn     2014-12-05 20:38:58

I meant the sample data.

Rodion (admin)     2014-12-06 06:24:39
User avatar

Could you please submit your updated code so I can see it. Currently here is only those with floats, so I have no idea what can be wrong.

Ashish Padalkar     2014-12-10 17:38:50
User avatar

@Evil Spawn I too faced some problems in this question but i was able to overcome them. dont worry if you are getting 45.0000000000 instead of 45.0 , This will also get accepted

Use double and do take a look at the printf format specifiers and formatting options especially the width , flags and precision to print the answer in correct format.

Refer:

http://www.cplusplus.com/reference/cstdio/printf/

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