# [plt-scheme] Numerical precision

 From: Joe Marshall (jmarshall at alum.mit.edu) Date: Wed Mar 11 15:29:44 EDT 2009 Previous message: [plt-scheme] Numerical precision Next message: [plt-scheme] Numerical precision Messages sorted by: [date] [thread] [subject] [author]

```On Wed, Mar 11, 2009 at 9:31 AM, Jaime Vargas <jev at mac.com> wrote:

> #lang scheme
>
> (provide numerical-precision
>         (rename-out [round:N round]
>                     [*:N *]
>                     [/:N /]
>                     [+:N +]
>                     [-:N -]))
>
> (define numerical-precision (make-parameter 2))

This is a little odd.  Do you expect to dynamically change the precision?
If not, then you might want to make this a regular define rather than
a parameter.  If so, however, you'll have to be very careful when you
change the precision.

> (define (N)
>  (expt 10 (numerical-precision)))

This is a procedure that raises 10 to the precision power every time
it is called.  If the precision doesn't change, you don't need to do this
every time.

> (define (1/N)
>  (/ 1 (N)))

Likewise, this computes the reciprocal every time it is called.

> (define (round:N x)
>  (* (1/N) (round (* x (N)))))

And here you will notice that when you round, you will raise 10 to the
correct power
twice and take its reciprocal once.

> (define (*:N . nums)
>  (round:N (apply * nums)))
>
> (define (/:N . nums)
>  (round:N (apply / nums)))
>
> (define (+:N . nums)
>  (round:N (apply + nums)))
>
> (define (-:N . nums)
>  (round:N (apply - nums)))

You have defined something weird here.  Your arithmetic is no
longer associative where you might expect it to be.

--
~jrm

```

 Posted on the users mailing list. Previous message: [plt-scheme] Numerical precision Next message: [plt-scheme] Numerical precision Messages sorted by: [date] [thread] [subject] [author]