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
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
One place where I've been caught by -ve zeros is with unit tests. If identical(-0, 0) returns FALSE, and the object storage doesn't preserve -ve zeros, that can lead to test failures that are tricky to debug. However, it doesn't look like that is too much a problem in the current incarnation of R, at least running under Linux: * identical(-0, 0) returns TRUE * save/load preserves -ve zero * however dump() does NOT preserve -ve zero The fact that identical(-0,0) is TRUE means that we have the situation where it is possible that identical(x, y) != identical(f(x), f(y)). I don't know if this is a real problem anywhere. > x <- 0 > y <- -0 > 1/x [1] Inf > 1/y [1] -Inf > identical(x, y) [1] TRUE > ### there exists f,x,y such that identical(x, y) != identical(f(x), f(y)) > identical(1/x, 1/y) [1] FALSE > ### save/load preserves -ve zero > save("y", file="y.rda") > remove("y") > load("y.rda") > 1/y [1] -Inf > identical(x, y) [1] TRUE > ### dump does not preserve -ve zero > dump("y", file="y.R") > remove("y") > source("y.R") > y [1] 0 > 1/y [1] Inf > version _ platform x86_64-unknown-linux-gnu arch x86_64 os linux-gnu system x86_64, linux-gnu 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) > -- Tony Plate
Hello everyone On 1 Sep 2007, at 01:39, Duncan Murdoch 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.) >I have been pondering branch cuts and branch points for some functions which I am implementing. In this area, it is very important to know whether one has +0 or -0. Take the log() function, where it is sometimes very important to know whether one is just above the imaginary axis or just below it: (i). Small y > y <- 1e-100 > log(-1 + 1i*y) [1] 0+3.141593i > y <- -y > log(-1 + 1i*y) [1] 0-3.141593i (ii) Zero y. > y <- 0 > log(-1 + 1i*y) [1] 0+3.141593i > y <- -y > log(-1 + 1i*y) [1] 0+3.141593i > [ie small imaginary jumps have a discontinuity, infinitesimal jumps don't]. This behaviour is undesirable (IMO): one would like log (-1+0i) to be different from log(-1-0i). Tony Plate's example shows that even though y<- 0 ; identical(y, -y) is TRUE, one has identical(1/ y, 1/(-y)) is FALSE, so the sign is not discarded. My complex function does have a branch cut that follows a portion of the negative real axis but the other cuts follow absurdly complicated implicit equations. At this point one needs the IEEE requirement that x=x == +0 [ie not -0] for any real x; one then finds that (s-t) and -(t-s) are numerically equal but not necessarily indistinguishable. One of my earlier questions involved branch cuts for the inverse trig functions but (IIRC) the patch I supplied only tested for the imaginary part being >0; would it be possible to include information about signed zero in these or other functions?> Duncan Murdoch > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Robin Hankin Uncertainty Analyst and Neutral Theorist, National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743