java中nan_java中的NaN | 学步园

NaN就是一个常量值,表示某一个对象是非数量而已。不过,这个值只能存在于double,float这些浮点类型变量的值中。

下面是转载的文章,估计足以说明这个主题:

今天调试程序时,意外的发现java的浮点数中也有NaN,留下来做个记录。

Java代码 ec0e6b7c3208f1ffc984ae6158fb61ed.gif9a5b51de231859e921decb7c5189fc7a.png

0714d0bea9c1495506bd90aa83cc5db1.gif

/**

* 测试java浮点数中存在的NaN

* @author ayis

*

* Feb 23, 2009

*/

publicclassTestNaN {

publicstaticvoidmain(String args[]){

// 在double 和 float 类型中,有NaN值

doublea =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!");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值