C++ 负无穷大
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20016600/
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
Negative infinity
提问by Setzer22
I'm trying to figure out how to assign the value of negative infinity to a float or double variable. It seems that including the standard library limits, I can get the infinity representation, and I know (quite surely) that adding a minus in front of that (-infinity) might result in the value I'm looking for in the IEEE754 floating point standard (as 0x7FFFFFFF might result on 0xFFFFFFFF), but I'm not even sure about that, not to mention other standards that might be out there (If there are any). I find it really, really unprofessional and implementation dependant.
我试图弄清楚如何将负无穷大的值分配给浮点或双变量。似乎包括标准库限制,我可以获得无穷大表示,并且我知道(非常肯定)在(-infinity)前面添加一个减号可能会导致我在 IEEE754 浮点数中寻找的值标准(因为 0x7FFFFFFF 可能会导致 0xFFFFFFFF),但我什至不确定,更不用说可能存在的其他标准(如果有的话)。我发现它真的非常不专业并且依赖于实施。
Is there a good way to get the value of negative infinity platform and implementation independently, of course, otherwise I might just as well use a #define, everybody likes preprocessing.
有没有什么好的方法可以独立获取负无穷平台和实现的值,当然,不然我还不如用一个#define,大家都喜欢预处理。
采纳答案by Sam
At least if std::numeric_limits::is_iec559(IEEE 754) is true (which guarantees, that std::numeric_limits::has_infinityis also true), you can express positive and negative infinity values the way you already stated.
至少如果std::numeric_limits::is_iec559(IEEE 754) 为真(这保证std::numeric_limits::has_infinity也为真),您可以按照您已经陈述的方式表达正无穷大值和负无穷大值。
Short explanation of IEEE 754-1985 infinity values from Wikipedia:
来自维基百科的 IEEE 754-1985 无穷大值的简短解释:
......snip......
The biased-exponent field is filled with all 1 bits to indicate either infinity or an invalid result of a computation.
Positive and negative infinity
Positive and negative infinity are represented thus:
sign = 0 for positive infinity, 1 for negative infinity. biased exponent = all 1 bits. fraction = all 0 bits.
......snip......
……剪……
偏置指数字段用全 1 位填充以指示无穷大或计算的无效结果。
正无穷大和负无穷大
正无穷大和负无穷大表示为:
sign = 0 for positive infinity, 1 for negative infinity. biased exponent = all 1 bits. fraction = all 0 bits.
……剪……
Assertions
断言
The following example will either work as expected, or cause a compile time error in case the target platform does not support IEEE 754 floats.
以下示例将按预期工作,或者在目标平台不支持 IEEE 754 浮点数的情况下导致编译时错误。
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <limits>
int main(void)
{
//Asserts floating point compatibility at compile time
static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 required");
//C99
float negative_infinity1 = -INFINITY;
float negative_infinity2 = -1 * INFINITY;
float negative_infinity3 = -std::numeric_limits<float>::infinity();
float negative_infinity4 = -1 * std::numeric_limits<float>::infinity();
assert(std::isinf(negative_infinity1) && negative_infinity1 < std::numeric_limits<float>::lowest());
assert(std::isinf(negative_infinity2) && negative_infinity2 < std::numeric_limits<float>::lowest());
assert(std::isinf(negative_infinity3) && negative_infinity3 < std::numeric_limits<float>::lowest());
assert(std::isinf(negative_infinity4) && negative_infinity4 < std::numeric_limits<float>::lowest());
return EXIT_SUCCESS;
}
回答by Ian Cook
If std::numeric_limits<double>::is_iec559
is true
then it should be safe to use -
如果std::numeric_limits<double>::is_iec559
是,true
那么使用应该是安全的 -
double negative_infinity = - std::numeric_limits<double>::infinity();
double negative_infinity = - std::numeric_limits<double>::infinity();
(IEC559 is the ISO equivalent of IEEE754)
(IEC559 是 IEEE754 的 ISO 等效标准)
If it's false
then there's a whole lot more work to do as I don't think the C++ standard gives you any help.
如果是这样,false
那么还有很多工作要做,因为我认为 C++ 标准不会为您提供任何帮助。
回答by kenba
I don't know what compiler your using, but you can use -std::numeric_limits<double>::infinity()
on gcc and MinGw see Infinity-and-NaN. Also I ran the following code on MSVC and it returned true:
我不知道您使用的是什么编译器,但是您可以-std::numeric_limits<double>::infinity()
在 gcc 和 MinGw 上使用,请参阅Infinity-and-NaN。此外,我在 MSVC 上运行了以下代码,它返回 true:
double infinity(std::numeric_limits<double>::infinity());
double neg_infinity(-std::numeric_limits<double>::infinity());
double lowest(std::numeric_limits<double>::lowest());
bool lower_than_lowest(neg_infinity < lowest);
std::cout << "lower_than_lowest: " << lower_than_lowest << std::endl;
However, it maybe worthwhile considering using lowest in your application instead of negative infinity as it's likely to result in a more portable solution.
但是,可能值得考虑在您的应用程序中使用最低而不是负无穷大,因为它可能会导致更便携的解决方案。