The IEEE floating point standard allows for negative zero, but it's hard to know that you have one in R. One reliable test is to take the reciprocal. For example, > y <- 0 > 1/y [1] Inf > y <- -y > 1/y [1] -Inf The other day I came across one in complex numbers, and it took me a while to figure out that negative zero was what was happening: > x <- complex(real = -1) > x [1] -1+0i > 1/x [1] -1+0i > x^(1/3) [1] 0.5+0.8660254i > (1/x)^(1/3) [1] 0.5-0.8660254i (The imaginary part of 1/x is negative zero.) As a Friday question: are there other ways to create and detect negative zero in R? And another somewhat more serious question: is the behaviour of negative zero consistent across platforms? (The calculations above were done in Windows in R-devel.) Duncan Murdoch
Friday question: negative zero
4 messages · Duncan Murdoch, Gabor Grothendieck, Steven McKinney +1 more
On 8/31/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
The IEEE floating point standard allows for negative zero, but it's hard to know that you have one in R. One reliable test is to take the reciprocal. For example,
> y <- 0 > 1/y
[1] Inf
> y <- -y > 1/y
[1] -Inf The other day I came across one in complex numbers, and it took me a while to figure out that negative zero was what was happening:
> x <- complex(real = -1) > x
[1] -1+0i
> 1/x
[1] -1+0i
> x^(1/3)
[1] 0.5+0.8660254i
> (1/x)^(1/3)
[1] 0.5-0.8660254i (The imaginary part of 1/x is negative zero.) As a Friday question: are there other ways to create and detect negative zero in R?
I think the only other way is the IEEE CopySign function but I don't think R has implemented it -- R probably should. Google CopySign .
Seems the same on this Apple Mac OSX platform:
y <- 0 1/y
[1] Inf
y <- -y 1/y
[1] -Inf
x <- complex(real = -1) x
[1] -1+0i
1/x
[1] -1+0i
x^(1/3)
[1] 0.5+0.8660254i
(1/x)^(1/3)
[1] 0.5-0.8660254i
sessionInfo()
R version 2.5.1 (2007-06-27) powerpc-apple-darwin8.9.1 locale: en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8 attached base packages: [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"
system("uname -a")
Darwin Dapple.local 8.10.0 Darwin Kernel Version 8.10.0: Wed May 23 16:50:59 PDT 2007; root:xnu-792.21.3~1/RELEASE_PPC Power Macintosh powerpc
version
_ platform powerpc-apple-darwin8.9.1 arch powerpc os darwin8.9.1 system powerpc, darwin8.9.1 status major 2 minor 5.1 year 2007 month 06 day 27 svn rev 42083 language R version.string R version 2.5.1 (2007-06-27) Steven McKinney -----Original Message----- From: r-devel-bounces at r-project.org on behalf of Duncan Murdoch Sent: Fri 8/31/2007 5:39 PM To: R Devel Subject: [Rd] Friday question: negative zero The IEEE floating point standard allows for negative zero, but it's hard to know that you have one in R. One reliable test is to take the reciprocal. For example, > y <- 0 > 1/y [1] Inf > y <- -y > 1/y [1] -Inf The other day I came across one in complex numbers, and it took me a while to figure out that negative zero was what was happening: > x <- complex(real = -1) > x [1] -1+0i > 1/x [1] -1+0i > x^(1/3) [1] 0.5+0.8660254i > (1/x)^(1/3) [1] 0.5-0.8660254i (The imaginary part of 1/x is negative zero.) As a Friday question: are there other ways to create and detect negative zero in R? And another somewhat more serious question: is the behaviour of negative zero consistent across platforms? (The calculations above were done in Windows in R-devel.) Duncan Murdoch ______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
On 8/31/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
The IEEE floating point standard allows for negative zero, but it's hard to know that you have one in R. One reliable test is to take the reciprocal. For example,
> y <- 0 > 1/y
[1] Inf
> y <- -y > 1/y
[1] -Inf The other day I came across one in complex numbers, and it took me a while to figure out that negative zero was what was happening:
> x <- complex(real = -1) > x
[1] -1+0i
> 1/x
[1] -1+0i
> x^(1/3)
[1] 0.5+0.8660254i
> (1/x)^(1/3)
[1] 0.5-0.8660254i (The imaginary part of 1/x is negative zero.) As a Friday question: are there other ways to create and detect negative zero in R? And another somewhat more serious question: is the behaviour of negative zero consistent across platforms? (The calculations above were done in Windows in R-devel.)
No, I get
1/ Im(1/complex(real = -1))
[1] Inf
sessionInfo()
R version 2.6.0 Under development (unstable) (2007-08-16 r42532) x86_64-unknown-linux-gnu This is on an AMD 64, and I get -Inf on everything else I've tried so far, which are all Intel. -Deepayan