[英]How the (-) operator converting negative numbers to positive works?
Here is an example:这是一个例子:
def absolute(x):
if x >= 0:
return x
else:
return -x
print(absolute(3))
print(absolute(-119))
The Output: Output:
3
119
Now, how -119
became a positive number by just using the (-) subtraction operator?现在,仅使用 (-) 减法运算符,
-119
是如何变成正数的?
Update:更新:
For those who misunderstood my q,对于那些误解我的q的人,
So I'm repeat, How -119 turns to be 119 by using the (-) operator!所以我再说一遍,-119 如何通过使用 (-) 运算符变成 119! as in
return -x
in the code above.如上面代码中的
return -x
一样。 Nope, I know it would be a multiplication but how it is achieved by using (-) operator?不,我知道这将是一个乘法,但它是如何通过使用 (-) 运算符来实现的?
You seem to be confusing the binary operator x - y
aka "x subtract y" with the unary operator -x
aka "negate x".您似乎将二元运算符
x - y
又名“x 减 y”与一元运算符-x
又名“否定 x”混淆了。 They are different things.它们是不同的东西。
-x
returns the negation of x
. -x
返回x
的否定。 If x
is positive, -x
is negative, and vice versa.如果
x
为正,则-x
为负,反之亦然。
Unary -
is: https://docs.python.org/3/library/operator.html#operator.neg一元
-
是: https://docs.python.org/3/library/operator.html#operator.neg
Binary -
is: https://docs.python.org/3/library/operator.html#operator.sub二进制
-
是: https://docs.python.org/3/library/operator.html#operator.sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.