Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 1.7: You should get an answer of 966279.5999999957 not 966,279.6 #29

Closed
Jock2018 opened this issue Jun 1, 2020 · 3 comments
Closed

Comments

@Jock2018
Copy link

Jock2018 commented Jun 1, 2020

# mortgage.py

principal = 500000.0
rate = 0.05
payment = 2684.11
total_paid = 0.0

while principal > 0:
    principal = principal * (1+rate/12) - payment
    total_paid = total_paid + payment

print('Total paid', total_paid)

Enter this program and run it. I get an answer of 966279.5999999957. Not 966,279.6.

I can get an anwer of 966,279.6 by following:

# Exercise 1.7


principal = 500000.0
rate = 0.05
payment = 2684.11
total_paid = 0.0

while principal > 0:
    principal = principal * (1+rate/12) - payment
    total_paid = total_paid + payment

print('Total paid', round(total_paid, 1))
@dabeaz
Copy link
Contributor

dabeaz commented Jun 1, 2020

The question is worded as intended. The fact that floating point numbers don't give exact results is mentioned a few places. However, the fact that you get a slightly "off" answer here would be a discussion point in a live-taught course. One could also point people at the decimal built-in module.

@atsalolikhin-spokeo
Copy link

Hello! I am learning Python. I appreciate that you shared your course materials, thank you!

What does the decimal built-in module have to do with this mortgage exercise, please? Is that covered later in the course? Is there a way to get the .6 answer using the decimal module?

@dabeaz
Copy link
Contributor

dabeaz commented Jun 16, 2020

Floating point point numbers are not exact. If this matters to you, consider the use of the decimal module (you'd need to look at the library documentation for more information).

@dabeaz dabeaz closed this as completed Jun 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants