读书人

1.IND 1.INF nan inf 1.QNAN SNaN 有关

发布时间: 2013-03-17 13:48:31 作者: rapoo

1.#IND 1.#INF nan inf 1.#QNAN SNaN 问题的解决

内容整理自网络

进行浮点数编程时,如果没有注意,常常会出现输出类似 1.#IND, 1.#INF 或者 nan, inf 之类奇怪的输出。这通常隐含了浮点数操作的异常。

特殊浮点数的含义
1.#INF / inf:这个值表示“无穷大 (infinity 的缩写)”,即超出了计算机可以表示的浮点数的最大范围(或者说超过了 double 类型的最大值)。例如,当用 0 除一个整数时便会得到一个1.#INF / inf值;相应的,如果用 0 除一个负整数也会得到 -1.#INF / -inf 值。

-1.#IND / nan:这个的情况更复杂,一般来说,它们来自于任何未定义结果(非法)的浮点数运算。"IND"是 indeterminate 的缩写,而"nan"是 not a number 的缩写。产生这个值的常见例子有:对负数开平方,对负数取对数,0.0/0.0,0.0*∞, ∞/∞ 等。

简而言之,如果遇到 1.#INF / inf,就检查是否发生了运算结果溢出除零,而遇到 1.#IND / nan,就检查是否发生了非法的运算。

特殊浮点数的判断
很多 C 库都提供了一组函数用来判断一个浮点数是否是无穷大或 NaN。int _isnan(double x) 函数用来判断一个浮点数是否是 NaN,而 int _finite(double x) 用以判断一个浮点数是否是无穷大。

你可能已经注意到了,上面两个函数都是以下划线开头的,因此在可移植性上可能是存在问题的,那么如何实现一个通用的判断版本呢?首先,对于 Nan,可以用下面的代码实现:

bool IsNumber(double x)
{
// 这里的比较操作看上去总是会得到 true
// 但有趣的是对于 IEEE 754 浮点数 NaN 来说总会得到 false!
return (x == x);
}
而下面的代码可以判断一个浮点数是否是有限的(finite, 即既不是 NaN 又不是 infinite):

bool IsFiniteNumber(double x)
{
return (x <= DBL_MAX && x >= -DBL_MAX);
}
其中,DBL_MAX 是 <float.h> 中预定义的常量。

把上面两个函数结合起来,还可以实现一个浮点数是否是 Inf 的判断。

转自:http://live.aulddays.com/tech/10/double-float-ind-nan-inf/


此处的1.#QNAN是一个打印呈现,QNAN是指Quiet Not a Number,类似的浮点错误还有SNaN(Signaling Not a Number),通常如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 1s and a non-zero fraction. There are two categories of NaN: QNaN (Quiet NaN) and SNaN (Signalling NaN).
A QNaN is a NaN with the most significant fraction bit set. QNaN’s propagate freely through most arithmetic operations. These values pop out of an operation when the result is not mathematically defined.
An SNaN is a NaN with the most significant fraction bit clear. It is used to signal an exception when used in operations. SNaN’s can be handy to assign to uninitialized variables to trap premature usage.
Semantically, QNaN’s denote indeterminate operations, while SNaN’s denote invalid operations. If a return value is a QNaN, it means that it is impossible to determine the result of the operation, a SNaN means that the operation is invalid

IEEE floating-point exceptions in C++

This page will answer the following questions.

读书人网 >其他相关

热点推荐