Logo

Programming-Idioms

Given a floating point number r1 classify it as follows:
If it is a signaling NaN, print "This is a signaling NaN."
If it is a quiet NaN, print "This s a quiet NaN."
If it is not a NaN, print "This is a number."
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
if (r1.isNaN) {
  print("This is a quiet NaN.");
} else {
  print("This is a number.");
}
use, intrinsic:: ieee_arithmetic
  character (len=:), allocatable :: msg
  if (ieee_support_nan(r1)) then
     if (ieee_class(r1) == ieee_quiet_nan) then
        msg = "This s a quiet NaN."
     else if (ieee_class(r1) == ieee_signaling_nan) then
        msg = "This is a signaling NaN."
     else
        msg = "This is a number."
     end if
  else
     msg = "NaNs are not supported."
  end if
  write (*,'(A)') msg
import static java.lang.Float.isNaN;
import static java.lang.System.out;
if (isNaN(r1)) out.println("This s a quiet NaN.");
else out.println("This is a number.");
import static java.lang.Float.isNaN;
import static java.lang.System.out;
if (r1 != r1) out.println("This s a quiet NaN.");
else out.println("This is a number.");
uses sysutils;
  if r1.IsNan then
  begin
   if (TDoubleRec(r1).Data and $4000000000000000) = $4000000000000000 then
     writeln('This is a quiet NaN')
   else
     writeln('This is a signalling NaN');
  end
  else
    writeln('This is a number: ',r1);
use POSIX qw(:nan_payload nan isnan issignaling setpayload);
my @r = (nan, nan, 1.234);

setpayloadsig $r[1],'999';

foreach my $r1 ( @r ) {
    if ( isnan $r1 ) {
        printf "This is a %s NaN\n",
            issignaling($r1) ? 'signaling' : 'quiet';
    } else {
        printf "This is not a NaN: %s\n", $r1;
    }
}

# Output:
# This is a quiet NaN
# This is a signaling NaN
# This is not a NaN: 1.234
import math
if math.isnan(r1):
    print('This is a quiet NaN.')
else:
    print('This is a number.')
from decimal import Decimal
d = Decimal(r1)
if d.is_snan(): print('This is a signaling NaN.')
if d.is_qnan(): print('This s a quiet NaN.')
if not d.is_nan(): print('This is a number.')