[R] Comparing complex numbers

From: David Stoffer <dsstoffer_at_gmail.com>
Date: Fri, 11 Jul 2008 08:51:28 -0700 (PDT)

Is there an easy way to compare complex numbers?

Here is a small example:

> (z1=polyroot(c(1,-.4,-.45)))
[1] 1.111111-0i -2.000000+0i

> (z2=polyroot(c(1,1,.25)))
[1] -2+0i -2+0i

>   x=0
>   if(any(identical(z1,z2))) x=99
>    x

[1] 0

# real and imaginary parts:

> Re(z1); Im(z1)
[1] 1.111111 -2.000000
[1] -8.4968e-21 8.4968e-21

> Re(z2); Im(z2)
[1] -2 -2
[1] 0 0

Both z1 and z2 have a root of -2, but I guess Im(z1) isn't close enough to zero for identical().


The power of accurate observation is commonly called cynicism by those who have not got it. George Bernard Shaw
View this message in context: http://www.nabble.com/Comparing-complex-numbers-tp18406924p18406924.html
Sent from the R help mailing list archive at Nabble.com.

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 11 Jul 2008 - 16:11:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Jul 2008 - 17:31:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive