sicp icon indicating copy to clipboard operation
sicp copied to clipboard

Chapter 1.1.7: Exercise 1.7

Open ktkennychow opened this issue 1 year ago • 1 comments

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.

ktkennychow avatar Dec 14 '23 14:12 ktkennychow

OK. Thanks. Can you make an issue in the repo on this please.

On Thu, 14 Dec 2023, 21:47 Kenny Chow, @.***> wrote:

Simply adjusting/scaleing 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; }

— Reply to this email directly, view it on GitHub https://github.com/source-academy/sicp/issues/958, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHGSDYDS6ZLOVVD32GQF73DYJMGQRAVCNFSM6AAAAABAU7AFPCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGA2DCOBVGQ4TCMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

martin-henz avatar Dec 15 '23 00:12 martin-henz