Related
I try get ration of variable and get unexpected result. Can somebody explain this?
>>> value = 3.2
>>> ratios = value.as_integer_ratio()
>>> ratios
(3602879701896397, 1125899906842624)
>>> ratios[0] / ratios[1]
3.2
I using python 3.3
But I think that (16, 5) is much better solution
And why it correct for 2.5
>>> value = 2.5
>>> value.as_integer_ratio()
(5, 2)
Use the fractions module to simplify fractions:
>>> from fractions import Fraction
>>> Fraction(3.2)
Fraction(3602879701896397, 1125899906842624)
>>> Fraction(3.2).limit_denominator()
Fraction(16, 5)
From the Fraction.limit_denominator() function:
Finds and returns the closest Fraction to self that has denominator at most max_denominator. This method is useful for finding rational approximations to a given floating-point number
Floating point numbers are limited in precision and cannot represent many numbers exactly; what you see is a rounded representation, but the real number is:
>>> format(3.2, '.50f')
'3.20000000000000017763568394002504646778106689453125'
because a floating point number is represented as a sum of binary fractions; 1/5 can only be represented by adding up 1/8 + 1/16 + 1/128 + more binary fractions for increasing exponents of two.
It's not 16/5 because 3.2 isn't 3.2 exactly... it's a floating point rough approximation of it... eg: 3.20000000000000017764
While using the fractions module, it is better to provide a string instead of a float to avoid floating point representation issues.
For example, if you pass '3.2' instead of 3.2 you get your desired result:
In : fractions.Fraction('3.2')
Out: Fraction(16, 5)
If you already have the value stored in a variable, you can use string formatting as well.
In : value = 3.2
In : fractions.Fraction(f'{value:.2f}')
Out: Fraction(16, 5)
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.
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.
I try get ration of variable and get unexpected result. Can somebody explain this?
>>> value = 3.2
>>> ratios = value.as_integer_ratio()
>>> ratios
(3602879701896397, 1125899906842624)
>>> ratios[0] / ratios[1]
3.2
I using python 3.3
But I think that (16, 5) is much better solution
And why it correct for 2.5
>>> value = 2.5
>>> value.as_integer_ratio()
(5, 2)
Use the fractions module to simplify fractions:
>>> from fractions import Fraction
>>> Fraction(3.2)
Fraction(3602879701896397, 1125899906842624)
>>> Fraction(3.2).limit_denominator()
Fraction(16, 5)
From the Fraction.limit_denominator() function:
Finds and returns the closest Fraction to self that has denominator at most max_denominator. This method is useful for finding rational approximations to a given floating-point number
Floating point numbers are limited in precision and cannot represent many numbers exactly; what you see is a rounded representation, but the real number is:
>>> format(3.2, '.50f')
'3.20000000000000017763568394002504646778106689453125'
because a floating point number is represented as a sum of binary fractions; 1/5 can only be represented by adding up 1/8 + 1/16 + 1/128 + more binary fractions for increasing exponents of two.
It's not 16/5 because 3.2 isn't 3.2 exactly... it's a floating point rough approximation of it... eg: 3.20000000000000017764
While using the fractions module, it is better to provide a string instead of a float to avoid floating point representation issues.
For example, if you pass '3.2' instead of 3.2 you get your desired result:
In : fractions.Fraction('3.2')
Out: Fraction(16, 5)
If you already have the value stored in a variable, you can use string formatting as well.
In : value = 3.2
In : fractions.Fraction(f'{value:.2f}')
Out: Fraction(16, 5)
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