Special cases in Java floating-point operations nan and Infinity__java

Source: Internet
Author: User

We know that cosmetic operations in Java do not allow 0 to be the denominator, but are possible in floating-point operations, so there is a special case of Nan and infinity, both of which are illegal floating-point data. 1.Infinity: The numerator is not 0 the denominator is zero, when doing division produces, represents infinity or infinitesimal number.

Double:

public static final Double positive_infinity = 1.0/0.0;

public static final Double negative_infinity = -1.0/0.0;

Float:

public static final Float positive_infinity = 1.0f/0.0f;

public static final Float negative_infinity = -1.0f/0.0f;

2.NAN: The numerator denominator is zero, the illegal type that occurs when dividing is defined as follows:
public static final Double NaN = 0.0d/0.0;

3. Evasion: in practice, the occurrence of Nan and infinity illegal floating-point types are sometimes not caused by our coding, sometimes because of the call of other people's interface or a large number of data intermediate processing need to do division will inevitably encounter this situation, then we have to think of it to evade, static method that needs to be used to double
Double.isnan (v)
Double.isinfinite (v)





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.