c 中是否有一个运算符可以将 int float 等的符号从负更改为正,反之亦然?

发布于 2024-08-17 07:50:06 字数 42 浏览 12 评论 0原文

试图找到绝对值,我认为有一种简单的方法可以用“~”或其他东西反转符号。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

翻了热茶 2024-08-24 07:50:06
float newValue = oldValue * -1;

或者

float newValue = -(oldValue); //() aren't needed, I just use them out of habit
float newValue = oldValue * -1;

or

float newValue = -(oldValue); //() aren't needed, I just use them out of habit
轮廓§ 2024-08-24 07:50:06

要反转符号,请在其前面加上减号。

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

沉睡月亮 2024-08-24 07:50:06

使用 - 进行简单的否定是有效的,但是大多数答案都忽略了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 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.)

红玫瑰 2024-08-24 07:50:06

一元否定运算符 -(expr) 正是您想要的。

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

另一方面,您提到的按位补码运算符 ~(expr) 会翻转输入中的所有位。

如果有帮助的话,许多绝对值实现在野外忽略的一个问题是,对给定固定大小的二进制补码整数类型的最大负值取反将会溢出。

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.

浮生未歇 2024-08-24 07:50:06

-x 将为您提供 x 的符号反转值。

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

下壹個目標 2024-08-24 07:50:06

棘手的话题。更改浮点数符号的直接​​方法是翻转变量最高有效位的值。其他答案中列出的符号由编译器决定,这通常很好,但并非总是如此。对于“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!

铁轨上的流浪者 2024-08-24 07:50:06

x = 0 - x;

?或者我错过了重点?

x = 0 - x;

? or do I miss the point?

反差帅 2024-08-24 07:50:06

这是一个蹩脚的解决方案,不确定这里发生了什么。请使用abs() 查看下面的答案,了解正确的答案。

尽管这是一个老问题,但也许这个答案会对你们中的一些人有所帮助。我想根据另一个结果(假设是布尔值 MustBeNegative)获得一个数字的正值或负值,我这样做的方式是:

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

该数字将根据“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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文