Den 2022-12-19 kl. 15:41, skrev Martin Maechler:>>>>>> G?ran Brostr?m >>>>>> on Mon, 19 Dec 2022 14:22:00 +0100 writes: > > > I have a long vector x with five-digit codes where the > > first digit of each is of special interest, so I extracted > > them through > > >> y <- x %/% 10000 > > > but to my surprise y contained the value -1 in some > > places. It turned out that x contains -1 as a symbol for > > 'missing value' so in effect I found that > > >> -1 %/% 10000 == -1 > > > Had to check the help page for "%/%", and the first > > relevant comment I found was: > > > "Users are sometimes surprised by the value returned". > > > No surprise there. Further down: > > > ?%%? indicates ?x mod y? (?x modulo y?) and ?%/%? > > indicates integer division. It is guaranteed that > > > ? x == (x %% y) + y * (x %/% y) ? (up to rounding > > error) > > > I did expect (a %/% b) to return round(a / b), like > > gfortran and gcc, > > What??? I cannot believe you.Well, you shouldn't, I generalized too far.> > No time for checking now, but I bet that > 8 / 3 gives 2 and not 3 in C and Fortran > (and hence gcc, etc)But compare -8 %/% 3 in R and -8 / 3 in C/Fortran. G,> > > > but instead I get floor(a / b) in > > R. What is the reason for these different definitions? And > > shouldn't R's definition be documented? > > > > > Thanks, G?ran > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and > > more, see https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html and provide > > commented, minimal, self-contained, reproducible code.
If R does exactly what it says it does, why are you surprised, whether or not that is what other languages do? (Please excuse my fractured English). (Note: -8 = 1 + 3*(-3) = (-8 %% 3) + 3 * (-8 %/% 3 ), exactly as the Help excerpt you cited says) Of course, one may always question the wisdom of whatever policy R chooses, but that gets into the bowels of what standards to follow, corner cases, and all that, about which I have nothing to say. My only point is the above. -- Bert On Mon, Dec 19, 2022 at 7:15 AM G?ran Brostr?m <gb at ehar.se> wrote:> > > Den 2022-12-19 kl. 15:41, skrev Martin Maechler: > >>>>>> G?ran Brostr?m > >>>>>> on Mon, 19 Dec 2022 14:22:00 +0100 writes: > > > > > I have a long vector x with five-digit codes where the > > > first digit of each is of special interest, so I extracted > > > them through > > > > >> y <- x %/% 10000 > > > > > but to my surprise y contained the value -1 in some > > > places. It turned out that x contains -1 as a symbol for > > > 'missing value' so in effect I found that > > > > >> -1 %/% 10000 == -1 > > > > > Had to check the help page for "%/%", and the first > > > relevant comment I found was: > > > > > "Users are sometimes surprised by the value returned". > > > > > No surprise there. Further down: > > > > > ?%%? indicates ?x mod y? (?x modulo y?) and ?%/%? > > > indicates integer division. It is guaranteed that > > > > > ? x == (x %% y) + y * (x %/% y) ? (up to rounding > > > error) > > > > > I did expect (a %/% b) to return round(a / b), like > > > gfortran and gcc, > > > > What??? I cannot believe you. > > Well, you shouldn't, I generalized too far. > > > > No time for checking now, but I bet that > > 8 / 3 gives 2 and not 3 in C and Fortran > > (and hence gcc, etc) > > But compare -8 %/% 3 in R and -8 / 3 in C/Fortran. > > G, > > > > > > > > but instead I get floor(a / b) in > > > R. What is the reason for these different definitions? And > > > shouldn't R's definition be documented? > > > > > > > > > Thanks, G?ran > > > > > ______________________________________________ > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and > > > more, see https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide > > > http://www.R-project.org/posting-guide.html and provide > > > commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
See https://en.m.wikipedia.org/wiki/Modulo_operation, Variants of the definition, esp the point that Knuth recommended the floor definition. The behavior of %/% follows from the definition of %% given the documented relation in ?Arithmetic. R is not obligated to repeat the mistakes of C or Fortran. On December 19, 2022 7:15:01 AM PST, "G?ran Brostr?m" <gb at ehar.se> wrote:> > >Den 2022-12-19 kl. 15:41, skrev Martin Maechler: >>>>>>> G?ran Brostr?m >>>>>>> on Mon, 19 Dec 2022 14:22:00 +0100 writes: >> >> > I have a long vector x with five-digit codes where the >> > first digit of each is of special interest, so I extracted >> > them through >> >> >> y <- x %/% 10000 >> >> > but to my surprise y contained the value -1 in some >> > places. It turned out that x contains -1 as a symbol for >> > 'missing value' so in effect I found that >> >> >> -1 %/% 10000 == -1 >> >> > Had to check the help page for "%/%", and the first >> > relevant comment I found was: >> >> > "Users are sometimes surprised by the value returned". >> >> > No surprise there. Further down: >> >> > ?%%? indicates ?x mod y? (?x modulo y?) and ?%/%? >> > indicates integer division. It is guaranteed that >> >> > ? x == (x %% y) + y * (x %/% y) ? (up to rounding >> > error) >> >> > I did expect (a %/% b) to return round(a / b), like >> > gfortran and gcc, >> >> What??? I cannot believe you. > >Well, you shouldn't, I generalized too far. >> >> No time for checking now, but I bet that >> 8 / 3 gives 2 and not 3 in C and Fortran >> (and hence gcc, etc) > >But compare -8 %/% 3 in R and -8 / 3 in C/Fortran. > >G, > >> >> >> > but instead I get floor(a / b) in >> > R. What is the reason for these different definitions? And >> > shouldn't R's definition be documented? >> >> >> >> > Thanks, G?ran >> >> > ______________________________________________ >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and >> > more, see https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guide >> > http://www.R-project.org/posting-guide.html and provide >> > commented, minimal, self-contained, reproducible code. > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.