Back to General discussions forum
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
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!
I see! To fix the problem, I simply created a new list to hold the new values. Thank you very much!
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?
Hi! Thanks for your message!
Probably your case is somewhat simpler: try using double
instead of float
.
The problem with float
s
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".
Thank you so much for your response! That was new for me! Unfortunately, it is not working either. :(
Did you make sure the first and last digit remain unchanged? Are you printing the values to a minimum of 1e-7 precision?
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
I meant the sample data.
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.
@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: