[racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-04 Thread Ben Duan
I've asked the question on Stack Overflow[1]. Óscar López gave me an answer. But I still don't understand it. So I re-post it here: Following is my code for SICP exercise 1.29

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-04 Thread Daniel Prager
Interestingly, your code gives the expected result with exact arithmetic: > (simpson-integral cube 0 1 100) 1/4 and less of an error for a smaller number of steps: > (simpson-integral cube 0 1.0 10) 0.24994 Suggestion: Modify the code to print out the intermediate steps in the calcu

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-04 Thread Jens Axel Søgaard
2013/11/4 Ben Duan : > I've asked the question on Stack Overflow [1]. Óscar López gave me an > answer. But I still don't understand it. So I re-post it here: > > Following is my code for SICP exercise 1.29 [2]. The exercise asks us to > implement Simpson's Rule using higher order procedure `sum`. I

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-05 Thread Ben Duan
Thanks, Jens and Daniel. There's no problem with Simpson's Rule. I got another way to implement Simpson's Rule from Eli Bendersky. And re-wrote it in Racket: (define (simpson-integral2 f a b n) (define h (/ (- b a) n)) (de

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-05 Thread Jens Axel Søgaard
I think I got it now. In your solution the x-values are computed as follows: h = (b-a)/n x1 = a+1 x3 = x1 +2*h x5 = x3 +2*h ... This means rounding errors slowly accumulate. It happens when (b-a)/n is not representable as floating point. If we instead compute xi as a+ (i*(b-a))/n you wi

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-06 Thread Ben Duan
Thank you, Jens. I didn't know that the inexactness of floating point numbers could make such a big difference. On Tue, Nov 5, 2013 at 10:01 PM, Jens Axel Søgaard wrote: > I think I got it now. > > In your solution the x-values are computed as follows: > h = (b-a)/n > x1 = a+1 > x3 = x1 +

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-06 Thread Matthias Felleisen
On Nov 6, 2013, at 7:13 AM, Ben Duan wrote: > Thank you, Jens. I didn't know that the inexactness of floating point numbers > could make such a big difference. >From HtDP/1e: (define JANUS (list #i31 #i2e+34 #i-1.2345678901235e+80 #i2749 #i-2939234

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-06 Thread Neil Toronto
On 11/06/2013 09:24 AM, Matthias Felleisen wrote: On Nov 6, 2013, at 7:13 AM, Ben Duan wrote: Thank you, Jens. I didn't know that the inexactness of floating point numbers could make such a big difference. From HtDP/1e: (define JANUS (list #i31 #i2e+34 #i-1.23456789

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-07 Thread Todd O'Bryan
I just found a lovely Java expression to emphasize the inexactness of doubles to my AP students. The problem--which I think is from HtDP/1e--is to find the value of a bag of coins given the number of pennies, nickels, dimes, and quarters. In BlueJ's code pad (or similar in DrJava, jGrasp, etc.) >

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-07 Thread Stephan Houben
The competition does it better, see Python's fsum: import math janus = [31.0, 2e+34, -1.2345678901235e+80, 2749.0, -2939234.0, -2e+33, 3.2e+270, 17.0, -2.4e+270, 4.2344294738446e+170, 1.0, -8e+269, 0.0, 99.0] print(math.fsum(janus)) # 4.2344294738446e+170 The fsum algorithm is interest

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-07 Thread Ben Duan
Thank you all. Your examples are really surprising to me. On Thu, Nov 7, 2013 at 6:20 PM, Todd O'Bryan wrote: > I just found a lovely Java expression to emphasize the inexactness of > doubles to my AP students. The problem--which I think is from > HtDP/1e--is to find the value of a bag of coins

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-07 Thread Laurent
In racket/base, you can also simply use bignums instead: > (define janus '(31.0 2e+34 -1.2345678901235e+80 2749.0 -2939234.0 -2e+33 3.2e+270 17.0 -2.4e+270 4.2344294738446e+170 1.0 -8e+269 0.0 99.0)) > (exact->inexact (apply + (map inexact->exact janus))) 4.2344294738446e+170 No n

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-07 Thread Konrad Hinsen
Todd O'Bryan writes: > I just found a lovely Java expression to emphasize the inexactness of > doubles to my AP students. The problem--which I think is from > HtDP/1e--is to find the value of a bag of coins given the number of > pennies, nickels, dimes, and quarters. In BlueJ's code pad (or si

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-07 Thread Neil Toronto
Huh. It looks like they're also using Shewchuk's O(n*log(n)) average-case distillation algorithm. Like Racket's math library does. :D Neil ⊥ On 11/07/2013 04:24 AM, Stephan Houben wrote: The competition does it better, see Python's fsum: import math janus = [31.0, 2e+34, -1.2345678901235e+80

Re: [racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

2013-11-07 Thread Stephan Houben
Umm, ok. I retract my comment. I had not inferred that from the.docs. Stephan Stephan Houben Op 7 nov. 2013 17:56 schreef "Neil Toronto" het volgende: > Huh. It looks like they're also using Shewchuk's O(n*log(n)) average-case > distillation algorithm. Like Racket's math library does. :D > > Ne