0

I am trying to check, if a variable is not a number. I tried to use std::isnan() for that.

Here's the code that I thought would work:

#include <iostream>
#include <cstddef>
#include <cmath>

int main()
{
    int num = 1;
    if (std::isnan(num))
    {
        std::cout << num << " is a Number" << std::endl;
    }
    else
    {
        std::cout << num << " is a Number" << std::endl;
    }
}

The error it is giving is the following:

"fpclassify": Ambiguous call of an overloaded function

I can't understand why that won't work as expected, seen on many websites as examples.

Here's also a screenshot of everything:

Screen

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Tobi Smith
  • 53
  • 2
  • 9
  • isnan works on floats or doubles, but it doesn't make any sense on integers which have no concept of NaN. – Vincent Fourmond Dec 14 '21 at 20:54
  • 1
    @VincentFourmond [`std::isnan()`](https://en.cppreference.com/w/cpp/numeric/math/isnan) is actually overloaded for integral types, and will internally call the `double` overload. – Remy Lebeau Dec 14 '21 at 20:56
  • 2
    @VincentFourmond Although, when I read the standard I interpreted it as if there should be overloads that cast to `double` for integral types. That's why I wrote a bug report on MSVC. It affects all functions using `fpclassify`, like `std::signbit` and `std::isnan` etc – Ted Lyngmo Dec 14 '21 at 20:56
  • 1
    What about [`std::isnan()/4`](https://en.cppreference.com/w/cpp/numeric/math/isnan)? – G.M. Dec 14 '21 at 20:58
  • @G.M. Yes and https://eel.is/c++draft/cmath.syn#2.2 – Ted Lyngmo Dec 14 '21 at 20:58
  • Is that an indication that OP isn't using C++11 ? – Vincent Fourmond Dec 14 '21 at 21:01
  • Thank you all. I am using C++ 20 – Tobi Smith Dec 14 '21 at 21:02
  • @VincentFourmond It's just a bug in MSVC - but when discussed at Microsoft's github repo, they labeled it _"LEWG issue needed"_ which makes me think they want to undo that part of the standard. It's still not fixed in VS2022 beta I just noticed. – Ted Lyngmo Dec 14 '21 at 21:05
  • @RemyLebeau I must admit, I have difficulty envisaging many situations in which an integral value, when converted to `double` will produce a value for which `std::isnan()` returns true. Let alone situations where such a result is *useful*. – Peter Dec 15 '21 at 05:36
  • @Peter regardless of what the overloads *return*, the fact is the overloads *exist* and should not be causing an ambiguity error. – Remy Lebeau Dec 15 '21 at 06:01

0 Answers0