4

How modulus works with negative integers in R:

-90 %% 360
90 %% -360
-400 %% 360
400 %% -360

outputs:

270
-270
320
-320

what is the rationale behind this?

The answer from @JimSlonder:

a <- c(-90,90,-400,400)
b <- c(360,-360,360,-360)

modulus <- function(a,b)
  a-floor(a/b)*b


all("%%"(a,b) == modulus(a,b))
Farid Cheraghi
  • 847
  • 2
  • 12
  • 23

0 Answers0