今天调试程序时,意外的发现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!");
}
}
}