Python 3 int 除法运算符返回的是 float?
Python 3 int division operator is returning a float?
在我的一项作业中,我遇到了一个奇怪的实现,我很好奇它是错误还是设计行为。
在Python3中,除以/
,return是一个浮点数,//
是整数除法,应该return是一个整数。我发现,如果在进行整数除法时,如果其中一个值是浮点数,它将 return 一个浮点数。
示例:
# These all work as expected
10 / 2
-> 5.0
11 / 2
-> 5.5
10 // 2
-> 5
11 // 2
-> 5
# Here things start to get weird
10.0 // 2
-> 5.0
10 // 2.0
-> 5.0
11.0 // 2
-> 5.0
这应该这样做吗?如果是这样,为什么它会这样?
来自 PEP-238,介绍了新部门(强调我的):
Semantics of Floor Division
Floor division will be implemented in all the Python numeric types,
and will have the semantics of:
a // b == floor(a/b)
except that the result type will be the common type into which a and
b are coerced before the operation.
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.
In particular, if a and b are both ints or longs, the result has the
same type and value as for classic division on these types (including
the case of mixed input types; int//long
and long//int
will both
return a long).
For floating point inputs, the result is a float. For example:
3.5//2.0 == 1.0
For complex numbers, //
raises an exception, since floor()
of a
complex number is not allowed.
For user-defined classes and extension types, all semantics are up to
the implementation of the class or type.
所以是的,它应该以这种方式运行。 "//
表示整数除法并且应该 return 一个整数" - 不完全是,它表示 整除 并且应该 return 某物 等于 一个整数(您总是期望 (a // b).is_integer()
在任一操作数是浮点数的情况下为真)。
我希望这能澄清情况:
/ Division
Divides left hand operand by right hand operand
b / a = 2
// Floor Division
The division of operands where the result is the quotient in which the
digits after the decimal point are removed. But if one of the operands
is negative, the result is floored, i.e., rounded away from zero
(towards negative infinity)
9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
https://www.tutorialspoint.com/python/python_basic_operators.htm
在我的一项作业中,我遇到了一个奇怪的实现,我很好奇它是错误还是设计行为。
在Python3中,除以/
,return是一个浮点数,//
是整数除法,应该return是一个整数。我发现,如果在进行整数除法时,如果其中一个值是浮点数,它将 return 一个浮点数。
示例:
# These all work as expected
10 / 2
-> 5.0
11 / 2
-> 5.5
10 // 2
-> 5
11 // 2
-> 5
# Here things start to get weird
10.0 // 2
-> 5.0
10 // 2.0
-> 5.0
11.0 // 2
-> 5.0
这应该这样做吗?如果是这样,为什么它会这样?
来自 PEP-238,介绍了新部门(强调我的):
Semantics of Floor Division
Floor division will be implemented in all the Python numeric types, and will have the semantics of:
a // b == floor(a/b)
except that the result type will be the common type into which a and b are coerced before the operation.
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.In particular, if a and b are both ints or longs, the result has the same type and value as for classic division on these types (including the case of mixed input types;
int//long
andlong//int
will both return a long).For floating point inputs, the result is a float. For example:
3.5//2.0 == 1.0
For complex numbers,
//
raises an exception, sincefloor()
of a complex number is not allowed.For user-defined classes and extension types, all semantics are up to the implementation of the class or type.
所以是的,它应该以这种方式运行。 "//
表示整数除法并且应该 return 一个整数" - 不完全是,它表示 整除 并且应该 return 某物 等于 一个整数(您总是期望 (a // b).is_integer()
在任一操作数是浮点数的情况下为真)。
我希望这能澄清情况:
/ Division
Divides left hand operand by right hand operand
b / a = 2
// Floor Division
The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity)
9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
https://www.tutorialspoint.com/python/python_basic_operators.htm