Logo

Programming-Idioms

  • C++
  • Python

Idiom #285 Set variable to NaN

Given two floating point variables a and b, set a to a to a quiet NaN and b to a signalling NaN. Use standard features of the language only, without invoking undefined behavior.

from decimal import Decimal
a, b = Decimal('NaN'), Decimal('sNaN')
a = float('nan')

Python makes no effort to distinguish signaling NaNs from quiet NaNs, and behavior for signaling NaNs remains unspecified. Typical behavior is to treat all NaNs as though they were quiet.
#include <limits>
a = std::numeric_limits<float>::quiet_NaN();
b = std::numeric_limits<float>::signaling_NaN();
    use, intrinsic :: ieee_arithmetic, only: IEEE_Value, IEEE_QUIET_NAN, IEEE_SIGNALING_NAN
 a = ieee_value (a, IEEE_QUIET_NAN)
 b = ieee_value (b, IEEE_SIGNALING_NAN)

New implementation...
< >
tkoenig