【问题标题】:Where is isnan() in MSVC 2010?MSVC 2010 中的 isnan() 在哪里?
【发布时间】:2016-11-21 08:34:42
【问题描述】:

我正在尝试编写一个在 MSVC 2010 中使用std::isnan() 的程序。我包含cmath 但不幸的是编译器返回错误:

isnan 不是 std 命名空间的一部分

MSVC 2010 是否支持 std (AKA C++11) 中的此功能?

【问题讨论】:

  • MSVC 2010 不是很抱怨 C++11。他们最合规的版本是 2015 年。
  • @NathanOliver,不幸的是,目前我被 2010 年困住了。关于修复的任何提示?
  • this 可能会有所帮助。可以使用 boost 吗?
  • x != x 怎么样?
  • 2010年还没有标准,所以叫_isnan

标签: c++ std


【解决方案1】:

std::isnan <cmath>http://en.cppreference.com/w/cpp/numeric/math/isnan

您的问题可能是 VS2010 对 C++11 的支持很差。我建议使用 VS2015,在这方面要好得多

可以看出hereVS2010只有_isnan

【讨论】:

  • 正如我上面所写的,现在我被 2010 年困住了。关于修复的任何提示?
  • 这并不能真正回答问题。根据您的链接,isnan 在 c++11 中并没有说明 msvc2010 拥有它。
  • 编辑了我的答案以添加指向显示 VS2010 只有 _isnan 的页面的链接。顺便提一句;这东西很容易用谷歌搜索。
【解决方案2】:

不幸的是,Visual Studio 的 C++11 支持直到 2015 版本才支持 complete,因此您将无法使用 C++ std::isnan 功能。有趣的是,有一个 C99 isnan 宏,但它的实现已定义,而 VS 2010 似乎没有任何这些宏。不过幸运的是,MS 系列编译器确实具有其特定于 MS 的 _ 版本的 _isnan 功能。所以你可以这样写自己的isnan

#include <iostream>
#include <cmath>
#include <cfloat>
#include <limits>

namespace non_std
{

    template < typename T >
    bool isnan(T val)
    {
        #if defined(_WIN64)
            // x64 version
            return _isnanf(val) != 0;
        #else
            return _isnan(val) != 0;
        #endif
    }

}

int main(int argc, char** argv)
{
    float value = 1.0f;
    std::cout << value << " is " <<
        (non_std::isnan(value) ? "NaN" : "NOT NaN") << std::endl;

    if (std::numeric_limits<float>::has_quiet_NaN) {
        value = std::numeric_limits<float>::quiet_NaN();
        std::cout << value << " is " <<
            (non_std::isnan(value) ? "NaN" : "NOT NaN") << std::endl;
    }

    return 0;
}

在我的机器上,这会产生输出:

1 不是 NaN

1.#QNAN 是 NaN

请注意,_isnanf 是针对 64 位应用程序的,_WIN64 宏可能不一定要定义,因此如果您以 64 位为目标,请务必进行调整。

希望能有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多