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
Read More:
- The precision of decimal calculation with double and float in Java is not accurate
- Explicit and implicit conversion of Java data type
- Java — for loop printing graphics (loop structure)
- [Solved] Initialization of anonymous inner class member variable causes java.lang.stackoverflowerror
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- Java Processbuilder Calls the command error: CreateProcess error = 2
- [Solved] Java Run Error: For input string: “XXX”
- JAVA: How to use the third-party font library method (using the third-party TTF / TTC font library method)
- Java error prompt….. cannot be resolved
- JAVA 8: How to Convert List to Map
- Exception information: jsonmappingexception: out of start_ ARRAY token
- Tensorflow Error TypeError: Fetch argument XXXX has invalid typeXXXX,must be a string or Tensor
- Asynchronous callback case of Java callback function
- Conversion of two data types in Java
- MapReduce running in Hadoop environment with maven
- Idea2022 automatic generate poji error [How to Solve]
- Java callback function implementation case
- Abstract method and static method of java interface
- The number of control threads and concurrency number of concurrent executor of Java starting gun
- Comparison between ArrayList and HashMap