Jules
Jules

Reputation: 7766

how to change a positive number to negative and vice versa?

I'm thinking there must be a function to do both. I think fabs will do negative to positive.

But not sure about the other way around ?

Upvotes: 7

Views: 10277

Answers (2)

Alexander Kuzin
Alexander Kuzin

Reputation: 505

I would rather write like this:

num = -num;

This won't need a multiplication, just changing the sign.

Upvotes: 13

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726629

num *= -1 will change the sign from positive to negative and vice versa.

Upvotes: 16

Related Questions