9

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

nickthedude
  • 4,835
  • 8
  • 33
  • 50

8 Answers8

21
float newValue = oldValue * -1;

or

float newValue = -(oldValue); //() aren't needed, I just use them out of habit
Beep beep
  • 18,063
  • 12
  • 60
  • 76
13

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

Mitch Wheat
  • 280,588
  • 41
  • 444
  • 526
  • 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 Jan 08 '10 at 07:57
  • 4
    @Windows programmer: "You should have stayed out of it like I did." THanks for that. – Mitch Wheat Jan 08 '10 at 08:11
  • 1
    This is the most elegant of all. Pretty self explanatory. – Paul Moore Feb 02 '19 at 01:26
12

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
  • 43,807
  • 16
  • 109
  • 128
  • 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 Jan 08 '10 at 07:02
  • @Nick: `abs( )` doesn't trap on the iPhone; it will just return INT_MIN. – Stephen Canon Jan 08 '10 at 15:28
  • @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 Jan 08 '10 at 15:44
  • @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 Jan 09 '10 at 00:55
  • @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 Jan 09 '10 at 00:59
8

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
  • 2,519
  • 15
  • 14
  • 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 Jan 08 '10 at 06:55
4

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

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
4

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
  • 41
  • 1
1

x = 0 - x;

? or do I miss the point?

Mawg says reinstate Monica
  • 34,839
  • 92
  • 281
  • 509
-1

This is a crappy solution, not sure what happened here. See the answer below with abs() for the correct one.

Even though this is an old question, maybe this answer will help some of you out. I wanted to have a positive or negative value of a number based on another result (lets say a boolean mustBeNegative) the way I did this is:

int number = 7;
if(mustBeNegative)
{
      number = number * 1;
}
else
{
      number = number * -1;
}

The number will be its positive or negative value based on the "mustBeNegative" value. When number was already a negative number it will become positive and visa versa.

Matthijn
  • 3,242
  • 8
  • 41
  • 62
  • This is overly complex, and incorrect at the same time. – fishinear Nov 15 '16 at 13:13
  • Yeah, not sure what happened there. If someone asks, what is the thing on the internet you are most ashamed of? I'll point to this answer from now on. – Matthijn Nov 15 '16 at 14:11