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

Code example missing statement p 50 #224

Open
dgrunberg opened this issue Feb 7, 2022 · 0 comments
Open

Code example missing statement p 50 #224

dgrunberg opened this issue Feb 7, 2022 · 0 comments

Comments

@dgrunberg
Copy link

The 2 code examples for the iterative square root algorithm cause an 'UnboundLocalError' and are missing an assignment; even with local vars initialized, the loop will never terminate as x is never updated.

I agree the use of math.inf is messy but prevents an extra check in the while loop like while x is None or abs()>.01

These are on line approx 644 of chapter1.md (p50 of the un-numbered chapter of the pdf book), and then again 3 paragraphs later.

import math
def sqrt(a: float) -> float:
    x = math.inf                         #Need to avoid UboundLocalError, x is previous value, x_n is current value
    x_n = a / 2 # initial guess
    while abs(x_n - x) > 0.01:
        x = x_n                             #<== missing                
        x_n = (x + (a / x)) / 2
    return x_n
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

1 participant