首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

检查C++中的double(或float)是否为NaN

在C++中,您可以使用std::isnan函数来检查一个doublefloat是否为NaN。std::isnan函数是C++11中引入的,它位于<cmath>头文件中。以下是如何使用std::isnan函数的示例:

代码语言:cpp
复制
#include<iostream>
#include <cmath>

int main() {
    double num = 0.0 / 0.0; // 计算一个NaN值

    if (std::isnan(num)) {
        std::cout << "The number is NaN."<< std::endl;
    } else {
        std::cout << "The number is not NaN."<< std::endl;
    }

    return 0;
}

在这个示例中,我们尝试将0除以0,这将产生一个NaN值。然后,我们使用std::isnan函数检查num是否为NaN。如果是NaN,我们输出"The number is NaN.",否则输出"The number is not NaN."。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券