Skip to content
Prev 31125 / 398513 Next

How %% and %/% work on negative numbers?

Kenneth Cabrera wrote:

            
Because R is not C, and they are doing different things.
R is doing "arithmetic modulo N" (which can only take values from 0 to 
N-1) and C is doing 'remainder when divided by N' which can be negative.

  These are two different (but related) things. R's %% operator is 
documented as "x mod y" - which means it does modulo-y arithmetic. The 
man page for 'fmod' in C says: "The  fmod()  function  computes  the 
remainder of dividing x by y". I dont have my K+R C book handy to see 
how they define the x % y operator, but I suspect its the same.

Baz