SICP - exercise 1.45 - are these solutions equivalent? [on hold]
QUESTION:
I'm going through the exercise in SICP and am wondering if someone can explain the difference between these 2 seemingly equivalent functions that are giving different results! Is this because of rounding?? I'm thinking the order of functions shouldn't matter here but somehow it does? Can someone explain what's going on here and why it's different?
Details:
Exercise 1.45: ..saw that finding a fixed point of y => x/y does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-damped y => x/y^2. Unfortunately,
the process does not work for fourth roots—a single average damp is not enough to make a fixed-point search for y => x/y^3 converge. On the other hand, if we
average damp twice (i.e., use the average damp of the average damp of y => x/y^3) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute nth roots as a fixedpoint search based upon repeated average damping of y =>x/y^(n-1). Use this to implement a simple procedure for computing the roots using fixed-point, average-damp, and the
repeated procedure of Exercise 1.43. Assume that any arithmetic operations you need are available as primitives.
My answer (note order of repeat and average-damping):
(define (nth-root-me x n num-repetitions)
(fixed-point (repeat (average-damping (lambda (y) (/ x (expt y (- n 1)))))
num-repetitions)
1.0))
I see an alternate web solution where repeat is called direcly on average damp and then that function is called with the argument
(define (nth-root-web-solution x n num-repetitions)
(fixed-point
((repeat average-damping num-repetition)
(lambda (y) (/ x (expt y (- n 1)))))
1.0))
Now calling both of these, there seems to be a difference in the answers and I can't understand why! My understanding is the order of the functions shouldn't affect the output (they're associative right?), but clearly it is!
(nth-root-me 10000 4 2)
10.050110705350287
(nth-root-web-solution 10000 4 2)
10.0
I did more tests and it's always like this, my answer is close, but the other answer is almost always closer!
Can someone explain what's going on? Why aren't these equivalent?
functional-programming lisp scheme sicp
New contributor
put on hold as off-topic by Mast, Jamal♦ Dec 25 at 8:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Mast, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
QUESTION:
I'm going through the exercise in SICP and am wondering if someone can explain the difference between these 2 seemingly equivalent functions that are giving different results! Is this because of rounding?? I'm thinking the order of functions shouldn't matter here but somehow it does? Can someone explain what's going on here and why it's different?
Details:
Exercise 1.45: ..saw that finding a fixed point of y => x/y does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-damped y => x/y^2. Unfortunately,
the process does not work for fourth roots—a single average damp is not enough to make a fixed-point search for y => x/y^3 converge. On the other hand, if we
average damp twice (i.e., use the average damp of the average damp of y => x/y^3) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute nth roots as a fixedpoint search based upon repeated average damping of y =>x/y^(n-1). Use this to implement a simple procedure for computing the roots using fixed-point, average-damp, and the
repeated procedure of Exercise 1.43. Assume that any arithmetic operations you need are available as primitives.
My answer (note order of repeat and average-damping):
(define (nth-root-me x n num-repetitions)
(fixed-point (repeat (average-damping (lambda (y) (/ x (expt y (- n 1)))))
num-repetitions)
1.0))
I see an alternate web solution where repeat is called direcly on average damp and then that function is called with the argument
(define (nth-root-web-solution x n num-repetitions)
(fixed-point
((repeat average-damping num-repetition)
(lambda (y) (/ x (expt y (- n 1)))))
1.0))
Now calling both of these, there seems to be a difference in the answers and I can't understand why! My understanding is the order of the functions shouldn't affect the output (they're associative right?), but clearly it is!
(nth-root-me 10000 4 2)
10.050110705350287
(nth-root-web-solution 10000 4 2)
10.0
I did more tests and it's always like this, my answer is close, but the other answer is almost always closer!
Can someone explain what's going on? Why aren't these equivalent?
functional-programming lisp scheme sicp
New contributor
put on hold as off-topic by Mast, Jamal♦ Dec 25 at 8:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Mast, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
I believe this is better suited for StackOverflow.
– Faraz
Dec 25 at 7:43
Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is. If you don't understand how/why your code works, a review is not what you want.
– Mast
Dec 25 at 7:53
add a comment |
QUESTION:
I'm going through the exercise in SICP and am wondering if someone can explain the difference between these 2 seemingly equivalent functions that are giving different results! Is this because of rounding?? I'm thinking the order of functions shouldn't matter here but somehow it does? Can someone explain what's going on here and why it's different?
Details:
Exercise 1.45: ..saw that finding a fixed point of y => x/y does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-damped y => x/y^2. Unfortunately,
the process does not work for fourth roots—a single average damp is not enough to make a fixed-point search for y => x/y^3 converge. On the other hand, if we
average damp twice (i.e., use the average damp of the average damp of y => x/y^3) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute nth roots as a fixedpoint search based upon repeated average damping of y =>x/y^(n-1). Use this to implement a simple procedure for computing the roots using fixed-point, average-damp, and the
repeated procedure of Exercise 1.43. Assume that any arithmetic operations you need are available as primitives.
My answer (note order of repeat and average-damping):
(define (nth-root-me x n num-repetitions)
(fixed-point (repeat (average-damping (lambda (y) (/ x (expt y (- n 1)))))
num-repetitions)
1.0))
I see an alternate web solution where repeat is called direcly on average damp and then that function is called with the argument
(define (nth-root-web-solution x n num-repetitions)
(fixed-point
((repeat average-damping num-repetition)
(lambda (y) (/ x (expt y (- n 1)))))
1.0))
Now calling both of these, there seems to be a difference in the answers and I can't understand why! My understanding is the order of the functions shouldn't affect the output (they're associative right?), but clearly it is!
(nth-root-me 10000 4 2)
10.050110705350287
(nth-root-web-solution 10000 4 2)
10.0
I did more tests and it's always like this, my answer is close, but the other answer is almost always closer!
Can someone explain what's going on? Why aren't these equivalent?
functional-programming lisp scheme sicp
New contributor
QUESTION:
I'm going through the exercise in SICP and am wondering if someone can explain the difference between these 2 seemingly equivalent functions that are giving different results! Is this because of rounding?? I'm thinking the order of functions shouldn't matter here but somehow it does? Can someone explain what's going on here and why it's different?
Details:
Exercise 1.45: ..saw that finding a fixed point of y => x/y does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-damped y => x/y^2. Unfortunately,
the process does not work for fourth roots—a single average damp is not enough to make a fixed-point search for y => x/y^3 converge. On the other hand, if we
average damp twice (i.e., use the average damp of the average damp of y => x/y^3) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute nth roots as a fixedpoint search based upon repeated average damping of y =>x/y^(n-1). Use this to implement a simple procedure for computing the roots using fixed-point, average-damp, and the
repeated procedure of Exercise 1.43. Assume that any arithmetic operations you need are available as primitives.
My answer (note order of repeat and average-damping):
(define (nth-root-me x n num-repetitions)
(fixed-point (repeat (average-damping (lambda (y) (/ x (expt y (- n 1)))))
num-repetitions)
1.0))
I see an alternate web solution where repeat is called direcly on average damp and then that function is called with the argument
(define (nth-root-web-solution x n num-repetitions)
(fixed-point
((repeat average-damping num-repetition)
(lambda (y) (/ x (expt y (- n 1)))))
1.0))
Now calling both of these, there seems to be a difference in the answers and I can't understand why! My understanding is the order of the functions shouldn't affect the output (they're associative right?), but clearly it is!
(nth-root-me 10000 4 2)
10.050110705350287
(nth-root-web-solution 10000 4 2)
10.0
I did more tests and it's always like this, my answer is close, but the other answer is almost always closer!
Can someone explain what's going on? Why aren't these equivalent?
functional-programming lisp scheme sicp
functional-programming lisp scheme sicp
New contributor
New contributor
New contributor
asked Dec 25 at 7:22
ajivani
99
99
New contributor
New contributor
put on hold as off-topic by Mast, Jamal♦ Dec 25 at 8:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Mast, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Mast, Jamal♦ Dec 25 at 8:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Mast, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
I believe this is better suited for StackOverflow.
– Faraz
Dec 25 at 7:43
Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is. If you don't understand how/why your code works, a review is not what you want.
– Mast
Dec 25 at 7:53
add a comment |
I believe this is better suited for StackOverflow.
– Faraz
Dec 25 at 7:43
Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is. If you don't understand how/why your code works, a review is not what you want.
– Mast
Dec 25 at 7:53
I believe this is better suited for StackOverflow.
– Faraz
Dec 25 at 7:43
I believe this is better suited for StackOverflow.
– Faraz
Dec 25 at 7:43
Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is. If you don't understand how/why your code works, a review is not what you want.
– Mast
Dec 25 at 7:53
Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is. If you don't understand how/why your code works, a review is not what you want.
– Mast
Dec 25 at 7:53
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
I believe this is better suited for StackOverflow.
– Faraz
Dec 25 at 7:43
Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is. If you don't understand how/why your code works, a review is not what you want.
– Mast
Dec 25 at 7:53