5.2 infinity and Nan

Source: Internet
Author: User
5.2 infinity and Nan

IEEE 754 floating point numbers can represent positive or negative infinity, andNan(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 = & infin; log (0) =-& infin; 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 wowould country CT: for example, 2 + & infin; = & infin;, 4/& infin; = 0, atan (& infin;) = & PI; /2. nan, on the other hand, infects any calculation that involves it. unless the calculation wocould 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 between T itself and Nan, and negative infinity is smaller than all values between T itself and Nan. Nan isUnordered: It is not equal to, greater than, or less than anything,Including itself.X = xIs false if the valueXIs Nan. You can use this to test whether a value is Nan or not, but the recommended way to test for Nan is withIsnanFunction (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 like1.0/0.0.-InfinityRepresents negative infinity.

You can test whether a floating-point value is infinite by comparing it to this macro. However, this is not recommended; You shocould useIsfiniteMacro 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 include math. 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. negative zero behaves identically to zero in all calculations, unless you explicitly test the sign bitSignbitOrCopysign.

 

Http://www.linuxtopia.org/online_books/programming_books/gnu_libc_guide/Infinity-and-NaN.html 

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.