J. Mini
J. Mini

Reputation: 1610

Does R have "negative zero"?

My console tells me that -0 returns 0 and that both c(1:5)[0] and c(1:5)[-0] return integer(0). Does R this mean that R has no concept of "negative zero"?

Upvotes: 4

Views: 560

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 270170

Although R does a good job of hiding it, in fact R does have a negative zero:

# R says these are the same

0 == -0
## [1] TRUE

identical(0, -0)
## [1] TRUE

# but they are not

is.neg0 <- function(x) x == 0 && sign(1/x) == -1

is.neg0(0)
## [1] FALSE

is.neg0(-0)
## [1] TRUE

Upvotes: 8

bra_racing
bra_racing

Reputation: 620

Indexing in R starts in 1, the index 0 is allowed, but represents an empty vector (R Language Definition). The negative sign means "give me all the elements of the list but the one I've specified in the index", i.e. c(1:5)[-2] returns [1] 1 3 4 5. In your situation, there is not difference between index 0 and -0, they represent the same behaviour.

Upvotes: -1

Related Questions