Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 647
J. Mini Avatar asked Jul 27 '26 07:07

J. Mini


1 Answers

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
like image 98
G. Grothendieck Avatar answered Jul 28 '26 20:07

G. Grothendieck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!