正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 7.3.1 Let's use an example of a debugging session to highlight these ideas. This will be primarily to fix a structural error, but A debugging example will see how the other tools come into play as we do this uppose we want to compute an approximation to the sine We want to compute sing the mathematical function. Here is a mathematical approximation that will give approximation us a pretty good solution. So let's try coding this up 6001 sICP Slide 7.3.2 Initial code example So here is a first attempt at some code to do this. We will define (sine x) assume that fact and small-enuf? already (define (aux x n current) exist. The basic idea behind this procedure is quite similar to (let ((next (/(expt x n)(fact n)))) what we did for square roots. We start with a guess. We then :: compute next term if (small-enuf? next) ; if small see how to improve the guess, in this case by computing the current::just return current guess next term in the approximation, which we would like to add (aux x (+ n 1) (+ current next) i otherwise, create new guess If this improvement is small enough, we are done and can ))) return the desired value. If not, we repeat the process with a (aux x 1 0)) better guess, by adding in the improvement to the current 6001 SICP Slide 7.3.3 Now, let's try it out on some test cases. One nice test case is st cases the base case, of x equal to 0. That clearly works. Another ice test case is when x is equal to pi, where we know the result (sine o should also be close to 0. Oops! That didn t work. Nor does the code work for x equal to pi half. Both of these latter cases sine3.1415927 give results that are much too large Vaue:22.140666527138016 Vaue:3,8104481565660486 8n003 60015e Slide 7.3.4 Chasing down the error Okay, we need to figure out where our conceptual error lies define (sine x) ts try to isolate this by tracing through the computation. In (newline) particular, we will add some display expressions that will show (display "n is " us the state of the computation each time through the recursion (display n) dsp1ay" current is”) let ((next (/(expt x n)(fact n)))) iE《sma11-enuf?next) aux * (+n 1)(+ current next)))) (aux x 1 0))6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 7.3.1 Let’s use an example of a debugging session to highlight these ideas. This will be primarily to fix a structural error, but we will see how the other tools come into play as we do this. Suppose we want to compute an approximation to the sine function. Here is a mathematical approximation that will give us a pretty good solution. So let’s try coding this up. Slide 7.3.2 So here is a first attempt at some code to do this. We will assume that fact and small-enuf? already exist. The basic idea behind this procedure is quite similar to what we did for square roots. We start with a guess. We then see how to improve the guess, in this case by computing the next term in the approximation, which we would like to add in. If this improvement is small enough, we are done and can return the desired value. If not, we repeat the process with a better guess, by adding in the improvement to the current guess. Slide 7.3.3 Now, let’s try it out on some test cases. One nice test case is the base case, of x equal to 0. That clearly works. Another nice test case is when x is equal to pi, where we know the result should also be close to 0. Oops! That didn’t work. Nor does the code work for x equal to pi half. Both of these latter cases give results that are much too large. Slide 7.3.4 Okay, we need to figure out where our conceptual error lies. Let’s try to isolate this by tracing through the computation. In particular, we will add some display expressions that will show us the state of the computation each time through the recursion
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有