4

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"?

J. Mini
  • 1,868
  • 1
  • 9
  • 38
  • 1
    I believe that the answer to the question is *no, there is no negative zero in R* but see if [this SO post](https://stackoverflow.com/questions/59229545/what-is-the-correct-standard-way-to-check-if-difference-is-smaller-than-machine) can be of help. – Rui Barradas Feb 26 '21 at 10:55
  • @PKumar That looks sufficient. The only way to do better would be to find somewhere in the documentation that says in plain English that you're right. – J. Mini Feb 26 '21 at 11:00
  • Also relevant, see `?.Machine` and [this other SO post](https://stackoverflow.com/questions/38165221/r-largest-smallest-representable-numbers). – Rui Barradas Feb 26 '21 at 11:01
  • 1
    @PKumar That should be an answer. Post it. – J. Mini Feb 26 '21 at 11:13
  • Why would R have it? – Roland Feb 26 '21 at 11:48
  • The apparent negative answer is somewhat surprising in that R does support [subnormal numbers](https://en.wikipedia.org/wiki/Denormal_number), as simple experiments show. I wouldn't be surprised if there was *some* way to create a negative 0 in R, one that didn't involve a literal `-0` but instead casts a bit pattern to a float. – John Coleman Feb 26 '21 at 11:49

2 Answers2

7

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
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks for this, blew my mind, i will be deleting my answer. I was convinced that they are the same. – PKumar Feb 26 '21 at 13:30
  • I think this is slightly misleading. It's not R that has a negative zero, it's C that has a negative zero. See the "Implementation limits" section in `help("Arithmetic")`. The R language definition does not describe a negative zero. I don't have access to the "Blue Book" to check if a negative zero is mentioned there. – Roland Feb 26 '21 at 14:15
  • 2
    Which part of the stack does this isn't really the key point. What is important is what the result is when you use R. It certainly is not misleading. – G. Grothendieck Feb 26 '21 at 14:22
  • I found this out the hard way - write_csv (from the Tidyverse) will write -0 if there is a minus zero! ````format_csv(tribble(~Zero, ~MinusZero, ~MinusOneTimesZero, 0, -0, -1*0))```` – Dan Jun 28 '23 at 12:03
0

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.

bra_racing
  • 622
  • 1
  • 8
  • 32
  • 2
    "_In your situation, there is not difference between index 0 and -0_". This seems like a backwards way to answer the question. I'm asking if there's any difference between `0` and `-0` at all. How they act as indexes is tangential to the real problem. – J. Mini Feb 26 '21 at 10:42
  • Well, if 0 index returns an empty vector, and a negative index returns all the elements but the ones returned by the positive index, what should be the expected result? An infinite vector? – bra_racing Feb 26 '21 at 10:45
  • 2
    Again, you're talking about index 0. I'm talking about number 0 and number -0. – J. Mini Feb 26 '21 at 10:46
  • Are you talking about the result? integer(0) does not represent the number zero, but a vector of integers with 0 elements. Sorry if I am misundersting your question – bra_racing Feb 26 '21 at 10:50
  • 2
    The question is "does R have "negative 0"?"; I'm asking if R distinguishes between `-1*0` and `0`. Indexing is related but not an answer. – J. Mini Feb 26 '21 at 10:59