正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 7.3.5 And lets try this again. Here we have used the test case of x Test cases equal to pi. And we can see the trace of the computation. If we (sine 3.1415927) compare this to the mathematical equation we can see one problem. We really only want terms where n is odd, but clearly is s curent we are getting all terms for n. So we need to fix this. Most ikely this is because we are not changing our parameters rly is 15 current is 22.14066652713801 Waue:22.14066652713801 6001 sICP Slide 7.3.6 Fixing the increments So here is the correction We will need to increment our parameter by 2 each time, not by 1-an easy mistake to make nd to miss line) let ((next ( (expt x n)(fact n)))) (1E(ma11-nuf?m。xt current next))))) (aux x 1 0) 6001 SICP Slide 7.3.7 o let s try this again. Hmm. We have gotten better as we are only computing the odd terms for n, but we are still not right. If Test cases should be alternating signs on each term. Or said another way, nls,>3.1415921.o we look again at the mathematical equation, we can see that we(sine the successive approximations should go up, then down, then n up, then down, and so on. Note that we could have also spotted n⊥s7 ,39594659 this if we had chosen to display the value ofnext at each nis9 current is11.4587345286088 is 11 current is 11. 5408804261 n主s13 current i11.548250858285089 step nis15 current s11,548717161180408 So we need to keep track of some additional information, in this va1ue:11.548717161180408 case whether the term should be added or subtracted from the 8n003 60015c current guess6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 7.3.5 And let’s try this again. Here we have used the test case of x equal to pi. And we can see the trace of the computation. If we compare this to the mathematical equation we can see one problem. We really only want terms where n is odd, but clearly we are getting all terms for n. So we need to fix this. Most likely this is because we are not changing our parameters properly. Slide 7.3.6 So here is the correction. We will need to increment our parameter by 2 each time, not by 1 – an easy mistake to make, and to miss! Slide 7.3.7 So let’s try this again. Hmm. We have gotten better as we are only computing the odd terms for n, but we are still not right. If we look again at the mathematical equation, we can see that we should be alternating signs on each term. Or said another way, the successive approximations should go up, then down, then up, then down, and so on. Note that we could have also spotted this if we had chosen to display the value of next at each step. So we need to keep track of some additional information, in this case whether the term should be added or subtracted from the current guess
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有