objective-c c 中是否没有运算符可以将 int float 等的符号从负更改为正,反之亦然?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2025788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
isn't there an operator in c to change the sign of a int float etc from negative to positive or vice versa?
提问by nickthedude
trying to find absolute value and i thought there was a simple way to just invert the sign with '~' or something.
试图找到绝对值,我认为有一种简单的方法可以用“~”或其他东西来反转符号。
回答by Beep beep
float newValue = oldValue * -1;
or
或者
float newValue = -(oldValue); //() aren't needed, I just use them out of habit
回答by Mitch Wheat
To invert the sign, put a minus in front of it.
要反转符号,请在其前面加一个减号。
回答by Quinn Taylor
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.)
对-作品的简单否定,但大多数答案都忽略了 OP 试图做绝对值的事实。为此,正确的工具是abs()整数和fabs()浮点数。代码将非常清晰,结果将是您所期望的。(编辑:请务必阅读这些工具的文档和错误。正如尼克指出的那样,否定最大的负数会abs()返回相同的负数。)
回答by Nick Guerrera
The unary negation operator -(expr)does exactly what you want.
一元否定运算符-(expr)正是您想要的。
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.
~(expr)另一方面,您提到的按位补码运算符会翻转输入中的所有位。
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.
如果有帮助,许多绝对值实现会忽略的一个问题是,否定给定固定大小的二进制补码整数类型的最负值将溢出。
回答by JPN
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:
棘手的话题。更改浮点数符号的直接方法是翻转变量最高有效位的值。其他答案中列出的符号受编译器的支配,这通常很好,但并非总是如此。对于“float x”,答案是:
*(unsigned int*)&x ^= (1<<31)
*(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:
如果您正在为 iPhone 之类的东西编写代码,它有一个 Cortex A8 处理器(或类似的处理器),那么在处理内部循环中的浮点数时,您希望避免双精度并避免条件语句。所以,你可以这样做:
*(unsigned int*)&x ^= ( (x<0) << 31 );
*(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!
这将在不使用分支指令的情况下将负数变为正数。如果这是在内部循环中,它将比在 iPhone 上使用另一种方法快 5 到 20 倍。如果它不在内部循环中,那么您可能不会太在意!
回答by Ignacio Vazquez-Abrams
-x will give you the sign-inverted value of x.
-x 将为您提供 x 的符号反转值。
回答by Mawg says reinstate Monica
x = 0 - x;
x = 0 - x;
? or do I miss the point?
? 还是我错过了重点?
回答by Matthijn
This is a crappy solution, not sure what happened here. See the answer below with abs() for the correct one.
这是一个糟糕的解决方案,不确定这里发生了什么。请参阅下面的 abs() 答案以获得正确的答案。
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:
尽管这是一个老问题,但也许这个答案会对你们中的一些人有所帮助。我想根据另一个结果(比如布尔值 mustBeNegative)获得一个数字的正值或负值,我这样做的方式是:
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.
该数字将是基于“mustBeNegative”值的正值或负值。当数字已经是负数时,它将变为正数,反之亦然。

