Python Forum
Floor division return value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Floor division return value (/thread-31172.html)



Floor division return value - Chirumer - Nov-26-2020

10//4 evaluates to 2.0

Why is it evaluated as 2.0 and not just 2? The result is an integer after all, isn't it better to store an integer in an int? Why is it stored as a floating when the fractional part is truncated off anyway?

EDIT: sorry, I meant to write 10//4.0 instead of 10//4


RE: Floor division return value - Axel_Erfurt - Nov-26-2020

I get 2

Output:
axel@Esprimo-P400:/tmp$ python3 Python 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 10//4 2 >>>
Output:
brian@Esprimo-P400:/tmp$ python Python 2.7.18 (default, Aug 4 2020, 11:16:42) [GCC 9.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 10//4 2 >>>



RE: Floor division return value - perfringo - Nov-26-2020

You have changed your original post but your claim was that in Python 3 floor division returns float and Python 2 floor division returns integer.

Quote:Better end this dream before it becomes a nightmare
--Rachel Cohn

Let's do reality check with Python 3:

>>> import sys
>>> sys.version
'3.9.0 (v3.9.0:9cf6752276, Oct  5 2020, 11:29:23) \n[Clang 6.0 (clang-600.0.57)]'
>>> 10 // 4
2
With Python 2

>>> import sys
>>> sys.version
'2.7.16 (default, Oct 30 2020, 02:15:49) \n[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri'
>>> 10 // 4
2
EDIT: if interested one can read PEP 238 Changing the Division Operator > Semantics of Floor Division:

Quote: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.

So if one of the operands is a float then indeed the floor division returns a float:

>>> 10.0 // 4
2.0
>>> 10 // 4.0
2.0



RE: Floor division return value - buran - Nov-26-2020

what version do you use?
Python 3.7.9 (default, Aug 18 2020, 06:24:24) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 10//4
2
>>> type(10//4)
<class 'int'>



RE: Floor division return value - DeaD_EyE - Nov-26-2020

Since Python 3 the // is the integer division. It returns a number without remainder.
Depending on the datatype the result could be an int or float.

In [1]: 5 // 2
Out[1]: 2

In [2]: 5 // 2.0
Out[2]: 2.0

In [3]: 5.0 // 2
Out[3]: 2.0

In [4]: 5.0 / 2
Out[4]: 2.5

In [5]: 5 / 2.0
Out[5]: 2.5

In [6]: 5 / 2
Out[6]: 2.5

In [7]: 2 // 1
Out[7]: 2



RE: Floor division return value - Chirumer - Nov-26-2020

I'm very very sorry. I meant to write 10//4.0
Why does 10//4.0 return 2.0 and not simply 2?

The fractional part is truncated anyway, so why does 10//4.0 evaluate to a float and not an int?


RE: Floor division return value - Chirumer - Nov-26-2020

(Nov-26-2020, 09:29 AM)perfringo Wrote: You have changed your original post but your claim was that in Python 3 floor division returns float and Python 2 floor division returns integer.

Quote:Better end this dream before it becomes a nightmare
--Rachel Cohn

Let's do reality check with Python 3:

>>> import sys
>>> sys.version
'3.9.0 (v3.9.0:9cf6752276, Oct  5 2020, 11:29:23) \n[Clang 6.0 (clang-600.0.57)]'
>>> 10 // 4
2
With Python 2

>>> import sys
>>> sys.version
'2.7.16 (default, Oct 30 2020, 02:15:49) \n[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri'
>>> 10 // 4
2
EDIT: if interested one can read PEP 238 Changing the Division Operator > Semantics of Floor Division:

Quote: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.

So if one of the operands is a float then indeed the floor division returns a float:

>>> 10.0 // 4
2.0
>>> 10 // 4.0
2.0

Yes, I had changed the question.
A book I'm referring showed 10//4.0 to evaluate to 2 in python2.6 and 2.0 in python3 in an example. However, I tried it in an online editor to find that 10//4.0 evaluated to 2.0 in python2.6 as well, the book must have made a typo.

So I changed the question from "why // changed" to "why // works the way it does"


RE: Floor division return value - perfringo - Nov-26-2020

(Nov-26-2020, 10:12 AM)Chirumer Wrote: So I changed the question from "why // changed" to "why // works the way it does"

If you follow the documentation then you find:

- Documentation > The Python Language Reference > 6. Expressions > 6.1. Arithmetic Conversions:

Quote:When a description of an arithmetic operator below uses the phrase “the numeric arguments are converted to a common type”, this means that the operator implementation for built-in types works as follows:

  • If either argument is a complex number, the other is converted to complex;
  • otherwise, if either argument is a floating point number, the other is converted to floating point;
  • otherwise, both must be integers and no conversion is necessary.
Some additional rules apply for certain operators (e.g., a string as a left argument to the ‘%’ operator). Extensions must define their own conversion behavior.

On the same page, under 6.7. Binary arithmetic operations you will find:

Quote:The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. 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. Division by zero raises the ZeroDivisionError exception.

It is good practice to read documentation. Even more, if you follow python.org advice then you even keep some stuff under the pillow (documentation)


RE: Floor division return value - DeaD_EyE - Nov-26-2020

Quote:Why does 10//4.0 return 2.0 and not simply 2?

Because one of the data types is a float. In your case, the 4.0.
Then a float is returned, but the reminder not. You don't get a fraction back.
You can check the float object if it represents an integer:

value = 1.0
print(value.is_integer())

other_value = 1.1
print(other_value.is_integer())
The first one is True and the second is False.
But with the floor division, you always lose the rest/fraction.


If you insist on getting always an int as result, then you could make a helper function:
def floordiv_int(x, y):
    return int(x // y)


result = floordiv_int(10, 4.0)
print(result)
Output:
2