marcomaggi / vicare

A native compiler for Scheme compliant with R6RS
http://marcomaggi.github.com/vicare.html
Other
200 stars 34 forks source link

wrong handling of sum between exact integers and complex numbers #24

Closed marcomaggi closed 11 years ago

marcomaggi commented 13 years ago

The two following operations must return the same result:

(+ 1 -1.1-0.0i)       => -0.10000000000000009+0.0i
(- 1 +1.1+0.0i)       => -0.10000000000000009+0.0i
                                             ^
                                             !

but instead they returned:

(+ 1 -1.1-0.0i)       => -0.10000000000000009-0.0i
(- 1 +1.1+0.0i)       => -0.10000000000000009+0.0i
                                             ^
                                             !

this is wrong because R6RS specifies the following for the "-" function:

 Implementations that  distinguish -0.0 should  adopt behavior
 consistent with the following examples:

      (+  0.0 -0.0)           =>  0.0
      (+ -0.0  0.0)           =>  0.0
      (+  0.0  0.0)           =>  0.0
      (+ -0.0 -0.0)           => -0.0

so the "+" operation must be implemented as:

 (+ 1 -1.1-0.0i)
 == (make-rectangular (+ 1 -1.1) (+   0 -0.0))
 == (make-rectangular (+ 1 -1.1) (+ 0.0 -0.0))
 == (make-rectangular (+ 1 -1.1) (+ 0.0 -0.0))
 == (make-rectangular -0.100...  0.0)

notice that the step:

 (+ 0 -0.0) == (+ 0.0 -0.0)

is not compliant with R6RS but we accept it because it makes other formulas work correctly, for example the following two versions of ATAN which should return the same result:

 (define (atan-one x)
   (* +0.5i
      (- (log (+ 1 (* -1i x)))
         (log (+ 1 (* +1i x))))))

 (define (atan-two x)
   (* +0.5i
      (- (log (- 1 (* +1i x)))
         (log (+ 1 (* +1i x))))))
marcomaggi commented 11 years ago

Should be fixed in the head of the devel branch.