ITeye博客

java 原来也有NaN

ayis · 2009-02-23 19:34 · java · 编程语言 · Markdown

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

评论(1)

  1. #1 楼 · yywill · 2009-02-24 03:42

    本来就有 java虚拟机规范中写得很清楚的。

评论功能维护中,当前仅展示历史评论。