---
title: "java 原来也有NaN"
author: "ayis"
date: "2009-02-23T19:34:38Z"
tags: []
categories: ["编程语言"]
slug: "java-nan"
draft: false
lastmod: "2009-02-23T19:34:38Z"
id: 337482
url: "https://ayis.iteye.com/blog/337482"
---

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