A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing
 2 years ago
PS1, rounding problem. (with code)
I had a rounding problem for the values in PS1, so I searched and found about the DECIMAL module and it's quantize function.
I then rewrote my code to get the same results as in the solution, but there still is a difference. Is DECIMAL the way to go?
my code is here:
http://pastebin.com/ghg0vrDr
thanks
 2 years ago
PS1, rounding problem. (with code) I had a rounding problem for the values in PS1, so I searched and found about the DECIMAL module and it's quantize function. I then rewrote my code to get the same results as in the solution, but there still is a difference. Is DECIMAL the way to go? my code is here: http://pastebin.com/ghg0vrDr thanks

This Question is Closed

snark
 2 years ago
Best ResponseYou've already chosen the best response.0rounding error wasn't likely the problem  there isn't enough computation for even 3decimal accuracy to blow it off. It actually seems fine without the decimal import: balance: 100000.00 credit rate: .15 min pay .12 after 12 months, the balance due = 33326.866 which seems reasonable, and looked fine each step of the way. What data have you run it on? I've worked the problem set but can't locate it now.

TuringTest
 2 years ago
Best ResponseYou've already chosen the best response.0you could also consider using the "round" function: round(3.14159, 2) #rounds to 2 decimal places >>>3.14

petrissimo
 2 years ago
Best ResponseYou've already chosen the best response.0I had tried the 'round' function and it also gave a difference, this is why I searched for another option. Decimal seemed to address this exact problem. Here is a link to PS1, where you can compare the values. http://ocw.mit.edu/courses/electricalengineeringandcomputerscience/600scintroductiontocomputerscienceandprogrammingspring2011/unit1/lecture4machineinterpretationofaprogram/MIT6_00SCS11_ps1.pdf Here is a list of where it goes wrong, the differences are the same either with round or with Decimal. (myValue vs officialValue) : month 6  principal paid : 15.74 vs 15.73 month 7  principal paid : 15.68 vs 15.69 (also here, cents are missing) month 10  remain. balance : 4642.38 vs 4642.37 month 11  principal paid : 15.47 vs 15.48 month 11  remain. balance : 4626.9_ vs 4626.89 month 12 principal paid : 15.42 vs 15.43 month 12  remain. balance : 4611.48 vs 4611.46 Results  remain. balance : 4611.48 vs 4611.46 Results  total amount paid : 1131.11 vs 1131.12 the code for the 'round' option can be found here : http://pastebin.com/WbcHU0mZ the code for the 'Decimal' option can be found here : http://pastebin.com/rMUJWe4Q Am I being too picky? These differences would add up on the long term, so something is wrong, but what? thanks

petrissimo
 2 years ago
Best ResponseYou've already chosen the best response.0Here is a link to DECIMAL for those interested http://docs.python.org/2/library/decimal.html

snark
 2 years ago
Best ResponseYou've already chosen the best response.0oddly enough, i'd trust the accuracy of your result over the one given  float is accurate to (it looks like) 10 decimals  x.xxxxxxxxxx a few multiplies and divides won't effect the second decimal place. their answer for month 2 is 4950.13  you get 4950.125. yours is actually more accurate  you can check it by hand. (thanks for linking to problem set  )

petrissimo
 2 years ago
Best ResponseYou've already chosen the best response.0I forgot to say, I use the initial values of the problem Outstanding balance on your credit card: 4800 Annual credit card interest rate as a decimal: 0.2 Minimum monthly payment rate as a decimal: 0.02 This brings Snark's values in question 'their answer for month 2 is 4950.13  you get 4950.125. I tried with $5000 0,2%0,02% and I get 4950.17 for month 3... (I guess these are the values snark used) is it possible that the result could vary depending on the computer used??? thanks

TuringTest
 2 years ago
Best ResponseYou've already chosen the best response.0I actually wouldn't even worry about the discrepancy of a few cents due to the lack of exactitude of floating point numbers.

snark
 2 years ago
Best ResponseYou've already chosen the best response.0i used 5000.00, .18, and .02 : maybe i misscopied the problem? i think what they are doing is saying 11.041 is over 11.04 and so, thinking like a banker, they will call it 11.05

petrissimo
 2 years ago
Best ResponseYou've already chosen the best response.0Ok, but then, what data type would be used to get truly accurate values? Also btw, I tried comparing my code with the solution they post on the page here :http://ocw.mit.edu/courses/electricalengineeringandcomputerscience/600scintroductiontocomputerscienceandprogrammingspring2011/unit1/lecture4machineinterpretationofaprogram/ but all I get is a prompt window (black DOS box) to run the program. How can I see the code from there? thanks again

snark
 2 years ago
Best ResponseYou've already chosen the best response.0the floats are very good  10 decimals  you are certainly getting it right to at least 8 decimals (computers are good).

TuringTest
 2 years ago
Best ResponseYou've already chosen the best response.0Try rightclick>edit with IDLE to open the file

TuringTest
 2 years ago
Best ResponseYou've already chosen the best response.0And yeah, floating point numbers are good up to 8 digits, but this calculation involves many floating point numbers in its course, which can lead to an error in the 2nd decimal place.

snark
 2 years ago
Best ResponseYou've already chosen the best response.0very few, actually. what, 1 multiply or 2 per step? it really won't effect the second decimal.

petrissimo
 2 years ago
Best ResponseYou've already chosen the best response.0'Try rightclick>edit with IDLE to open the file' tried this on the page tried this on the download link tried this in the prompt window tried this on the downloaded file tried this in a new page in IDLE tried this in the program in IDLE I'm never offered the 'edit with IDLE' option... I'm a little bit of a beginner at this, it seems obvious, but...

petrissimo
 2 years ago
Best ResponseYou've already chosen the best response.0Sorry, got it. my downloaded files were not extracted...

petrissimo
 2 years ago
Best ResponseYou've already chosen the best response.0thanks for all the answers
Ask your own question
Ask a QuestionFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.