Igor
Igor

Reputation: 6275

Where is isnan() in MSVC 2010?

I'm trying to write a program which uses std::isnan() with MSVC 2010. I include cmath but unfortunately the compiler returns the error:

isnan is not part of the std namespace

Does MSVC 2010 support this function from std (AKA C++11)?

Upvotes: 4

Views: 4060

Answers (2)

Jesper Juhl
Jesper Juhl

Reputation: 31459

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

Your problem is probably VS2010 which has very poor C++11 support. I'd recommend grabbing VS2015 which is much better in that regard.

As can be seen here VS2010 only has _isnan .

Upvotes: 5

txtechhelp
txtechhelp

Reputation: 6777

Unfortunately, Visual Studio's C++11 support hasn't been that complete until the 2015 version, so you won't be able to utilize the C++ std::isnan functionality. Interestingly, there is a C99 isnan macro, but it's implementation defined and VS 2010 does not seem to have any of those macros. Fortunately though, the MS line of compilers do have their MS specific _ versions of the _isnan functionality. So you could write your own isnan as such:

#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;
}

On my machine this produces the output:

1 is NOT NaN

1.#QNAN is NaN

Note that the _isnanf is for 64-bit applications and the _WIN64 macro might not necessarily be defined, so if you target 64 bit be sure to adjust that.

Hope that can help.

Upvotes: 3

Related Questions