In recent versions of Python (3 onwards), integer division (dividing two integers) yields a float instead of an integer. This behavior differs from earlier versions, which favored integer results for integer operands.
Consider the following division in Python 3:
>>> 2 / 2 1.0
This surprising result may cause confusion, especially if you're accustomed to older Python versions.
The rationale for this change is documented in PEP-238: Changing the Division Operator. The proposal aimed to:
This change has several implications for your Python code:
Example:
# Explicit floor division result = 2 // 2 # 1 # Float division result = 2 / 2 # 1.0
The above is the detailed content of Why Does Python 3 Perform Floating-Point Division Instead of Integer Division?. For more information, please follow other related articles on the PHP Chinese website!