In Java computing, you must use a variety of basic data types to add, subtract, multiply, and divide. But if one day you find a function that returns a float, and you print it out and it’s not a decimal, but something like Infinity or NaN, you might get a black question mark. These two values are indeed Java supplied values, and there is nothing wrong with a function that returns float returning them to you. What do these two values mean? Here are the answers:

Infinite Infinity

In the Float class, there are two static constants:

Public static final float POSITIVE_INFINITY = 1.0f / 0.0f; Public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;Copy the code

These two constants one for plus infinity and one for minus infinity. In the calculation, one thing must be paid special attention to:

Infinity plus a number is infinity.

Double infinity = 1.0/0; System.out.println(infinity); //Infinity System.out.println(infinity + 1); //Infinity System.out.println(infinity == i + 1); //trueCopy the code

If you want to initialize a number to infinity, you can initialize it with any floating-point arithmetic expression that evaluates to infinity, or you can use a constant supplied by Float:

Float infinity = 1.0/0.0; float infinity = Float.POSITIVE_INFINITY;Copy the code

Nans that are not numbers

NaN, not a number, for all floating-point calculations that do not have a good number definition, such as 0.0/0.0, the result is NaN. In the Float class, a constant is provided to represent NaN:

Public static final float NaN = 0.0f / 0.0f;Copy the code

Initialize a number as NaN, either with any floating-point arithmetic expression that evaluates to NaN, like Float, or with this constant supplied by Float directly:

Float nan= 0.0/0.0; float nan = Float.NaN ;Copy the code

NaN has some strange properties that need to be kept in mind when calculating:

  1. NaN is not equal to any floating point value, including itself; That is, it returns false when compared to any number.
  2. Any floating-point operation that has one or more of its operands as NaN results in NaN.

Although NaN comparisons with any number return false, using Float.compare() to compare two nans gives equal results. As follows:

Float nan = 0.0/0; System.out.println(nan); //NaN System.out.println(nan + 1); //NaN System.out.println(nan == nan + 1); //false float anan = 0.0/0; System.out.println(Float.compare(nan,anan)); //trueCopy the code

Other constants in Float:

The three constants in the Float class were introduced above, and they are important to be aware of. There are other constants in other floats, but they are not commonly used. Here’s a summary of constants in Float:

  1. Float POSITIVE_INFINITY = 1.0f / 0.0f;Is infinite
  2. Float NEGATIVE_INFINITY = -1.0f / 0.0f; float NEGATIVE_INFINITY = -1.0f / 0.0f;Negative infinity
  3. Float NaN = 0.0f / 0.0f; float NaN = 0.0f / 0.0f;Indicates not a number
  4. float MAX_VALUE = 0x1.fffffeP+127f;Maximum positive value that float can represent: 3.4028235e+38f
  5. Float MIN_NORMAL = 0 x1. 0 p - 126 - f;1.17549435 e-38 f
  6. The float MIN_VALUE = 0 x0. 000002 p - 126 - f;The smallest positive value that float can represent 1.4E-45f
  7. final int MAX_EXPONENT = 127;The maximum index value of a finite float value
  8. final int MIN_EXPONENT = -126;The minimum exponent value of a finite float value
  9. final int SIZE = 32;The number of bits occupied by a float value
  10. final int BYTES = SIZE / Byte.SIZE;The number of bytes occupied by a float value

In addition, a constant of the same meaning exists in the Double class, another floating point class provided by Java, but with a different value.