Floor Division and Modulo
Floor Division and Modulo
Modulo Operator(%)
// and % with –ve and +ve integers
/ and //
Math.floor()
Math.floor(x)=[x]
• floor(2)=floor(2.00001)=floor(2.999999)=2
• floor(3)=floor(3.00001)=floor(3.999999)=3
• floor(-2)=floor(-1.999999)=floor(-1.000001)=-2
• floor(-3)=floor(-2.999999)=floor(-2.00001)=-3
/, // and %
• 50//12
• Remainder=dividend-divisor*floor(quotient)
a%b= a-b * (a//b)
// and % with +ve and –ve integers (of same
sign)
• 9/4= -9/-4 = 2.25
• 9//4 = -9//-4 = floor(2.25)=2
• 9%4= 9 - 4 * 2 = 9 -8 =1
• remainder=dividend-divisor*floor(quotient)
// and % with +ve and –ve integers (of same
sign)
• 9/4= -9/-4 = 2.25
• 9//4 = -9//-4 = floor(2.25)=2
• -9%-4= -9 - (- 4) * 2 = -9 + 8 = -1
• remainder=dividend-divisor*floor(quotient)
// and % with +ve and –ve integers (of
opposite sign)
• -9/4= 9/-4 = -2.25
• -9//4 = 9//-4 = floor(-2.25)=-3