[plt-scheme] Test a numbers representation to see if it's complex?

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Mon Jun 4 17:53:21 EDT 2007

 > Ryan Newton wrote:
 >> Yes, this is not normally a nice thing to do.  But I have a need to
 >> distinguish 3.0 and 3.0+0.0i.  As far as I can tell, while PLT
 > *could* turn 3.0+0.0i into 3.0, but for whatever reason the complex
 >> representation seems closed under PLT's arithmetic, which makes what 
 >> want to do somewhat sane.

The problem is that 3.0 and 3.0+0.0i is the same number.
The real part and the imaginary part shares the exactness.

So if you want to distinguish them, you need to defined
your own implementation of imaginary numbers.

Chongkai Zhu wrote:
> How about this `0-imag-part?'?
> (define (exact-zero? x)
>  (and (exact? x)
>       (zero? x)))
> (define (0-imag-part? z)
>  (exact-zero? (imag-part z)))

 > (0-imag-part? (- 3.0+0.5i 0+0.5i))

 > (0-imag-part? #i3.0)

 > (0-imag-part? #i3.5+0.0i)

 > (0-imag-part? 3.5+0.0i)

 > (0-imag-part? #i3.5+0.0i)

 > (0-imag-part? #e3.5+0.0i)

/Jens Axel

Posted on the users mailing list.