[plt-scheme] Numerical precision
From: Jaime Vargas (jev at mac.com)
Date: Wed Mar 11 16:46:01 EDT 2009 |
|
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