检查double是否等于C ++中的负无穷大的最佳方法 [英] Best way to check if double equals negative infinity in C++

查看:472
本文介绍了检查double是否等于C ++中的负无穷大的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这一点: http://en.cppreference.com/w/ cpp / numeric / math / isinf ,但它似乎可以检查正无穷大或负无穷大。我只想检查一个值是否恰好等于负无穷大,换句话说就是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 ++中的负无穷大的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆