How to test for NaN? [SOLVED]

PraxisPraxis Posts: 247

In DS v4.10.0.123, a test for Number.NaN always seems to return false.  Am I missing something?:

// Script to test Number.NaN// Define an anonymous function;// serves as our main loop,// limits the scope of variables(function(){  print( ' ' );  print( App.longVersionString );             // 4.10.0.123  print( ' ' );  var X;  X = 1 / 0;  print( 'X=1/0', X );                        // Infinity  print( X == Number.NaN               );     // false  print( X == Number.POSITIVE_INFINITY );     // true       <---OK  print( X == Number.MAX_VALUE         );     // false  print( X == Number.NEGATIVE_INFINITY );     // false  print( X == Number.MIN_VALUE         );     // false  print( ' ' );  X = Math.sqrt( -1 );  print( 'X=sqrt(-1)', X );                   // NaN  print( X == Number.NaN               );     // false      <---!!! Should be true  print( X == Number.POSITIVE_INFINITY );     // false  print( X == Number.MAX_VALUE         );     // false  print( X == Number.NEGATIVE_INFINITY );     // false  print( X == Number.MIN_VALUE         );     // false  print( ' ' );  X = Number.NaN;  print( 'X=NaN', X );                        // NaN  print( X == Number.NaN               );     // false      <---!!! Should be true  print( X == Number.POSITIVE_INFINITY );     // false  print( X == Number.MAX_VALUE         );     // false  print( X == Number.NEGATIVE_INFINITY );     // false  print( X == Number.MIN_VALUE         );     // false  print( ' ' );// Finalize the function and invoke})();

 

Post edited by Praxis on

Comments

  • ChoholeChohole Posts: 33,604

    Global::isNaN()

  • PraxisPraxis Posts: 247
    Chohole said:

    Global::isNaN()

    Thank you for that!

      // Global::isNaN() works OK:  //  Boolean : isNaN( String expression )  //    NB: "String expression" means an ordinary code Expression, NOT a quoted String:  X = Math.sqrt( -1 );  print( isNaN( 'X' )                  );   // true     <---OK  print( isNaN(  X  )                  );   // true     <---OK  var Z = undefined;  print( isNaN( 'Z' )                  );   // true     <---OK  print( isNaN(  Z  )                  );   // true     <---OK  Z = 0;  print( isNaN( 'Z' )                  );   // true     <---OK  print( isNaN(  Z  )                  );   // false    <---OK  print( isNaN( 'Math.sqrt( -1 )' )    );   // true     <---OK  print( isNaN(  Math.sqrt( -1 )  )    );   // true     <---OK  print( ' ' );  // Global::isFinite() is more general, and also works OK:  //  Boolean : isFinite( String expression )  //    NB: "String expression" means an ordinary code Expression, NOT a quoted String:  X = 1 / 0;  print( isFinite( X ) );                   // false    <---OK  print( isFinite( '1/0' ) );               // false    <---OK  print( isFinite( '1/1' ) );               // false    <---OK  X = Math.sqrt( -1 );  print( isFinite( X ) );                   // false    <---OK  print( isFinite( 'Math.sqrt( -1 )' ) );   // false    <---OK  print( isFinite( 'Math.sqrt(  2 )' ) );   // false    <---OK  print( isFinite(  Math.sqrt( -1 )  ) );   // false    <---OK  print( isFinite(  Math.sqrt(  2 )  ) );   // true     <---OK  X = Number.NaN;  print( isFinite( X ) );                   // false    <---OK  X = 0;  print( isFinite( X ) );                   // true     <---OK  print( ' ' );

     

Sign In or Register to comment.