Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Floor division return value
#1
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
Reply
#2
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 >>>
Reply
#3
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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'>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
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?
Reply
#7
(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"
Reply
#8
(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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get numpy ceil and floor value for nearest two decimals klllmmm 4 1,198 Jun-07-2023, 07:35 AM
Last Post: paul18fr
  Division questions Dionysis 5 990 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Floor approximation problem in algorithm gradlon93 3 901 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  Division by zero and value of argument Lawu 5 2,968 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Floor division problem with plotting x-axis tick labels Mark17 5 2,047 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 2,091 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
Photo Locate Noise floor level for a spectral data in Python Ranjan_Pal 1 2,991 Dec-19-2020, 10:04 AM
Last Post: Larz60+
  Integer division plozaq 2 1,938 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 4,873 Jun-12-2020, 01:52 PM
Last Post: dgrunwal
  mapping-camera-coordinates-to-a-2d-floor-plan fauveboyxuuki 0 2,493 Dec-10-2019, 10:34 PM
Last Post: fauveboyxuuki

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020