This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Issue with Python’s Floor Division
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, rhettinger, yao_way
Priority: normal Keywords:

Created on 2020-12-30 17:31 by yao_way, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg384073 - (view) Author: yao_way (yao_way) Date: 2020-12-30 17:31
There might be an issue with Python’s floor division:
Experienced Behavior: >>> (1 / 1) // 1 --> 1.0; >>> (0 / 1) // 1 --> 0.0
Expected Behavior: >>> (1 / 1) // 1 --> 1; >>> (0 / 1) // 1 --> 0
msg384076 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-12-30 17:43
The current behavior was specified in PEP 238:

   int / int -> float            
   float // int -> float 

See:  https://www.python.org/dev/peps/pep-0238/#semantics-of-floor-division
msg384078 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-12-30 17:46
Regular division (/) yields a float, so 1 / 1 is 1.0.

https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations says that for floor division, the arguments are first converted to a common type, which here would be float.

So, your examples are basically:

>>> 1 / 1
1.0
>>> 1.0 // 1
1.0

>>> 0 / 1
0.0
>>> 0.0 // 1
0.0

This is working as expected, and is not a bug.
msg384079 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-12-30 17:46
Oops, my message collided with rhettinger.
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86954
2020-12-30 17:46:52eric.smithsetnosy: + rhettinger
messages: + msg384079
2020-12-30 17:46:13eric.smithsetnosy: + eric.smith, - rhettinger
messages: + msg384078
2020-12-30 17:43:51rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg384076

resolution: not a bug
stage: resolved
2020-12-30 17:31:26yao_waycreate