问题描述
我发现了这一点:,但它似乎可以检查正无穷大或负无穷大。我只想检查一个值是否恰好等于负无穷大,换句话说就是log(0)
I found this: http://en.cppreference.com/w/cpp/numeric/math/isinf but it appears to check for either positive or negative infinity. I just want to check if a value is equal to exactly negative infinity, or in otherwords is log(0)
感谢您的回答!根据下面的响应,下面是一些代码,显示了有效的方法。
Thanks for answer! Based on response below, here is some code that shows what works.
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main()
{
double c = std::log(0.0);
auto result = c == - INFINITY;
cout << result << endl;
return 0;
}
推荐答案
x == -1.0 / 0.0
如果 x
为负无穷大。
如果您愿意包含 cmath
,则 x ==- INFINITY
更具可读性。
If you are willing to include cmath
, then x == - INFINITY
is more readable.
假定浮点类型映射到IEEE 754格式,则每种类型都有自己的无穷大。 1.0 / 0.0
是 double
的无穷大。 INFINITY
的类型无关紧要,因为常规算术转换将负责匹配<$ c的左侧和右侧的类型$ c> == 。
Assuming that floating-point types are mapped to IEEE 754 formats, then each of them has its own infinity. 1.0 / 0.0
is a double
infinity. It doesn't matter much the type of INFINITY
because "usual arithmetic conversions" will take care of matching the types of the left- and right-hand-side of ==
.
这篇关于检查double是否等于C ++中的负无穷大的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!