This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 6 years ago.
According to the Python tutorial here https://docs.python.org/3/tutorial/introduction.html
this gives a floating point number:
>>> 17 / 3 # classic division returns a float
5.666666666666667
However, when I try it I get:
>>> 17 / 3
5
How come? FWIW, I'm using Python 2.7.10 on the Mac.
That behaviour changed between Python 2.7 and 3.
The answer is in the URL:
https://docs.python.org/3/tutorial/introduction.html
https://docs.python.org/2.7/tutorial/introduction.html
The URL you posted is for Python 3, but you are using Python 2.7.
There's a dropdown at the top of the page. You can select 2.7 from there to get the documentation for Python 2.7:
>>> 17 / 3 # int / int -> int
5
Related
This question already has answers here:
Division in Python 3 gives different result than in Python 2
(3 answers)
Closed 1 year ago.
Can someone help me figure out why the same simple calculation gets different answers in python 2 and 3? The expression is (0.2**(-2)-1)**(1/2).
When I use python 2 in a Canopy IDE, I get 1.
When I use python 3 in google colab, I get 4.98.
In both cases I am literally running exactly the above expression. Any ideas?
Integer division works differently in Python 2 and 3.
For example (1/2) will return
0 in Python 2, and
0.5 (a float) in Python 3.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm new in python and I was typing float numbers in python2.7 shell when I figured this:
>>> 9.9
9.9000000000000004
>>> 9.9==_
True
>>> 9.9==9.90000004
False
>>> 7.7
7.7000000000000002
>>> 7.7==_
True
>>> 7.7==7.700000002
False
my question is, why does 9.9 became 9.900000000000004? Is it default?
Please let me know if this is a duplicate. thanks.
Generally it is due to the binary nature of the computers. This is not a Python specific issue. The fractional part cannot always be exactly represented in binary numbers. Read about IEEE 754 and check examples&solid explanation here.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I am running this code on Python (both 2.7, 3.x):
>>> 1.1 + 2.2
3.3000000000000003
>>> 1.1 + 2.3
3.4
Could someone explain how does it work and what is happening?
float in Python implementing double point precision. Unless a number has power two denominator, it cannot be represented exactly by Python, but only "approximately" - up to the 16-th digit. Thus number like: 1, 0.5, 0.25 can be represented exactly, but number like your case (3.3) can only be represented "approximately". Its all correct, up to the 16 digit, and then you get the last 3 there, which is incorrect.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
When I divide 1/5e-5 it gives the correct answer but 1/5e-6 gives an answer close to the correct one.
>>> 1/5e-5
20000.0
>>> 1/5e-6
199999.99999999997 (should be 200000)
>>>
How can I get it to show the exact value. I tried importing decimel, numpy and scipy but none of them change the result.
You can get decimal to do this properly -- You just have to avoid using floating point numbers along the way:
>>> import decimal
>>> decimal.Decimal('1') / (decimal.Decimal('5') / decimal.Decimal('1000000'))
Decimal('2E+5')
This question already has answers here:
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed 5 years ago.
I have python2.7 installed on ubuntu 16.04. It's showing strange behavior. If I check for (5/4), it will just display 1 as output.
for (6/7) it is showing 0
Can anyone help me to fix this?
Not a strange error at all. Python 2.7 does Integer division, so you will always get an integers as the result of the expressions you have named. You can view it this way if you want:
5/4=int(5/4)=int(1.25) = 1
And this is how you can understand the expression you have shown:
float(5/4)= float(int(5/4)) = float(1) = 1.0
you need to use floats:
5.0/4.0