Problem with std::isnan() in combination with __float128

Hello,

I am using the GCC compiler's __float128, but I've encountered a problem. I have a class template in which __float128 is passed as a parameter, when I compile I get this error:

error: 'isnan' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]|
note: 'template<class _Tp> constexpr typename __gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value, bool>::__type std::isnan(_Tp)' declared here, later in the translation unit|
error: call of overloaded 'isnan(const __float128&)' is ambiguous|
note: candidate: 'constexpr bool std::isnan(float)'|
note: candidate: 'constexpr bool std::isnan(double)'|
note: candidate: 'constexpr bool std::isnan(long double)'|

isnan() does not seem to recognize __float128. There is no overloaded function for __float128. However, I found a way to get rid of the error messages by writing this global function:

bool isnan(__float128 x){return isnan((__float128)x);}

It helps to make my code compile again, but... is it ok? Have I done something that should have been solved differently? I am not sure if isnan() now handles __float128 correctly.
Have you tried calling the function after compiling? Looks like infinite recursion.

__float128 is not a standard type, so there are probably no guarantees that standard library functions like std::isnan will work correctly on it.

Search your compiler's documentation.
I found this PDF: https://gcc.gnu.org/onlinedocs/gcc-5.5.0/libquadmath.pdf
The version might be outdated, but it suggests using the function:
isnanq: check for not a number

https://github.com/gcc-mirror/gcc/blob/master/libquadmath/math/isnanq.c
Last edited on
Library support for __float128 comes from libquadmath. The function you'd want is <quadmath.h>'s isnanq().

Because GCC implements IEEE 754 binary128, you should also be able to implement it safely using bool my_isnan(__float128 x) { return x != x; }
Last edited on
Do you really need float128? Ordinary doubles can keep a whole lot of precision if you don't abuse them.
I've decided to choose the easy solution and use mbozzi's method:

bool isnan(__float128 x) { return x != x; }

I had to look it up on google as to why and how this would work. It looks weird but legit. Nice that there is an easy way.

I will also take a look at libquadmath. Thanks for the help guys.
Topic archived. No new replies allowed.