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

Open
ktkennychow opened this issue Dec 14, 2023 · 1 comment
Open

Chapter 1.1.7: Exercise 1.7 #958

ktkennychow opened this issue Dec 14, 2023 · 1 comment

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

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

2 participants