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:
- Split log by date in log4j2 of spring boot
- [Solved] Log4j2 log startup error: javax.xml.parsers.ParserConfigurationException…
- [Solved] could not open error log file: CreateFile() “logs/error.log“ fail
- Frequent log swiping after Nacos client starts [How to Solve]
- log4j:WARN Error during default initialization [How to Solve]
- How to Fix log4j2 warning: warn unable to identify org.fusesource.jansi.WindowsAnsiOutputStream
- ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration…
- How to Solve Log4j 2.5 upgrade to 2.15 error
- [Solved] Artifact spbjh:war exploded: Error during artifact deployment. See server log for details.
- [Solved] Artifact AServletTestCode:war exploded: Error during artifact deployment. See server log for details
- [Solved] Tomcat Start Project Error: Artifact xxxxxx:war exploded: Error during artifact deployment. See server log
- Tensorflow Error TypeError: Fetch argument XXXX has invalid typeXXXX,must be a string or Tensor
- [Solved] Error getting generated key or setting result to parameter object. UnsupportedOperationException
- Gephi cannot find Java 1.8 or higher [How to Solve]
- Java Running Error: Could not find or load main class
- Monitoring session to determine whether the user is online or not
- Springboot uses Oracle database to report property ‘sqlsessionfactory’ or ‘sqlsessiontemplate’ are required
- How to Check the appPackage or appActivity of an APK
- Java error: unable to find or load main class (package name in source file)
- mybatis “case when” Error: Failed to process, please exclude the tableName or statementId.