isn't there an operator in c to change the sign of a int float etc from negative to positive or vice versa?
Asked Answered
A

8

10

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

Apostrophe answered 8/1, 2010 at 5:51 Comment(2)
what if its already negative? they'll cancel out?Apostrophe
I don't know objective-c, but based on your question, there's probably a library function to get the absolute value ... mucking with the sign would be a code smell to me.Sporophore
C
26
float newValue = oldValue * -1;

or

float newValue = -(oldValue); //() aren't needed, I just use them out of habit
Cointon answered 8/1, 2010 at 5:53 Comment(1)
You'll get a entirely useless warning if you just use -oldValue, at least in MSVC.Sedberry
S
13

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

Singband answered 8/1, 2010 at 5:53 Comment(2)
@Windows programmer: "You should have stayed out of it like I did." THanks for that.Singband
This is the most elegant of all. Pretty self explanatory.Form
E
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.)

Eft answered 8/1, 2010 at 6:19 Comment(5)
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().Denumerable
@Nick: abs( ) doesn't trap on the iPhone; it will just return INT_MIN.Unknot
@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.Domett
@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.Denumerable
@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".Denumerable
D
9

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.

Denumerable answered 8/1, 2010 at 6:1 Comment(1)
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." en.wikipedia.org/wiki/Objective-CDenumerable
P
4

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

Popovich answered 8/1, 2010 at 5:53 Comment(0)
T
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!

Tremolo answered 4/10, 2010 at 21:43 Comment(0)
G
2

x = 0 - x;

? or do I miss the point?

Georgeanngeorgeanna answered 8/1, 2010 at 5:53 Comment(0)
K
-2

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.

Killiecrankie answered 20/10, 2013 at 14:51 Comment(2)
This is overly complex, and incorrect at the same time.Halfwit
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.Killiecrankie

© 2022 - 2024 — McMap. All rights reserved.