Skip to content

Cross vendor ``isnan``

Mayeul d'Avezac edited this page Apr 11, 2014 · 6 revisions

Each and every vendor provides a different isnan. There is a script to help define a portable c++ macro. It is meant to be used within a configuration file as follows:

include("path/to/cookoff/CheckIsNaN.cmake")
if(NOT ISNAN_VARIATION)
  message(STATUS "Could not find working isnan.")
endif(NOT ISNAN_VARIATION)

configure_file(/path/to/config.h.in /path/to/config.h)

Two cmake variables are defined:

  • ISNAN_HEADERS will the header(s) relevant to the local isnan definition
  • ISNAN_VARIATIOPN is the fully qualified name to the local isnan definition

They can be used as follows in a the configuration file config.h.in:

@ISNAN_HEADERS@

#define not_a_number(X) @ISNAN_VARIATION@

One should then use the macro not_a_number in-place of any isnan flavour.

In c++11, it is also possible to define a function that takes only arithmetic type, thus obviating the need for a macro:

@ISNAN_HEADERS@
#include <type_traits>

template<class T>
  typename std::enable_if<std::is_arithmetic<T>::value, bool> :: type
    not_a_number(T const &_in) { return @ISNAN_VARIATION@(_in); }