0% found this document useful (0 votes)
17 views13 pages

Floor Division and Modulo

The document discusses the floor division (//) and modulo (%) operators in Python. It explains that: 1) // performs floor division, which returns the integer part of the quotient of the numbers. % calculates the remainder of the division. 2) These operators can be used with both positive and negative integers and floats. The sign of the numbers determines whether the floor or modulo is calculated before or after division. 3) Examples are provided to illustrate the behavior of // and % in different cases like when the numbers have the same sign or opposite signs. Remainder is calculated as dividend - divisor * floor(quotient).

Uploaded by

shivam0402008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

Floor Division and Modulo

The document discusses the floor division (//) and modulo (%) operators in Python. It explains that: 1) // performs floor division, which returns the integer part of the quotient of the numbers. % calculates the remainder of the division. 2) These operators can be used with both positive and negative integers and floats. The sign of the numbers determines whether the floor or modulo is calculated before or after division. 3) Examples are provided to illustrate the behavior of // and % in different cases like when the numbers have the same sign or opposite signs. Remainder is calculated as dividend - divisor * floor(quotient).

Uploaded by

shivam0402008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Floor Division (//) and

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

• -9%4= -9 - 4 * (-3)= -9 +12 = 3


• 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)=-3

• 9%-4= 9 - (- 4) * (-3) = 9 -12 = -3


• remainder=dividend-divisor*floor(quotient)
// and % with +ve and –ve floats (of same
sign)
• 6.3/1.5= -6.3/-1.5 = 4.2
• 6.3//1.5 = -6.3//-1.5 = floor(4.2)=4.0

• 6.3%1.5= 6.3 - 1.5 * 4.0 = 6.3 - 6.0=0.3


• remainder=dividend-divisor*floor(quotient)
// and % with +ve and –ve floats (of same
sign)
• 6.3/1.5= -6.3/-1.5 = 4.2
• 6.3//1.5 = -6.3//-1.5 = floor(4.2)=4.0

• -6.3% -1.5= - 6.3 –(-1.5) * 4.0 = - 6.3 +6.0 = - 0.3


• remainder=dividend-divisor*floor(quotient)
// and % with +ve and –ve floats (of opposite
sign)
• -6.3/1.5= 6.3/-1.5 = -4.2
• -6.3//1.5 = 6.3//-1.5 = floor(-4.2)=-5.0

• -6.3%1.5= -6.3 - 1.5 * (-5.0) = -6.3 +7.5=1.2


• remainder=dividend-divisor*floor(quotient)
// and % with +ve and –ve floats (of opposite
sign)
• -6.3/1.5= 6.3/-1.5 = - 4.2
• -6.3//1.5 = 6.3//-1.5 = floor(-4.2)= -5.0

• 6.3% -1.5= 6.3 –(-1.5) * (-5.0) = 6.3 -7.5 = - 1.2


• remainder=dividend-divisor*floor(quotient)
// and % in Python
(Practice)

You might also like