python计算算术表达式的怪异行为(5-1//2)
python's weird behaivor of evaluating the arithmetic expression(5-1//2)
print(5-1//2)
这个表达式给出结果 5
如何?
我的尝试:
x=5-1//2
x=5-1 #(-1//2 gives -1)
x=4
但是 python 说的是 5
怎么样?
The -
in 5-1//2
is a binary -
. What you have checked with -1//2
is a unary -
. These have different precedence. The way you separated 5-1//2
into 5+(-1//2)
is not how it is actually evaluated.
-- comment by MisterMiyagi,已更正数学并添加 link
print(5-1//2)
这个表达式给出结果 5
如何?
我的尝试:
x=5-1//2
x=5-1 #(-1//2 gives -1)
x=4
但是 python 说的是 5
怎么样?
The
-
in5-1//2
is a binary-
. What you have checked with-1//2
is a unary-
. These have different precedence. The way you separated5-1//2
into5+(-1//2)
is not how it is actually evaluated.
-- comment by MisterMiyagi,已更正数学并添加 link