This paper summarizes the meaning, generation and judging methods of Nan and INF in C language. Nan in the C language means not a number, which is equivalent to #IND: indeterminate (Windows) generates:
An undefined operation was performed on a floating-point number;
- For negative numbers, the logarithm is calculated for negative numbers, 0.0/0.0,0.0*inf, Inf/inf, Inf-inf, and all of these operations will be Nan. (0/0 will produce an operation exception; 0.0/0.0 will not produce an operation exception, but will get Nan);
- In GNU, use a macro: Float Nan to assign a floating-point number;
Decision: library Function Method: (recommended) < see after > Custom function: int isnumber (double d) {return (d==d);} To determine if D is Nan and if D is Nan returns 0, otherwise a value other than 0 is returned. Attention:
- Nan is unordered (unordered), which is not greater than, less than, or equal to any number (including itself), so the Nan==nan result is 0 or false, and the <,>,<=, and >= acting on Nan produce a exception;
- See if you have illegal operations when you get Nan.
- If an expression contains Nan, the result of the expression is Nan;
- Implementations of Nan are available in two ways: signaling nan and quiet Nan. Signaling Nan is the way to throw an exception, so it doesn't need to define a Nan macro. Quiet nan means that, even if an exception is computed, the exception is not thrown to interrupt the execution of the program, but the result is represented as a special value, so only in this case Nan macro is defined;
The Infinfinity (Linux) in C language is equivalent to #INF: Infinity (Windows) generates:
Exceeding the range of floating point numbers (overflow, that is, the order portion exceeds the maximum value it can represent);
- 1.0/0.0 equals inf,-1.0/0.0 equals-inf,0.0+inf=inf;log (0);
- In C99, use the macro: Float infinity to assign values to floating-point numbers;
Decision: library Function Method: (recommended) < see after > Custom function: int isfinitenumber (double d) {return (D<=dbl_max&&d>=-dbl_max);} To determine if D is a finite number (neither INF nor Nan (with D as Nan, then D will get a value of False (0) if it participates in the comparison)). Attention:
- +inf is greater than any number (except for its own and Nan);-inf is less than any number (except for itself and Nan);
- When you get the INF, see if there is an overflow or divide by 0;
- Header file <float.h>, there is a defined constant Dbl_max, which represents "the largest double-precision floating-point value that can be represented". The <float.h> also has a constant dbl_min,dbl_min that represents the smallest positive floating-point number that can be normalized, but dbl_min is not the smallest positive floating-point number, because it can be smaller with a non-normalized floating-point number;
- In the C language expression, the INF represents an infinite concept in mathematics, such as 1.0/inf equals 0.0, and can be compared with other floating-point numbers (can participate in <=, >+, = =,! =, etc.);
Library function method to determine the INF and nan the following macros (implemented by macros, using the same form as the function) is to determine whether the result of an expression is INF, Nan, or other: header file:include< math.h> Macro usage (similar to function prototypes): int fpclassify (x); &NB Sp int isfinite (x); &N Bsp int isnormal (x); &N Bsp int isNaN (x); &NBSP ; int isinf (x); Specific usage: & nbsp 1, int fpclassify (x) is used to view the case of a floating-point number x, fpclassify can use any floating-point expression as an argument, and the return value of fpclassify has the following conditions. FP_NAN:X is a "not a number". Fp_infInite:x is positive and negative infinity. FP_ZERO:X is 0. fp_subnormal:x is too small to be represented by the normalized form of floating-point numbers. fp_normal:x is a normal floating-point number (not any of the above results). 2, int isfinite (x) (fpclassify (x)!=fp_nan&&fpclassify (x)!=fp_infinit E), this macro gets a value that is not 0. 3, int isnormal (x) When (fpclassify (x) ==fp_normal), this macro gets a non-0 value. 4, int isNaN (x) When (fpclassify (x) ==fp_nan), this macro returns a non-0 value. 5, int isinf (x) when x is positive infinity is return 1, when x is negative infinity returns-1. (Some older compiler versions, whether positive or negative, return a non-0 value, with no distinction between positive and negative infinity). Resources:
- C language inf and Nan (http://blog.sina.com.cn/s/blog_8b745a5f01014ifk.html)
- 20.5.2 Infinity and Nan (http/ www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html)
- http://bbs.csdn.net/topics/70279090