1.#QNAN、1.#IND和1.#INF等“无效”浮点数说明及其判断

在GIS视图上发现部分小区不能正常呈现,通过跟踪异常小区发现其所属基站的经纬度坐标都是-1.#QNAN00000000000无效值,导致小区绘制失败,这些小区均属新入网的3G基站,资源数据还没有维护起来,数据库中对应字段为空,经过TUXEDO接口后数据反映为QNAN无效值。在基础数据完善之前,可在SQL取数据时将空值转化为0,或在接收数据时对此类数据作进一步的过滤。
  此处的1.#QNAN是一个打印呈现,QNAN是指Quiet Not aNumber,类似的浮点错误还有SNaN(Signaling Not aNumber),通常如0.0/0.0、给负数开平方根等溢出或无效运算就会产生一个NAN结果的表示值,NaN及两种浮点错误的说明如下:
The value NaN (Not a Number)is used to represent a value that does not represent a real number.NaN’s are represented by a bit pattern with an exponent of all 1sand a non-zero fraction. There are two categories of NaN: QNaN(Quiet NaN) and SNaN (Signalling NaN).
QNaN is a NaN withthe most significant fraction bit set. QNaN’s propagate freelythrough most arithmetic operations. These values pop out of anoperation when the result is not mathematically defined.
An SNaN is a NaN withthe most significant fraction bit clear. It is used to signal anexception when used in operations. SNaN’s can be handy to assign touninitialized variables to trap premature usage.
Semantically, QNaN’s denote indeterminate operations, while SNaN’sdenote invalid operations. If a return value is a QNaN, it meansthat it is impossible to determine the result of the operation, aSNaN means that the operation is invalid.
  这样的特殊浮点数还有INF和IND:INF就是Infinity,表示一个无穷大的数,包括正无穷和负无穷;IND则表示无限小,但不确定。如1.0/0.0会产生一个INF无穷大,-1.0/0.0会产生一个负无穷大。
 
  回到最初的错误上,这种无效数据之所以通过了经纬度有效值判断,是因为常规的浮点范围对这类浮点数无效。我们知道常规的浮点数不能直接判0等于,而如果这个浮点数为NaN,那if(f==f) 会得到false结果。但是利用这个结果来判断一个浮点数是否为NaN并不总是安全的,例如用这个宏定义#defineisnan(x) ((x) != (x)),在某些编译环境下可能就会脱离了你的预期。那如何判断特殊的浮点数呢?各种语言应该都能找到对应的辅助函数或者类方法,在C语言中,可以用float.h中的int_isnan(double x)、int _finite(double x)、int _fpclass(doublex)函数来判断,返回结果代表意义分别为:_FPCLASS_SNAN (Signaling NaN)、_FPCLASS_QNAN(Quiet NaN)、_FPCLASS_NINF (Negative Infinity, –INF)、_FPCLASS_PINF(Positive Infinity,+INF);在C++中,可以用STL中的limits类:numeric_limits::quiet_NaN()、numeric_limits::signaling_NaN()、numeric_limits::infinity()等方法。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值