[racket] HTDP 2nd edition exercise 4.2.3 - floating point issues

From: Bo Gus (forumangus at gmail.com)
Date: Tue Oct 22 10:30:17 EDT 2013

(=~ is interesting.  Thanks.


On 22 October 2013 13:53, Matthias Felleisen <matthias at ccs.neu.edu> wrote:

>
> ;; equation3 : number  ->  boolean
> ;; to determine whether n is a solution for 2n^2  =  102
> (define (equation3 n)
>   (=~ (* 2 n n) 102 .001))
>
>
> -- Matthias
>
>
>
> On Oct 22, 2013, at 8:40 AM, Bo Gus <forumangus at gmail.com> wrote:
>
> > equation 2 is 2n^2 = 102 so I implement like this:
> >
> > ;; equation3 : number  ->  boolean
> > ;; to determine whether n is a solution for 2n^2  =  102
> > (define (equation3 n)
> >   (= (* 2 n n) 102))
> >
> > And my answer is the same as per the online answer.  so great.
> >
> > But how can I check a valid answer.
> >
> > Eg if I do:
> >
> > (equation3 (sqrt 51))
> > >false
> >
> > same using - square root 51.
> >
> > How can I fix this?  Is the only way to do a range check?  Eg have some
> sort of tolerance - eg between 0.01 above and below answer?  Any other
> ideas?
> >
> > ____________________
> >  Racket Users list:
> >  http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131022/6b0f8bb3/attachment.html>

Posted on the users mailing list.