views:

519

answers:

7

trying to find absolute value and i thought there was a simple way to just invert the sign with '~' or something.

+7  A: 

To invert the sign, put a minus in front of it.

Mitch Wheat
Would the downvoter please leave a comment. Thanks.
Mitch Wheat
2 downvotes for no reason? Come on!
Mitch Wheat
The title asks for negation in C, the contents of the question ask for absolute value, and somewhere else the poster said Objective C instead of C. You can't expect your answer to satisfy everyone. Or anyone. Or even the original poster. You should have stayed out of it like I did.
Windows programmer
@Windows programmer: "You should have stayed out of it like I did." THanks for that.
Mitch Wheat
+1 Simple answer to a simple question.
Stephen Canon
+5  A: 
float newValue = oldValue * -1;

or

float newValue = -(oldValue); //() aren't needed, I just use them out of habit
Jess
thanks brain isn't working right now
nickthedude
@nick - no problem, it's late. =)
Jess
**Late? Where?**
pavium
ive been getting so into this book "programming in objective c" that im getting a little burned out but I'm so excited to get to the point where I'm ready to start building some iphone stuff it just pumps me up. I'm really glad to have found this resource and hope it gives me the push i need to finally get a grip on programming. Ive tried about a million times to teach myself but always got stuck somewhere and frustrated so i stopped. this site is rad because everyone here is helping me, so cool! I hope i get good enough to help out here as well.Thanks everyone,Nick
nickthedude
+4  A: 

-x will give you the sign-inverted value of x.

Ignacio Vazquez-Abrams
+1  A: 

x = 0 - x;

? or do I miss the point?

Mawg
+5  A: 

The unary negation operator -(expr) does exactly what you want.

int x = -7;
int y = 7;
x = -x; // x is now 7
y = -y; // y is now -7

The bitwise complement operator ~(expr) that you mention, on the other hand, flips all of the bits in the input.

In case it helps, one issue that many absolute value implementations in the wild ignore is that negating the most negative value of a given fixed-size two's complement integer type will overflow.

Nick Guerrera
nice answer ...
Jess
I just saw that this is tagged objective-c. I had originally anwered with respect to the title, asking for an operator "in C". Thankfully, "Objective-C is a thin layer on top of C, and moreover is a strict superset of C. It is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class." http://en.wikipedia.org/wiki/Objective-C
Nick Guerrera
+5  A: 

Simple negation with - works, but most of the answers have ignored the fact that the OP is trying to do absolute value. For that, the correct tool is abs() for integers and fabs() for floats. The code will be crystal clear and the result will be what you expect. (Edit: Be sure to read the documentation and bugs for these tools. As Nick points out, negating the most negative number with abs() returns the same negative number.)

Quinn Taylor
In principle, I agree and I've up-voted your answer. Of course, one might reasonably expect the program to be terminated, or an exception to be raised, when trying to negate the most negative integer. See BUGS in linked documentation for abs().
Nick Guerrera
@Nick: `abs( )` doesn't trap on the iPhone; it will just return INT_MIN.
Stephen Canon
@Nick: That's not in the C spirit. As Stroustrup said in a similar case for C++, the language should provide the fast operator, and perhaps the checked operator. If the language provides the fast operator, the user can insert checks as needed, while if the language only provides the checked operator the user has no way of discarding the checks when unnecessary and doing it fast. You can always write `int checked_abs(int)` if you like.
David Thornley
@David: I don't disagree. My point was just that you might have a legitimate reason to write your own abs(): to trap this error.
Nick Guerrera
@Stephen: I think you misread my comment. I was saying that although abs() does not trap, a user might expect that it does. This was in regards to the statement that "the result will be what you expect".
Nick Guerrera
A: 

Tricky subject. The direct way to change the sign on a floating point number is to flip the value of the variable's most significant bit. The notation listed in the other answers is at the mercy of the compiler, which is usually fine, but not always. For "float x", the answer is:

*(unsigned int*)&x ^= (1<<31)

If you are writing for something like the iPhone, which has a Cortex A8 processor (or something like it), you want to avoid doubles and also avoid conditionals when working with floats in inner loops. So, you can do this:

*(unsigned int*)&x ^= ( (x<0) << 31 );

Which will turn negatives to positives without using a branch instruction. If this is in an inner loop, it will be 5 to 20 times faster than using another method, on the iPhone. If it's not in an inner loop, then you probably don't care too much!

JPN