Search code examples
c++doublecross-platformnan

Is std::numeric_limits<double>::quiet_NaN() cross platform safe?


In a c++ app I've created for an armhf board I get the gps longitude and latitude decimal values and store it to double variables. For the time being I initialize these variable to an invalid value (255).
However I came across that by using std::numeric_limits one can assign a double variable to std::numeric_limits<double>::quiet_NaN().
Regarding the nan cross platform issues I read the quite useful link Should I use floating point's NaN, or floating point + bool for a data set that contains invalid values?
On the other hand std::numeric_limits is a pure c++11 standard but I was wondering if someone has experienced any problems when porting this kind of code in different platforms.
For example if I move my code from a hard to soft floating or even an amd64 platform will the above initialization cause any problems that should be handled with OS preprocessor directives?


Solution

  • From cppreference: "The value of std::numeric_limits::has_quiet_NaN is true for all types T capable of representing the special value "Quiet Not-A-Number". This constant is meaningful for all floating-point types and is guaranteed to be true if std::numeric_limits::is_iec559 == true."

    You would need to check for the soft floating point library or FPU to be IEEE 754 (IEC 559) compliant, and also that you use the correct compiler flags to enable their use see: gcc-flags for floating point.