Inf-inf Nam

Source: Internet
Author: User

http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html20.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 was undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of the 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 the 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 and the result is Nan.

In comparison operations, positive infinity are larger than all values except itself and NaN, and negative infinity are smal Ler than all values except itself and NaN. NaN is unordered: It isn't equal to, greater than, or less than anything, including itself. is x == x False if the value of is x NaN. Can use this to test whether a value are NaN or not, but the recommended-in-the-test for NaN are with the isnan function (see floating point Classes). In addition,,, < > <= , and would >= 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 I Nfinity. 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 was infinite by comparing it to the this macro. However, this is not recommended; You should use the isfinite macro instead. See floating point Classes.

This macro is introduced in the ISO&NBSP;C99 standard.

Macro: float NAN

An expression representing a value which are "not a number". This macro was a GNU extension, available only on machines, the ' not a number ' value-that is-to-say, on all Mac Hines. Support IEEE floating point.

You can use the "to test whether" the machine #ifdef NAN supports NaN. (Of course, must arrange for GNU extensions to is visible, such _GNU_SOURCE as by defining, and then you must include math.h .)

IEEE 754 also allows for another unusual value:negative zero. This value was produced when you divide a positive number by negative infinity, or when a negative result is smaller than t He limits of representation.

Inf:infinity (Linux) equivalent to #INF: Infinity (Windows)

Nan:not a number equals #IND: Indeterminate (Windows)

Note: 1, INF is generally due to the resulting value, exceeding the range of floating point number (overflow, that is, the order portion exceeds the maximum value it can represent), and Nan is generally because the floating-point numbers are undefined operations, such as the 1 root.

2, Nan==nan result is 0 or false, that is, can not compare with Nan, and Nan to compare the results are always false or 0. So you can use the 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.

3, 1.0/0.0 equals inf,-1.0/0.0 equals-inf,0.0+inf=inf;

4, the negative root sqrt (-1.0), the negative logarithm (log (-1.0)), 0.0/0.0, 0.0*inf, Inf/inf, inf-inf These operations will be Nan. (0/0 generates an operation exception; 0.0/0.0 does not produce an operation exception, but it gets Nan)

5, when the INF to see if there is overflow or divided by 0, to obtain a nan to see if there is an illegal operation.

6, C language header file <float.h>, there is a defined constant Dbl_max, this constant represents "the largest double-precision floating-point value 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. Functions can be used: 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)).

7, 1.0/inf equals 0.0.

8, INF can be compared with other floating-point numbers, that is, you can participate in <=, >+, = =,! = and other operations.

The following macros (implemented as macros, using the same form as functions) are used to determine whether the result of an expression is INF, Nan, or other:

Header file:include<math.h>

Use of macros (similar to function prototypes): int fpclassify (x);

int isfinite (x);

int isnormal (x);

int isNaN (x);

int Isinf (x);

Specific usage:

1, int fpclassify (x) is used to view the case of floating-point number x, fpclassify can use any floating-point expression as an argument, 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.

The fp_subnormal:x is too small to be represented by the normalized form of a floating-point number.

Fp_normal:x is a normal floating-point number (not any of the above results).

2, int isfinite (x) when (fpclassify (x)!=fp_nan&&fpclassify (x)!=fp_infinite), this macro gets a non-0 value.

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).

Inf-inf Nam

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.