Tag Archives: NAN OR INFINITY

[Solved] Log Error: NAN OR INFINITY

Log Error: NAN OR INFINITY

An error is reported in the log today: infinity or Nan. After checking the code, it is found that float performs the division operation /0.

In integer int operations, the divisor cannot be 0. Otherwise, an exception will be reported directly. But what is it like in floating-point arithmetic?Let’s have a look.

1.infinity: infinite

Observe the results of the following outputs:

        double POSITIVE_INFINITY = 1.0/0.0;
		double NEGATIVE_INFINITY = -1.0/0.0;
		System.out.println(POSITIVE_INFINITY);
		System.out.println(NEGATIVE_INFINITY);
		float POSITIVE_INFINITY2 = 1.0f/0.0f;
		float NEGATIVE_INFINITY2 = -1.0f/0.0f;
		System.out.println(POSITIVE_INFINITY2);
		System.out.println(NEGATIVE_INFINITY2);

Output results:

Infinity
-infinity
infinity
-infinity

Take another look at the following examples and output results:

        //Infinite multiplication by 0 results in NAN
		System.out.println(Float.POSITIVE_INFINITY * 0); // output: NAN
		System.out.println(Float.NEGATIVE_INFINITY * 0); // output: NAN

		//Dividing infinity by 0, the result remains the same, or infinity
		System.out.println((Float.POSITIVE_INFINITY/0) == Float.POSITIVE_INFINITY); // output: true
		System.out.println((Float.NEGATIVE_INFINITY/0) == Float.NEGATIVE_INFINITY); // output: true
		
		//Infinite does all operations except multiplying by zero, and the result is still infinite
		System.out.println(Float.POSITIVE_INFINITY == (Float.POSITIVE_INFINITY + 10000)); // output: true
		System.out.println(Float.POSITIVE_INFINITY == (Float.POSITIVE_INFINITY - 10000)); // output: true
		System.out.println(Float.POSITIVE_INFINITY == (Float.POSITIVE_INFINITY * 10000)); // output: true
		System.out.println(Float.POSITIVE_INFINITY == (Float.POSITIVE_INFINITY/10000)); // output: true

To determine whether a floating-point number is infinite, you can use the isinfinite method:

System.out.println(Double.isInfinite(Float.POSITIVE_INFINITY)); // output: true

2.NAN

System.out.println(0.0d/0.0); //NaN

Nan means non numeric. It is not equal to any value, or even to itself. To judge whether a number is a Nan, use the IsNaN method:

System.out.println(Float.NaN == Float.NaN); // output: false
System.out.println(Double.isNaN(Float.NaN)); // output: true

https://blog.csdn.net/susu1083018911/article/details/124993209