Why Doesn't This Division Work in Python

Why doesn't this division work in Python?

Until version 3, Python's division operator, /, behaved like C's division operator when presented with two integer arguments: it returns an integer result that's truncated down when there would be a fractional part. See: PEP 238

>>> n = 29
>>> d = 1009
>>> print str(float(n)/d)
0.0287413280476

In Python 2 (and maybe earlier) you could use:

>>> from __future__ import division
>>> n/d
0.028741328047571853

Why is this division not performed correctly?

The above behavior is true for Python 2. The behavior of / was fixed in Python 3. In Python 2 you can use:

from __future__ import division

and then use / to get the result you desire.

>>> 5 / 2
2
>>> from __future__ import division
>>> 5 / 2
2.5

Since you are dividing two integers, you get the result as an integer.

Or, change one of the numbers to a float.

>>> 5.0 / 2
2.5

Why does the division get rounded to an integer?

You're using Python 2.x, where integer divisions will truncate instead of becoming a floating point number.

>>> 1 / 2
0

You should make one of them a float:

>>> float(10 - 20) / (100 - 10)
-0.1111111111111111

or from __future__ import division, which the forces / to adopt Python 3.x's behavior that always returns a float.

>>> from __future__ import division
>>> (10 - 20) / (100 - 10)
-0.1111111111111111

Wrong dividing in python

The division is not "wrong". It is integer division (a.k.a. floor division).

When you divide two integers, the result is an integer:

>>> 3/4
0
>>> 4/4
1

When you divide two floating-point numbers (numbers with a fractional part), the result is a float:

>>> 3./4
0.75
>>> 4./4
1.0

Note that this "problem" is confined to Python 2. One of the changes in Python 3 is to make normal division coerce to floats:

>>> 3/4   # Python 3 behavior
0.75

and to require a second operator (also in Python >2.2) to achieve integer division:

>>> 3//4
0

Why does Python return 0 for simple division calculation?

In Python 2, 25/100 is zero when performing an integer divison. since the result is less than 1.

You can "fix" this by adding from __future__ import division to your script. This will always perform a float division when using the / operator and use // for integer division.

Another option would be making at least one of the operands a float, e.g. 25.0/100.

In Python 3, 25/100 is always 0.25.

Why does integer division yield a float instead of another integer?

Take a look at PEP-238: Changing the Division Operator

The // operator will be available to request floor division unambiguously.

Why does this line of code work in python?

// performs a Floor Division.

Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.

So in your example, it is taking the number 2374 and dividing it by 10.

This returns 237.4

The 237.4 is then floored. Resulting in 237.0, or just 237.

Division Mistake in Python

Is it truly an Integer?

Oddly, my Python gives the correct answer, and not an e+16 number. Maybe my Python version is different.

This is not a mistake, this is how Python (and most programming languages) store numbers (or floats).

Your 'big' value is not an Integer. It's an 'Any'. It could be anything and store anything. You can reassign it to a string if you wish. If you force it to become an Int, it will check if it can be an Int. But it doesn't really turn it into an Int.


What really are Floats?

Floating Point Numbers are not precise. Division becomes very weird. Sometimes, they can even be infinity or nan. They use a certain amount of bits for the numeric value, and another certain amount of bits for its magnitude: e+16.

1.0415037564023284e+16 == 10.0415037564023284e+15
(Integers don't ever do this. They don't have magnitude. They are numeric only.)

Division in Python is tricky. If you have an Int value, the "/" method will always return a Float (That is, it will always return a numeric trait and a magnitude trait).


Final Answer

• "//" Operator

The // Operator will always only return a perfect numeric trait (and never a magnitude e+ trait).



Related Topics



Leave a reply



Submit