[plt-scheme] Numerical precision

From: Jos Koot (jos.koot at telefonica.net)
Date: Wed Mar 11 18:39:01 EDT 2009

(define numerical-precision
 (make-parameter #f
  (lambda (p)
  (set! N (expt 10 p))
  (set! 1/N (/ N))
  p)))

(define N #f)
(define 1/N #f)
(numerical-precision 2)


Jos

----- Original Message ----- 
From: "Jaime Vargas" <jev at mac.com>
To: "PLT-list" <plt-scheme at list.cs.brown.edu>; "Joe Marshall" 
<jmarshall at alum.mit.edu>
Sent: Wednesday, March 11, 2009 9:46 PM
Subject: Re: [plt-scheme] Numerical precision


> On Mar 11, 2009, at 4:23 PM, Jaime Vargas wrote:
>
>> On Mar 11, 2009, at 3:29 PM, Joe Marshall wrote:
>>
>>> You have defined something weird here.  Your arithmetic is no
>>> longer associative where you might expect it to be.
>>
>> How so, I don't see the problem. Again, some naive testing.
>>
>> #lang scheme
>>
>> (require "accountants-math.ss")
>>
>> (equal? (* 1/2 (+ 2/3 1/3))
>>          (+ (* 1/2 1/3) (* 1/2 2/3)))
>> => #t
>>
>> (equal? (/ (+ 2/3 1/3) 1/2)
>>        (+ (/ 2/3 1/2) (/ 1/3 1/2)))
>> => #t
>>
>> (equal? (* 1/2 (- 2/3 1/3))
>>        (- (* 1/2 2/3) (* 1/2 1/3)))
>> => #t
>>
>>
>> (equal? (/ (- 2/3 1/3) 1/2)
>>        (- (/ 2/3 1/2) (/ 1/3 1/2)))
>> => #t
>
> Correcting myself, the above only works for those values. Upon
> further testing. I see your point, the arithmetic is not associative.
> Can you point me to a better technique?
>
> #lang scheme
>
> (require "accountants-math.ss")
>
> (define-values (a b c) (values 1/7 1/3 2/5))
>
> (equal? (* a (+ b c))
>           (+ (* a b) (* a c)))
> => #f
>
> (equal? (/ (+ b c) a)
>         (+ (/ b a) (/ c a)))
> => #f
>
> (equal? (* a (- b c))
>         (- (* a b) (* a c)))
> => #t
>
> (equal? (/ (- b c) a)
>         (- (/ b a) (/ c a)))
> => #f
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme 



Posted on the users mailing list.