Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

Idiom #270 Test for quiet or signaling NaN

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."

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.");

Similar to other coding languages, a `NaN` value will never equal itself.
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.");

"... The differences between the two kinds of NaN are generally not visible in Java. Arithmetic operations on signaling NaNs turn them into quiet NaNs with a different, but often similar, bit pattern."
if (r1.isNaN) {
  print("This is a quiet NaN.");
} else {
  print("This is a number.");
}

Dart has no concept of signaling NaN. NaN are all quiet AFAIK.

New implementation...
< >
tkoenig