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

Chapter 1.1.7: Exercise 1.7 #958

Closed
ktkennychow opened this issue Dec 14, 2023 · 1 comment · Fixed by #1040
Closed

Chapter 1.1.7: Exercise 1.7 #958

ktkennychow opened this issue Dec 14, 2023 · 1 comment · Fixed by #1040
Labels
Exercise solution Solutions to textbook exercises

Comments

@ktkennychow
Copy link

ktkennychow commented Dec 14, 2023

Simply adjusting/scaling the tolerance with x will do, instead of complicating the logic with the current solution.

Consider:

function is_good_enough(guess, x) {
    return abs(square(guess) - x) < x * 0.001;
}

Instead of:

const error_threshold = 0.01;
function is_good_enough(guess, x) {
    return relative_error(guess, improve(guess, x))
           < error_threshold;
}
function relative_error(estimate, reference) {
    return abs(estimate - reference) / reference;
}

Defining the error_threshold outside is a good practice.

So maybe also consider:

const error_threshold = 0.001;

function is_good_enough(guess, x) {
    return abs(square(guess) - x) < x * error_threshold;
}

Relatively minor: keeping the original threshold = 0.001 instead of changing it to 0.01.

@martin-henz
Copy link
Member

martin-henz commented Dec 15, 2023 via email

martin-henz added a commit that referenced this issue Jul 1, 2024
@martin-henz martin-henz added the Exercise solution Solutions to textbook exercises label Jul 1, 2024
@RichDom2185 RichDom2185 linked a pull request Jul 10, 2024 that will close this issue
RichDom2185 pushed a commit that referenced this issue Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Exercise solution Solutions to textbook exercises
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants