[Rd] Friday question: negative zero

Tony Plate tplate at acm.org
Sun Sep 2 01:22:26 CEST 2007


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



More information about the R-devel mailing list