How can i make -a into +a?

Printable View

- 12-27-2002krappykodernegative to posative
How can i make -a into +a?

- 12-27-2002lurker
Multiply by -1.

- 12-27-2002gamer4life687
Or you can use the absolute value function.

http://www.cprogramming.com/fod/abs.html - 12-27-2002moiQuote:

*Originally posted by gamer4life687*

**Or you can use the absolute value function.**

http://www.cprogramming.com/fod/abs.html

- 12-27-2002PJYelton
If you want to change the sign on any variable just do:

Code:`a=-a;`

- 12-27-2002gamer4life687Code:
`Originally posted by Moi`

that page is 100% wrong. there is no abs() which takes and returns an int

Its funny you say it doesnt work. I tested the exact program on that site and it works 100%!. Maybe your compiler doesnt have that. - 12-27-2002Travis DaneQuote:

*Originally posted by gamer4life687*

Its funny you say it doesnt work. I tested the exact program on that site and it works 100%!. Maybe your compiler doesnt have that. [/B]

mathematical questions - 12-27-2002moi
think before you speak

of course the program will work, ints and doubles can be implicitly converted around. that is not what i said though. - 12-27-2002quzah
Simply return 0 - x.

Quzah. - 12-27-2002OneStiffRod
To get the inverse of a number...

Code:`//To get the (-) negative equiv. of 100 (-100) just reverse the bits and add 1,`

//same for negative to positive.

int main(){

int i = 100;

**i = ~i + 1;**

cout<<i<<endl;

return 0;

}

- 12-27-2002moi
ahh, i see. there is an int abs (int), but its in <stdlib.h>, not <math.h>. therefore the flaw in the tutorial program is #including the wrong header file, because without <stdlib.h> int abs (int) is not in scope, and double abs (double) is used with implicit conversions (although if <stdlib.h> had been there it would use an overloaded abs() instead). happy now?

- 12-27-2002gamer4life687
Interesting what we can come up with with only one arguement. I sure hope that KrappyKoder responds to this and gets his answer. Now that we know how to do the change plz lets not argue anymore. I get the point.

- 12-27-2002Eibro
No, clearly the best solution is the following:

Code:`int a = -4394;`

for (int i = 0; i < a*-2; i++)

a++;

- 02-28-2003krappykodermeh
The method i ended up using was from a friend, playing with unsigned and signed