首页 > 代码库 > inf -inf nam
inf -inf nam
http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
20.5.2 Infinity and NaN
IEEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of them, which is sometimes useful. Some examples of calculations that produce infinity or NaN:
1/0 = ∞log (0) = -∞sqrt (-1) = NaN
When a calculation produces any of these values, an exception also occurs; see FP Exceptions.
The basic operations and math functions all accept infinity and NaN and produce sensible output. Infinities propagate through calculations as one would expect: for example, 2 + ∞ = ∞, 4/∞ = 0, atan (∞) = π/2. NaN, on the other hand, infects any calculation that involves it. Unless the calculation would produce the same result no matter what real value replaced NaN, the result is NaN.
In comparison operations, positive infinity is larger than all values except itself and NaN, and negative infinity is smaller than all values except itself and NaN. NaN is unordered: it is not equal to, greater than, or less than anything, including itself. x == x
is false if the value of x
is NaN. You can use this to test whether a value is NaN or not, but the recommended way to test for NaN is with the isnan
function (see Floating Point Classes). In addition, <
, >
, <=
, and >=
will raise an exception when applied to NaNs.
math.hdefines macros that allow you to explicitly set a variable to infinity or NaN.
- Macro: float INFINITY
An expression representing positive infinity. It is equal to the value produced by mathematical operations like
1.0 / 0.0
.-INFINITY
represents negative infinity.You can test whether a floating-point value is infinite by comparing it to this macro. However, this is not recommended; you should use the
isfinite
macro instead. See Floating Point Classes.This macro was introduced in the ISO C99 standard.
- Macro: float NAN
An expression representing a value which is “not a number”. This macro is a GNU extension, available only on machines that support the “not a number” value—that is to say, on all machines that support IEEE floating point.
You can use ‘#ifdef NAN’ to test whether the machine supports NaN. (Of course, you must arrange for GNU extensions to be visible, such as by defining
_GNU_SOURCE
, and then you must includemath.h.)
IEEE 754 also allows for another unusual value: negative zero. This value is produced when you divide a positive number by negative infinity, or when a negative result is smaller than the limits of representation.
inf :infinity (linux) 等同于 #INF:infinity (windows)
nan :not a number 等同于 #IND:indeterminate (windows)
注意:1、inf一般是因为得到的数值,超出浮点数的表示范围(溢出,即阶码部分超过其能表示的最大值);而nan一般是因为对浮点数进行了未定义的操作,如对-1开方。
2、nan==nan 结果是0或false,即不能和nan进行比较,和nan进行比较得到的结果总是false或0。所以可以用函数: int isNumber(double d){return (d==d);}来判断d是否为nan,若d是nan则返回0,否则返回非零值。
3、1.0/0.0等于inf,-1.0/0.0等于-inf,0.0+inf=inf;
4、对负数开方sqrt(-1.0)、对负数求对数(log(-1.0))、0.0/0.0、0.0*inf、inf/inf、inf-inf这些操作都会得到nan。(0/0会产生操作异常;0.0/0.0不会产生操作异常,而是会得到nan)
5、得到inf时就查看是否有溢出或者除以0,得到nan时就查看是否有非法操作。
6、C语言的头文件<float.h>中,有定义的常量DBL_MAX,这个常量表示“能表示出来的最大的双精度浮点型数 值”。<float.h>中还有常量DBL_MIN,DBL_MIN表示可以用规格化表示的最小的正浮点数,但DBL_MIN并不是最小的正 浮点数,因为可以用可以用非规格化浮点数表示的更小。可以用函数:int isFiniteNumber(double d){return (d<=DBL_MAX&&d>=-DBL_MAX);}来判断d是否为一个finite数(既不是inf,又不是 nan(加入d为nan,则d参加比较就会得到false(0)值))。
7、1.0/inf等于0.0。
8、inf是可以与其他浮点数进行比较的,即可以参与<=、>+、==、!=等运算。
下面这几个宏(用宏实现的,使用时跟函数的形式基本相同)是判断一个表达式的结果是否为inf、nan或其他:
头文件:include<math.h>
宏的用法(类似于函数原型):int fpclassify(x);
int isfinite(x);
int isnormal(x);
int isnan(x);
int isinf(x);
具体用法:
1、int fpclassify(x) 用来查看浮点数x的情况,fpclassify可以用任何浮点数表达式作为参数,fpclassify的返回值有以下几种情况。
FP_NAN:x是一个“not a number”。
FP_INFINITE: x是正、负无穷。
FP_ZERO: x是0。
FP_SUBNORMAL: x太小,以至于不能用浮点数的规格化形式表示。
FP_NORMAL: x是一个正常的浮点数(不是以上结果中的任何一种)。
2、int isfinite(x) 当(fpclassify(x)!=FP_NAN&&fpclassify(x)!=FP_INFINITE)时,此宏得到一个非零值。
3、int isnormal(x) 当(fpclassify(x)==FP_NORMAL)时,此宏得到一个非零值。
4、int isnan(x) 当(fpclassify(x)==FP_NAN)时,此宏返回一个非零值。
5、int isinf(x) 当x是正无穷是返回1,当x是负无穷时返回-1。(有些较早的编译器版本中,无论是正无穷还是负无穷,都返回非零值,不区分正负无穷)。
inf -inf nam