Logo

Programming-Idioms

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.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
#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)
local a = 0/0
math
a := NaN;
use feature 'say';
# built-in support (use POSIX not needed))
my $a = 'nan';

say $a;
# prints string value: nan

say 0 + $a;
# prints numeric value: NaN

use feature 'say';
use POSIX qw(:nan_payload nan isnan issignaling getpayload);
say isnan($a) ? 'true' : 'false';;
# prints true

say $a == $a ? 'true' : 'false';
# prints false because NaN does not equal NaN

say issignaling($a) ? 'true' : 'false';
# prints false because $a is non-signaling

my $b = nan(999);  # set to signaling NaN by adding a payload

say $b;
# prints NaN

say getpayload($b);
# prints 999
a = float('nan')
a = Float::NAN
let a: f64 = f64::NAN;