jordanv
jordanv

Reputation: 43

Converting a postive integer to a negative

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

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

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

Related Questions