码迷,mamicode.com
首页 > 其他好文 > 详细

-INFINITY的一个坑

时间:2015-04-29 07:10:06      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

float a=INFINITY;

if(a==INFINITY){

  cout<<"a is inf"<<endl;

}else{

  cout<<"a is not inf"<<endl;

}

输出结果为:a is inf 

没问题。

但是:

float b=-INFINITY;

if(b==-INFINITY){

  cout<<"b is -inf"<<endl;

}else{

  cout<<"b is not -inf"<<endl;

}

输出结果为:b is not -inf

原因是-INFINITY对于float其实是溢出的。

所以永远不要用-INFINITY,如果已经大量使用,可批量将程序中所有-INFINITY替换成-0.5*INFINITY。

-INFINITY的一个坑

标签:

原文地址:http://www.cnblogs.com/wantnon/p/4464562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!