# Infinity (Infinity) and Nan in Java

Source: Internet
Author: User

1, i = = i + 1

A number never equals its own plus 1? Java enforcement requires the use of IEEE 754 floating-point arithmetic operations [IEEE 754], which allows you to represent infinity with a double or float. As we learned in school, Infinity plus 1 is infinitely large.

You can initialize I with any floating-point arithmetic expression that is computed as infinity, for example:

Double i = 1.0/0.0;

However, you'd better be able to use the constants provided to you by the Standard class library:

Double i = double.positive_infinity;

In fact, you do not have to initialize I to infinity to ensure that the loop executes forever. Any floating-point number that is large enough to achieve this, such as:

Double i = 1.0e40;

2, I! = i

A number is always equal to its own? IEEE 754 floating-point arithmetic retains a special value used to denote a number that is not a number [IEEE 754]. This value is Nan ("not a number" abbreviation), and for all floating-point calculations that do not have a good numeric definition, such as 0.0/0.0, the value is it. The specification describes that NaN is notequal to any floating-point value, including itself [JLS].

You can initialize I with any floating-point arithmetic expression that evaluates to Nan, for example:

Double i = 0.0/0.0;

Again, for clarity, you can use the constants provided by the standard class library:

Double i = Double.NaN;

There are other amazing places to NaN. any floating-point operation, as long as one or more of its operands is Nan, the result is Nan. This rule is very reasonable, but it has a strange result. For example, the following program will print false:

Class Test {

public static void Main (string[] args) {

Double i = 0.0/0.0;

System.out.println (I-i = = 0);

}

}

In summary, both the float and double types have a special Nan value, which is used to denote a number that is not a number.

3, Nan and any number comparison are returned false

if ((0 > C) | | (0 = = c) | | (0 < C)) {
SYSTEM.OUT.PRINTLN ("NaN compared with 0 are not always false.");
}else{
SYSTEM.OUT.PRINTLN ("NaN compared with 0 are always false!");
}

Note:

Double.NaN = = Double.NaN, the result is false. However,

Double A = new double (Double.NaN);

Double b = new double (Double.NaN);]

A.equals (b); //true

4, Float.compare ()

And when we use the Float.compare () method to compare two Nan, we get an equal result. Can be verified with the following code:

float Nan=float.nan;
float Anothernan=float.nan;
System.out.println (Float.compare (Nan,anothernan));

The Compare () method returns 0, which indicates that the two numbers are equal, and returns-1, which means the first is smaller than the second and returns 1 is the opposite.
The return result of the above statement is 0.
In general, the basic type of compare () method is the same as the effect of using = = directly, but inconsistency on Nan is a benefit and disadvantage, depending on the expectations of the person who uses it. When the semantics of a program require that two Nan should not be considered equal (for example, using Nan to represent two infinity, a friend who has studied higher mathematics remembers that two infinite-looking symbols are the same, but should not be considered equal), use = = to judge; If Nan is seen as insignificant (after all, I only care about the numbers, two is not the number of things to the same class well, use Float.compare ().

Another floating-point number that is inconsistent on the = = and compare () method is positive 0 and minus 0 (which, of course, is a very persistent problem for computers representing signed numbers), and we (Magnum) certainly know that 0.0f and -0.0f should be equal numbers, but try the following code:

float negzero=-0.0f;
float zero=0.0f;
System.out.println (Zero==negzero);
System.out.println (Float.compare (Zero,negzero));

The returned result is true and-1. See, = = think positive 0 and negative 0 are equal, and the Compare () method thinks positive 0 is greater than negative 0. So for a comparison of 0, = = is a better choice.

Infinity (Infinity) and Nan in Java

Related Keywords:
Related Article

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.