Skip to content

Chapter 1.1.7: Exercise 1.7 #958

@ktkennychow

Description

@ktkennychow

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions