c 中是否有一个运算符可以将 int float 等的符号从负更改为正,反之亦然?
试图找到绝对值,我认为有一种简单的方法可以用“~”或其他东西反转符号。
trying to find absolute value and i thought there was a simple way to just invert the sign with '~' or something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
或者
or
要反转符号,请在其前面加上减号。
To invert the sign, put a minus in front of it.
使用
-
进行简单的否定是有效的,但是大多数答案都忽略了OP正在尝试执行绝对值的事实。为此,正确的工具是abs()
用于整数和fabs()
用于浮动。代码将非常清晰,结果将是您所期望的。 (编辑:请务必阅读这些工具的文档和错误。正如 Nick 指出的,使用abs()
对最大负数求反会返回相同的负数。)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 isabs()
for integers andfabs()
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 withabs()
returns the same negative number.)一元否定运算符
-(expr)
正是您想要的。另一方面,您提到的按位补码运算符
~(expr)
会翻转输入中的所有位。如果有帮助的话,许多绝对值实现在野外忽略的一个问题是,对给定固定大小的二进制补码整数类型的最大负值取反将会溢出。
The unary negation operator
-(expr)
does exactly what you want.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.
-x 将为您提供 x 的符号反转值。
-x will give you the sign-inverted value of x.
棘手的话题。更改浮点数符号的直接方法是翻转变量最高有效位的值。其他答案中列出的符号由编译器决定,这通常很好,但并非总是如此。对于“float x”,答案是:
*(unsigned int*)&x ^= (1<<31)
如果您正在为 iPhone 等具有 Cortex A8 的设备编写代码处理器(或类似的东西),您希望避免双打,并在内部循环中使用浮点数时避免条件语句。因此,您可以这样做:
*(unsigned int*)&x ^= ( (x<0) << 31 );
这将在不使用分支指令的情况下将负数变为正数。如果这是在内循环中,它会比在 iPhone 上使用另一种方法快 5 到 20 倍。如果它不在内部循环中,那么您可能不会太在意!
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!
x = 0 - x;
?或者我错过了重点?
x = 0 - x;
? or do I miss the point?
这是一个蹩脚的解决方案,不确定这里发生了什么。请使用abs() 查看下面的答案,了解正确的答案。
尽管这是一个老问题,但也许这个答案会对你们中的一些人有所帮助。我想根据另一个结果(假设是布尔值 MustBeNegative)获得一个数字的正值或负值,我这样做的方式是:
该数字将根据“mustBeNegative”值为其正值或负值。当数字已经是负数时,它将变成正数,反之亦然。
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:
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.