[R] How %% and %/% work on negative numbers?

Kenneth Cabrera krcabrer at perseus.unalmed.edu.co
Thu Apr 24 14:40:37 CEST 2003


Hi R developers and users:

How do "mod" and "integer division" work with negative integers on R?

I mean why you obtain different results if you work on C or anywhere else 
and in R?

I see R guarantees that `x  ==  (x %% y)  +  y * ( x %/% y )', but
it looks rare the individual (x %% y) and y * ( x %/% y ) results when
you use negative integers, because is contradictory to what is expected, 
e.g.
-17 mod 15 should be -2, but R shows 13 and
-17 integer division 15 should be -1, but R shows -2
because the reconstruction formula should be 15*(-1)+(-2) == -17
Or am I wrong?

Thank you for your help

Kenneth



More information about the R-help mailing list