What does the double percent sign (%%) mean?

What is the double percentage ( %% ) used for R?

From its use, it looks as if it divides the number in front by the number in the opposite direction as many times as it can, and returns the value on the left. It is right?

Out of curiosity, when will it be useful?

+13
source share
1 answer

On the Help page "Arithmetic Operators" (which can you go through ?"%%" ) is written

"%% indicates" x mod y

This is only useful if you have done enough programming to know that this relates to modular division , that is, divide the integer x by y and return the remainder. This is useful in many, many, many applications. For example (from @GavinSimpson in the comments), %% is useful if you run a loop and want to display some kind of progress indicator on the screen every nth iteration (for example, use if (i %% 10 == 0) { #do something} to do something every 10th iteration).

Since %% also works for floating-point numbers in R, I just have an if (any(wts %% 1 != 0)) example, where if (any(wts %% 1 != 0)) used to check, where any of the wts values wts not an integer.

+22
source

Source: https://habr.com/ru/post/987266/


All Articles