Reputation: 43
I am new to programming and was curious to know why the following returns a negative integer:
-num.abs
My understanding is that abs returns the value as a positive number yet this will return any positive number into a negative?
Upvotes: 2
Views: 130
Reputation: 230286
why the following returns a negative integer:
-num.abs
Because it's -(num.abs)
, not (-num).abs
. First, you take abs
, then you negate that value, resulting in a negative number.
Upvotes: 5