Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

I have solved problem 1 of Problem set 1: http://dpaste.com/hold/1370231/ I noticed an error of about 0.02 on my final amounts, and I think the error is from round just the printed values rather than the entire expressions. I also think it is unnecessary to re-declare the printed variables as 'float' after casting them as 'str' to print.

OpenStudy (e.mccormick):

There are also two types of rounding errors in computers when dealing with floats. One is the decimal rounding errors you normally think of. However, the computer does not use 10 base math. It uses binary, which can also have rounding errors. This dual set of rounding errors is why floating point math is more an approximation than something exact.

OpenStudy (anonymous):

the only comment i have is that it is not necessary to use float() in lines 12, 14, and 16. balance, annRate, and pmtRate are floats (lines 1,2,3) - any calculations performed with these values will result in a float. ``` >>> >>> type(0.10 / 12) <type 'float'> >>> type(25 - 1.2) <type 'float'> >>> ``` http://docs.python.org/2.7/reference/expressions.html#arithmetic-conversions

OpenStudy (e.mccormick):

Very true. minPmt = pmtRate * balance makes minPmt a float if either of pmtRate or balance is a float,and in this case both are. Many languages have automatic casting from lower to higher bit sized types. So a char will become an int automatically if needed, and so on, but not the reverse. This is called promotion. Static casting, where you do it by hand, is only needed always in languages where they are not only strongly typed but also have no dynamic casting at all. In the rest of the languages, it is only needed when dynamic won't work. At the same time, it is the most dangerous because you can static cast something to a type where it will not work and end up with an error or loss.

OpenStudy (anonymous):

Ok. thank you two for clearing that up. I included be safe (redundant) so that the variable did not get stuck as a 'str' type. I also played around and found that the rounding difference I had was due to the manner in which the round() function was used in my code vs. what must have been used in the example code. I put parentheses in different places to round less numbers. Is there an accepted best practice for which and how many numbers to round in an expression? or does it make any difference?

OpenStudy (e.mccormick):

It all comes down to accuracy. The later the rounding is, the more the accuracy.

OpenStudy (anonymous):

don't round intermediate results round the final result to the number of digits in the least precise number in all the calcs. or round to a precision that makes sense http://en.wikipedia.org/wiki/Accuracy_and_precision http://en.wikipedia.org/wiki/Significant_figures

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!