Java I! = I + 1? You are wrong.

Source: Internet
Author: User

Today, the instructor asked 1/0, 1.0/0, 1/0. 0, 1.0/0.0 I still have no details ~~~~ Take a good look .......!!!!!!!!!!!!!!!!!!!!!

1. I = I + 1?

A number will never add 1 to itself? Java enforces the use of IEEE 754 Floating Point Number Arithmetic Operations [IEEE 754], which allows you to use a double or float to represent infinity. As we learned in school, the infinite plus 1 is still infinite.

You can use any floating-point arithmetic expression that is computed as infinity to initialize I. For example:

Double I = 1.0/0.0;

However, you 'd better use the constants provided 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 is always executed. Any large floating point number can achieve this purpose, for example:

Double I = 1.0e40;

2. I! = I?

Is a number always equal to itself? IEEE 754 Floating Point Arithmetic retains a special value to indicate a number that is not a number [IEEE 754]. This value is Nan (not a number). It is used for all floating point calculations without a good number definition, such as 0.0/0.0. The Specification describes,NaN is not equal to any floating point value, including itself[JLS].

You can use any floating-point arithmetic expression that calculates NaN to initialize I. For example:

Double I = 0.0/0.0;

Similarly, to be clear, you can use the constants provided by the standard class Library:

Double I = Double. NaN;

NaN has other amazing features.For any floating-point operation, if one or more of its operands is NaN, the result is NaN.. This rule is very reasonable, but it has strange results. For example, the following program prints false:

Class Test {

Public static void main (String [] args ){

Double I = 0.0/0.0;

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

}

}

In short, float and double types both have a special NaN value, which is used to indicate the number of numbers.

3. If NaN is compared with any number, false is returned. 

If (0> c) | (0 = c) | (0 <c )){
System. out. println ("NaN compared with 0 is not always false .");
} Else {
System. out. println ("NaN compared with 0 is always false! ");
}

Note:

Double. Nan = double. Nan. The result isFalse.However,

Double A = new double (double. Nan );

Double B = new double (double. Nan);]

A. Equals (B );// True

4. Float. Compare ()

When we use the float. Compare () method to compare two Nan, we will get the same result. You can use the following code for verification:

Float Nan = float. Nan;
Float anothernan = float. Nan;
System. Out. println (float. Compare (Nan, anothernan ));


If the compare () method returns 0, it means that the two numbers are equal, and-1 is returned, it means that the first one is smaller than the second, and 1 is the opposite.
The returned result of the preceding statement is 0.
In general, the compare () method of the basic type has the same effect as the result of using = directly. However, the advantage and disadvantage of Nan are different, depends on what the user expects. When the semantics of the program requires that two nan should not be considered equal (for example, Nan is used to represent two infinity, and friends who have learned advanced mathematics remember that the two Infinity seem to have the same symbol, but it should not be considered equal.) Use = to judge. If Nan is not important (after all, I only care about numbers, the two items that are not numbers belong to the same category.) use float. compare ().

Another floating point number that is inconsistent in the = and compare () methods is positive 0 and negative 0 (of course, this is also an old difficulty for computers to represent signed numbers) humans 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 results are true and-1. As you can see, = indicates that positive 0 is equal to negative 0, while compare () indicates that positive 0 is greater than negative 0. Therefore, for the comparison of 0, = is a better choice.

More interesting:

double i = 1.0 / 0;                System.out.println(i);             //InfinitySystem.out.println(i + 1);         //InfinitySystem.out.println(i == i + 1);    //truei = 0.0 / 0;System.out.println(i);             //NaNSystem.out.println(i + 1);         //NaNSystem.out.println(i == i + 1);    //false

Related Article

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.