NaN就是一个常量值,表示某一个对象是非数量而已。不过,这个值只能存在于double,float这些浮点类型变量的值中。
下面是转载的文章,估计足以说明这个主题:
今天调试程序时,意外的发现java的浮点数中也有NaN,留下来做个记录。
- /**
- * 测试java浮点数中存在的NaN
- * @author ayis
- *
- * Feb 23, 2009
- */
- public class TestNaN {
- public static void main(String args[]){
- // 在double 和 float 类型中,有NaN值
- double a =0 , b= 0 ,c;
- c = a/(a+b);
- System.out.println(c);
- // 在int 和 long 类型中,没有NaN值,解除注释,此处会抛异常 --- / by zero
- // int a =0 , b= 0 ,c;
- // c = a/(a+b);
- // System.out.println(c);
- // NaN与任何数比较均返回false
- if( (0 > c) || (0 == c) || (0 < c)){
- System.out.println("NaN compared with 0 is not always false.");
- }else{
- System.out.println("NaN compared with 0 is always false!");
- }
- }
- }
/**
* 测试java浮点数中存在的NaN
* @author ayis
*
* Feb 23, 2009
*/
public class TestNaN {
public static void main(String args[]){
// 在double 和 float 类型中,有NaN值
double a =0 , b= 0 ,c;
c = a/(a+b);
System.out.println(c);
// 在int 和 long 类型中,没有NaN值,解除注释,此处会抛异常 --- / by zero
// int a =0 , b= 0 ,c;
// c = a/(a+b);
// System.out.println(c);
// NaN与任何数比较均返回false
if( (0 > c) || (0 == c) || (0 < c)){
System.out.println("NaN compared with 0 is not always false.");
}else{
System.out.println("NaN compared with 0 is always false!");
}
}
}