Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"%%" and "%/%" for the remainder and the quotient

Tags:

r

I am wondering how and why the operator %% and %/% are for the remainder and the quotient.

Is there any reason or history that R developer had given them the meaning they have?

 > 0 %/% 10 [1] 0 > 30 %% 10 [1] 0 > 35 %/% 10 [1] 3 > 35 %% 10 [1] 5 
like image 960
KH Kim Avatar asked Jul 26 '12 13:07

KH Kim


People also ask

How do you find the quotient and remainder?

Expressions used in program to calculate quotient and remainder: quotient = dividend / divisor; remainder = dividend % divisor; Note: The program will throw an ArithmeticException: / by zero when divided by 0.

What is quotient and remainder examples?

It can be greater than or lesser than the quotient. For example; when 41 is divided by 7, the quotient is 5 and the remainder is 6. Here the remainder is greater than the quotient.


1 Answers

In R, you can assign your own operators using %[characters]%. A trivial example:

'%p%' <- function(x, y){x^2 + y}  2 %p% 3 # result: 7 

While I agree with BlueTrin that %% is pretty standard, I have a suspicion %/% may have something to do with the sort of operator definitions I showed above - perhaps it was easier to implement, and makes sense: %/% means do a special sort of division (integer division)

like image 51
Edward Avatar answered Sep 19 '22 11:09

Edward