Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Floor division return value
#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


Messages In This Thread
Floor division return value - by Chirumer - Nov-26-2020, 09:04 AM
RE: Floor division return value - by Axel_Erfurt - Nov-26-2020, 09:28 AM
RE: Floor division return value - by perfringo - Nov-26-2020, 09:29 AM
RE: Floor division return value - by Chirumer - Nov-26-2020, 10:12 AM
RE: Floor division return value - by perfringo - Nov-26-2020, 12:03 PM
RE: Floor division return value - by buran - Nov-26-2020, 09:33 AM
RE: Floor division return value - by DeaD_EyE - Nov-26-2020, 09:50 AM
RE: Floor division return value - by Chirumer - Nov-26-2020, 10:05 AM
RE: Floor division return value - by DeaD_EyE - Nov-26-2020, 02:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Get numpy ceil and floor value for nearest two decimals klllmmm 4 1,337 Jun-07-2023, 07:35 AM
Last Post: paul18fr
  Division questions Dionysis 5 1,101 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Floor approximation problem in algorithm gradlon93 3 994 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  Division by zero and value of argument Lawu 5 3,207 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Floor division problem with plotting x-axis tick labels Mark17 5 2,157 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 2,156 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
Photo Locate Noise floor level for a spectral data in Python Ranjan_Pal 1 3,100 Dec-19-2020, 10:04 AM
Last Post: Larz60+
  Integer division plozaq 2 2,012 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 5,104 Jun-12-2020, 01:52 PM
Last Post: dgrunwal
  mapping-camera-coordinates-to-a-2d-floor-plan fauveboyxuuki 0 2,577 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