区分0和- 0

时间:2022-09-08 13:26:25

I have run into a situation in my code where a function returns a double, and it is possible for this double to be a zero, a negative zero, or another value entirely. I need to distinguish between zero and negative zero, but the default double comparison does not. Due to the format of doubles, C++ does not allow for comparison of doubles using bitwise operators, so I am unsure how to procede. How can I distinguish between the two?

在我的代码中,我遇到了一个函数返回一个double的情况,这可能是一个零,一个负数,或者一个完全的值。我需要区分0和- 0,但是默认的双比较没有。由于双精度操作的格式,c++不允许使用位操作符对双精度操作进行比较,所以我不确定如何操作。我如何区分这两者?

3 个解决方案

#1


15  

Call std::signbit() to determine the state of the sign bit.

调用std::signbit()来确定符号位的状态。

#2


6  

Due to the format of doubles, C++ does not allow for comparison of doubles using bitwise operators, so I am unsure how to procede.

由于双精度操作的格式,c++不允许使用位操作符对双精度操作进行比较,所以我不确定如何操作。

First off, C++ doesn't mandate any standard for float/double types.

首先,c++不强制任何浮动/双类型的标准。

Assuming you're talking about IEEE 754's binary32 and binary64 formats, they're specifically designed to maintain their order when their bit patterns are interpreted as integers so that a non-FPU can sort them; this is the reason they have a biased exponent.

假设你说的是IEEE 754的binary32和binary64格式,它们是专门设计来保持它们的顺序的,当它们的位模式被解释为整数时,非fpu可以对它们进行排序;这就是它们有偏指数的原因。

There're many SO posts discussing such comparisons; here's the most relevant one. A simple check would be

有很多这样的帖子讨论这种比较;这是最相关的一个。一个简单的支票就是

bool is_negative_zero(float val)
{
   return ((val == 0.0f) && std::signbit(val));
}

This works since 0.0f == -0.0f, although there're places where the sign makes a difference like atan2 or when dividing by -0 as opposed to +0 leads to the respective infinities.

这是在0。0f == -0.0f的情况下进行的,尽管有些地方的符号会像atan2那样产生差异,或者当除以-0时,而不是+0会导致各自的无穷大。

#3


0  

To test explicitly for a == -0 in C do the following:

要在C中显式地测试a = -0,请执行以下操作:

if (*((long *)&a) == 0x8000000000000000) {
    //  a is -0
}

#1


15  

Call std::signbit() to determine the state of the sign bit.

调用std::signbit()来确定符号位的状态。

#2


6  

Due to the format of doubles, C++ does not allow for comparison of doubles using bitwise operators, so I am unsure how to procede.

由于双精度操作的格式,c++不允许使用位操作符对双精度操作进行比较,所以我不确定如何操作。

First off, C++ doesn't mandate any standard for float/double types.

首先,c++不强制任何浮动/双类型的标准。

Assuming you're talking about IEEE 754's binary32 and binary64 formats, they're specifically designed to maintain their order when their bit patterns are interpreted as integers so that a non-FPU can sort them; this is the reason they have a biased exponent.

假设你说的是IEEE 754的binary32和binary64格式,它们是专门设计来保持它们的顺序的,当它们的位模式被解释为整数时,非fpu可以对它们进行排序;这就是它们有偏指数的原因。

There're many SO posts discussing such comparisons; here's the most relevant one. A simple check would be

有很多这样的帖子讨论这种比较;这是最相关的一个。一个简单的支票就是

bool is_negative_zero(float val)
{
   return ((val == 0.0f) && std::signbit(val));
}

This works since 0.0f == -0.0f, although there're places where the sign makes a difference like atan2 or when dividing by -0 as opposed to +0 leads to the respective infinities.

这是在0。0f == -0.0f的情况下进行的,尽管有些地方的符号会像atan2那样产生差异,或者当除以-0时,而不是+0会导致各自的无穷大。

#3


0  

To test explicitly for a == -0 in C do the following:

要在C中显式地测试a = -0,请执行以下操作:

if (*((long *)&a) == 0x8000000000000000) {
    //  a is -0
}