Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:
>>> 6/3
2
>>> 6//3
2
In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.
In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.
Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.
You can find a detailed description at PEP 238: Changing the Division Operator.
Python 2.x Clarification:
To clarify for the Python 2.x line, / is neither floor division nor true division.
/ is floor division when both args are int, but is true division when either of the args are float.
// implements "floor division", regardless of your type. So
1.0/2.0 will give 0.5, but both 1/2, 1//2 and 1.0//2.0 will give 0.
See PEP 238: Changing the Division Operator for details.
/ → Floating point division
// → Floor division
Let’s see some examples in both Python 2.7 and in Python 3.5.
Python 2.7.10 vs. Python 3.5
print (2/3) ----> 0 Python 2.7
print (2/3) ----> 0.6666666666666666 Python 3.5
Python 2.7.10 vs. Python 3.5
print (4/2) ----> 2 Python 2.7
print (4/2) ----> 2.0 Python 3.5
Now if you want to have (in Python 2.7) the same output as in Python 3.5, you can do the following:
Python 2.7.10
from __future__ import division
print (2/3) ----> 0.6666666666666666 # Python 2.7
print (4/2) ----> 2.0 # Python 2.7
Whereas there isn't any difference between floor division in both Python 2.7 and in Python 3.5.
138.93//3 ---> 46.0 # Python 2.7
138.93//3 ---> 46.0 # Python 3.5
4//3 ---> 1 # Python 2.7
4//3 ---> 1 # Python 3.5
As everyone has already answered, // is floor division.
Why this is important is that // is unambiguously floor division, in all Python versions from 2.2, including Python 3.x versions.
The behavior of / can change depending on:
Active __future__ import or not (module-local)
Python command line option, either -Q old or -Q new
>>> print 5.0 / 2
2.5
>>> print 5.0 // 2
2.0
Python 2.7 and other upcoming versions of Python:
Division (/)
Divides left hand operand by right hand operand
Example: 4 / 2 = 2
Floor division (//)
The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):
Examples: 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
Both / division and // floor division operator are operating in similar fashion.
The double slash, //, is floor division:
>>> 7//3
2
// is floor division. It will always give you the integer floor of the result. The other is 'regular' division.
The previous answers are good. I want to add another point. Up to some values both of them result in the same quotient. After that floor division operator (//) works fine but not division (/) operator:
>>> int(755349677599789174 / 2) # Wrong answer
377674838799894592
>>> 755349677599789174 // 2 # Correct answer
377674838799894587
The answer of the equation is rounded to the next smaller integer or float with .0 as decimal point.
>>>print 5//2
2
>>> print 5.0//2
2.0
>>>print 5//2.0
2.0
>>>print 5.0//2.0
2.0
Python 3.x Clarification
Just to complement some previous answers.
It is important to remark that:
a // b
Is floor division. As in:
math.floor(a/b)
Is not int division. As in:
int(a/b)
Is not round to 0 float division. As in:
round(a/b,0)
As a consequence, the way of behaving is different when it comes to positives an negatives numbers as in the following example:
1 // 2 is 0, as in:
math.floor(1/2)
-1 // 2 is -1, as in:
math.floor(-1/2)
Python 3
Operation
Result
Notes
x / y
quotient of x and y
x // y
floored quotient of x and y
(1)
Notes:
Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int. The result is always rounded towards minus infinity: 1//2 is 0, (-1)//2 is -1, 1//(-2) is -1, and (-1)//(-2) is 0.
Python 2
Operation
Result
Notes
x / y
quotient of x and y
(1)
x // y
(floored) quotient of x and y
(4)(5)
Notes:
1. For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result is a long integer if either operand is a long integer, regardless of the numeric value.
4. Deprecated since version 2.3: The floor division operator, the modulo operator, and the divmod() function are no longer defined for complex numbers. Instead, convert to a floating point number using the abs() function if appropriate.
5. Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int.
Summary
x//y : EXACT integer division
int(x/y) OR math.floor(x/y): INEXACT integer division (but almost correct)
x/y: floating point division (that has the loss of significance)
Remarkable Calculation Result
import math
N = 1004291331219602346 # huge number
print(N//100) #=> 10042913312196023 is correct answer
print(math.floor(N/100)) #=> 10042913312196024 is wrong answer
print(math.ceil(N/100)) #=> 10042913312196024 is wrong answer
print(int(N/100)) #=> 10042913312196024 is wrong answer
Consideration
I think about the evaluation of int(x/y).
At first, Python evaluate the expression x/y and get INEXACT floating number z.
Second, Python evaluate the expression int(z).
We get a wrong result when the loss of significance cannot be ignored.
// is floor division. It will always give you the floor value of the result.
And the other one, /, is the floating-point division.
In the following is the difference between / and //;
I have run these arithmetic operations in Python 3.7.2.
>>> print (11 / 3)
3.6666666666666665
>>> print (11 // 3)
3
>>> print (11.3 / 3)
3.7666666666666667
>>> print (11.3 // 3)
3.0
5.0//2 results in 2.0, and not 2, because the return type of the return value from // operator follows Python coercion (type casting) rules.
Python promotes conversion of lower data type (integer) to higher data type (float) to avoid data loss.
Related
For example,
int result;
result = 125/100;
or
result = 43/100;
Will result always be the floor of the division? What is the defined behavior?
Will result always be the floor of the division? What is the defined behavior?
Not quite. It rounds toward 0, rather than flooring.
6.5.5 Multiplicative operators
6 When integers are divided, the result of the / operator is the algebraic quotient with any
fractional part discarded.88) If the quotient a/b is representable, the expression
(a/b)*b + a%b shall equal a.
and the corresponding footnote:
This is often called ‘‘truncation toward zero’’.
Of course two points to note are:
3 The usual arithmetic conversions are performed on the operands.
and:
5 The result of the / operator is the
quotient from the division of the
first operand by the second; the
result of the % operator is the
remainder. In both operations, if the
value of the second operand is zero,
the behavior is undefined.
[Note: Emphasis mine]
Dirkgently gives an excellent description of integer division in C99, but you should also know that in C89 integer division with a negative operand has an implementation-defined direction.
From the ANSI C draft (3.3.5):
If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.
So watch out with negative numbers when you are stuck with a C89 compiler.
It's a fun fact that C99 chose truncation towards zero because that was how FORTRAN did it. See this message on comp.std.c.
Yes, the result is always truncated towards zero. It will round towards the smallest absolute value.
-5 / 2 = -2
5 / 2 = 2
For unsigned and non-negative signed values, this is the same as floor (rounding towards -Infinity).
Where the result is negative, C truncates towards 0 rather than flooring - I learnt this reading about why Python integer division always floors here: Why Python's Integer Division Floors
Will result always be the floor of the division?
No. The result varies, but variation happens only for negative values.
What is the defined behavior?
To make it clear floor rounds towards negative infinity,while integer division rounds towards zero (truncates)
For positive values they are the same
int integerDivisionResultPositive= 125/100;//= 1
double flooringResultPositive= floor(125.0/100.0);//=1.0
For negative value this is different
int integerDivisionResultNegative= -125/100;//=-1
double flooringResultNegative= floor(-125.0/100.0);//=-2.0
I know people have answered your question but in layman terms:
5 / 2 = 2 //since both 5 and 2 are integers and integers division always truncates decimals
5.0 / 2 or 5 / 2.0 or 5.0 /2.0 = 2.5 //here either 5 or 2 or both has decimal hence the quotient you will get will be in decimal.
Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:
>>> 6/3
2
>>> 6//3
2
In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.
In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.
Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.
You can find a detailed description at PEP 238: Changing the Division Operator.
Python 2.x Clarification:
To clarify for the Python 2.x line, / is neither floor division nor true division.
/ is floor division when both args are int, but is true division when either of the args are float.
// implements "floor division", regardless of your type. So
1.0/2.0 will give 0.5, but both 1/2, 1//2 and 1.0//2.0 will give 0.
See PEP 238: Changing the Division Operator for details.
/ → Floating point division
// → Floor division
Let’s see some examples in both Python 2.7 and in Python 3.5.
Python 2.7.10 vs. Python 3.5
print (2/3) ----> 0 Python 2.7
print (2/3) ----> 0.6666666666666666 Python 3.5
Python 2.7.10 vs. Python 3.5
print (4/2) ----> 2 Python 2.7
print (4/2) ----> 2.0 Python 3.5
Now if you want to have (in Python 2.7) the same output as in Python 3.5, you can do the following:
Python 2.7.10
from __future__ import division
print (2/3) ----> 0.6666666666666666 # Python 2.7
print (4/2) ----> 2.0 # Python 2.7
Whereas there isn't any difference between floor division in both Python 2.7 and in Python 3.5.
138.93//3 ---> 46.0 # Python 2.7
138.93//3 ---> 46.0 # Python 3.5
4//3 ---> 1 # Python 2.7
4//3 ---> 1 # Python 3.5
As everyone has already answered, // is floor division.
Why this is important is that // is unambiguously floor division, in all Python versions from 2.2, including Python 3.x versions.
The behavior of / can change depending on:
Active __future__ import or not (module-local)
Python command line option, either -Q old or -Q new
>>> print 5.0 / 2
2.5
>>> print 5.0 // 2
2.0
Python 2.7 and other upcoming versions of Python:
Division (/)
Divides left hand operand by right hand operand
Example: 4 / 2 = 2
Floor division (//)
The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):
Examples: 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
Both / division and // floor division operator are operating in similar fashion.
The double slash, //, is floor division:
>>> 7//3
2
// is floor division. It will always give you the integer floor of the result. The other is 'regular' division.
The previous answers are good. I want to add another point. Up to some values both of them result in the same quotient. After that floor division operator (//) works fine but not division (/) operator:
>>> int(755349677599789174 / 2) # Wrong answer
377674838799894592
>>> 755349677599789174 // 2 # Correct answer
377674838799894587
The answer of the equation is rounded to the next smaller integer or float with .0 as decimal point.
>>>print 5//2
2
>>> print 5.0//2
2.0
>>>print 5//2.0
2.0
>>>print 5.0//2.0
2.0
Python 3.x Clarification
Just to complement some previous answers.
It is important to remark that:
a // b
Is floor division. As in:
math.floor(a/b)
Is not int division. As in:
int(a/b)
Is not round to 0 float division. As in:
round(a/b,0)
As a consequence, the way of behaving is different when it comes to positives an negatives numbers as in the following example:
1 // 2 is 0, as in:
math.floor(1/2)
-1 // 2 is -1, as in:
math.floor(-1/2)
Python 3
Operation
Result
Notes
x / y
quotient of x and y
x // y
floored quotient of x and y
(1)
Notes:
Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int. The result is always rounded towards minus infinity: 1//2 is 0, (-1)//2 is -1, 1//(-2) is -1, and (-1)//(-2) is 0.
Python 2
Operation
Result
Notes
x / y
quotient of x and y
(1)
x // y
(floored) quotient of x and y
(4)(5)
Notes:
1. For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result is a long integer if either operand is a long integer, regardless of the numeric value.
4. Deprecated since version 2.3: The floor division operator, the modulo operator, and the divmod() function are no longer defined for complex numbers. Instead, convert to a floating point number using the abs() function if appropriate.
5. Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int.
Summary
x//y : EXACT integer division
int(x/y) OR math.floor(x/y): INEXACT integer division (but almost correct)
x/y: floating point division (that has the loss of significance)
Remarkable Calculation Result
import math
N = 1004291331219602346 # huge number
print(N//100) #=> 10042913312196023 is correct answer
print(math.floor(N/100)) #=> 10042913312196024 is wrong answer
print(math.ceil(N/100)) #=> 10042913312196024 is wrong answer
print(int(N/100)) #=> 10042913312196024 is wrong answer
Consideration
I think about the evaluation of int(x/y).
At first, Python evaluate the expression x/y and get INEXACT floating number z.
Second, Python evaluate the expression int(z).
We get a wrong result when the loss of significance cannot be ignored.
// is floor division. It will always give you the floor value of the result.
And the other one, /, is the floating-point division.
In the following is the difference between / and //;
I have run these arithmetic operations in Python 3.7.2.
>>> print (11 / 3)
3.6666666666666665
>>> print (11 // 3)
3
>>> print (11.3 / 3)
3.7666666666666667
>>> print (11.3 // 3)
3.0
5.0//2 results in 2.0, and not 2, because the return type of the return value from // operator follows Python coercion (type casting) rules.
Python promotes conversion of lower data type (integer) to higher data type (float) to avoid data loss.
This question already has answers here:
How does the modulo (%) operator work on negative numbers in Python?
(12 answers)
Closed last month.
What does modulo in the following piece of code do?
from math import *
3.14 % 2 * pi
How do we calculate modulo on a floating point number?
When you have the expression:
a % b = c
It really means there exists an integer n that makes c as small as possible, but non-negative.
a - n*b = c
By hand, you can just subtract 2 (or add 2 if your number is negative) over and over until the end result is the smallest positive number possible:
3.14 % 2
= 3.14 - 1 * 2
= 1.14
Also, 3.14 % 2 * pi is interpreted as (3.14 % 2) * pi. I'm not sure if you meant to write 3.14 % (2 * pi) (in either case, the algorithm is the same. Just subtract/add until the number is as small as possible).
In addition to the other answers, the fmod documentation has some interesting things to say on the subject:
math.fmod(x, y)
Return fmod(x, y), as defined by the platform C
library. Note that the Python expression x % y may not return the same
result. The intent of the C standard is that fmod(x, y) be exactly
(mathematically; to infinite precision) equal to x - n*y for some
integer n such that the result has the same sign as x and magnitude
less than abs(y). Python’s x % y returns a result with the sign of y
instead, and may not be exactly computable for float arguments. For
example, fmod(-1e-100, 1e100) is -1e-100, but the result of Python’s
-1e-100 % 1e100 is 1e100-1e-100, which cannot be represented exactly as a float, and rounds to the surprising 1e100. For this reason,
function fmod() is generally preferred when working with floats, while
Python’s x % y is preferred when working with integers.
Same thing you'd expect from normal modulo .. e.g. 7 % 4 = 3, 7.3 % 4.0 = 3.3
Beware of floating point accuracy issues.
same as a normal modulo 3.14 % 6.28 = 3.14, just like 3.14%4 =3.14 3.14%2 = 1.14 (the remainder...)
you should use fmod(a,b)
While abs(x%y) < abs(y) is true mathematically, for floats it may not be true numerically due to roundoff.
For example, and assuming a platform on which a Python float is an IEEE 754 double-precision number, in order that -1e-100 % 1e100 have the same sign as 1e100, the computed result is -1e-100 + 1e100, which is numerically exactly equal to 1e100.
Function fmod() in the math module returns a result whose sign matches the sign of the first argument instead, and so returns -1e-100 in this case. Which approach is more appropriate depends on the application.
where x = a%b is used for integer modulo
I am trying this in Python 2.7 Interpreter.
>>> print 1/10
0
>>> print float(1/10)
0.0
>>> print 1/5
0
>>> a = 1/5
>>> a
0
>>> float(a)
0.0
>>>
I want a value in floating point when I divide. Any idea on the logic behind this zero out put and please let me know how to do it correctly.
The float conversion is happening after the integer division. Try this:
>>> print(1.0/10)
0.1
In Python 2.x, by default / means integer (truncating) division when used with two integer arguments. In Python 3.x, / always means float division, and // always means integer division. You can get the Python 3.x behaviour in 2.x using:
from __future__ import division
See PEP 238 for a full discussion of the division operators.
You need to do something like:
print (1.0/10)
When you do:
print 1/10
It is evaluating it as 0 because it is treating it as an integer.
And when you do:
print float(1/10)
You take the stuff inside the float() and evalute that first so in essence you get:
print float(0)
Which would be 0.0
The integer division 1/10 returns an integer 0. Converting integer 0 to float, just returns 0.0. What you need to do is divide float and an integer to get a float. This will work.
>>> 1/10.0
0.1
or
>>> 1.0/10
0.1
Result of integer division is integer.
From python docs
The / (division) and // (floor division) operators yield the quotient
of their arguments. The numeric arguments are first converted to a
common type. Plain or long integer division yields an integer of the
same type; the result is that of mathematical division with the
‘floor’ function applied to the result
Try
>>> 1.0/5
0.20000000000000001
>>> 1/5.0
0.20000000000000001
>>>
You want to do the below:
(float (1)/10)
Division in Python 2 between integers returns an integer result. (In Python 3, it returns a float result as you might expect.)
To get a float result, convert at least one of the integers into a float:
>>> print(1/10)
0
>>> print(1/float(10))
0.1
>>> print(float(1)/10)
0.1
>>> print(float(1)/float(10))
0.1
Why So ?
>>> round(2/3)
0.0
>>> round(0.66666666666666666666666666666667)
1.0
>>> round(2.0/3)
1.0
That's not strange behaviour from round():
Try this:
>>> 2/3
0
Using / with two integer values will do an integer division. So the argument to round() is already 0, which makes round() return 0.
Update: as #Mark noted in the comment, this behaviour changed in Python 3: 2/3 will do a floating point division as 2.0/3 does in Python 2. 2//3 can be used to get integer division behaviour on both versions).
Your last example works, because 2.0 is not integer, so 2.0/3 will do a "propper" floating point division:
>>> 2.0/3
0.6666666666666666