Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 int division operator is returning a float?

In one of my assignments I came across a weird implementation, and I was curious if it's a bug or the designed behavior.

In Python 3, division by / returns a floating point number, and // means integer division and should return an integer. I've discovered though that if either of the values is a float when doing integer division, it will return a float.

Example:

# These all work as expected
10 / 2
-> 5.0
11 / 2
-> 5.5
10 // 2
-> 5
11 // 2
-> 5
# Here things start to get weird
10.0 // 2
-> 5.0
10 // 2.0
-> 5.0
11.0 // 2
-> 5.0

Is this supposed to behave this way? If so, why does it behave this way?

like image 448
McKayla Avatar asked Mar 14 '18 16:03

McKayla


People also ask

Why does division in Python return a float?

The floor division operator ( // ) is primarily used when you require an integer or need to return the smallest integer less than or equal to the input. If the operands are both integers, then the output will an integer. If either operand is a float then the output will be a float.

Does dividing give you a float Python?

As explained in the Python 3 documentation, dividing two int s in Python will result in a float . To get an integer result, use floor division, represented by the // operator.

Does division return float?

Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the 'floor' function applied to the result.

What is the integer division operator in Python 3?

In Python 3. x, slash operator ("/") does true division for all types including integers, and therefore, e.g. 3/2==1.5. The result is of a floating-point type even if both inputs are integers: 4 / 2 yields 2.0.


1 Answers

From PEP-238, which introduced the new division (emphasis mine):

Semantics of Floor Division

Floor division will be implemented in all the Python numeric types, and will have the semantics of:

a // b == floor(a/b)

except that the result type will be the common type into which a and b are coerced before the operation.

Specifically, if a and b are of the same type, a//b will be of that type too. If the inputs are of different types, they are first coerced to a common type using the same rules used for all other arithmetic operators.

In particular, if a and b are both ints or longs, the result has the same type and value as for classic division on these types (including the case of mixed input types; int//long and long//int will both return a long).

For floating point inputs, the result is a float. For example:

3.5//2.0 == 1.0

For complex numbers, // raises an exception, since floor() of a complex number is not allowed.

For user-defined classes and extension types, all semantics are up to the implementation of the class or type.

So yes, it is supposed to behave that way. "// means integer division and should return an integer" - not quite, it means floor division and should return something equal to an integer (you'd always expect (a // b).is_integer() to be true where either operand is a float).

like image 154
jonrsharpe Avatar answered Nov 13 '22 11:11

jonrsharpe