Can You guess the output of following code fragment:
Yes, You guessed it right: ArithmeticException
Exception in thread "main" java.lang.ArithmeticException: / by zero at traps.Test.main(Test.java:3)
Now guess the Output of :
Did you guessed it right ?
What is NaN?
“NaN” stands for “not a number”. “Nan” is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Finding out the square root of a negative number too is undefined.
NaN NaN NaN
In javadoc, the constant field NaN is declared as following in the Float and Double Classes respectively.
public static final float NaN = 0f / 0f; public static final double NaN = 0d / 0d;
How to Compare NaN Values?
All numeric operations with NaN as an operand produce NaN as a result. Reason behind this is that NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false.
- The numerical comparison operators <, <=, >, and >= always return false if either or both operands are NaN.(§15.20.1)
- The equality operator == returns false if either operand is NaN.
- The inequality operator != returns true if either operand is NaN . (§15.21.1)
Check if equal :false Check if UNequal: true Check if equal: false Check if UNequal: true false false
This method returns true if the value represented by this object is NaN; false otherwise.
NaN = true -Infinity = false
Floating type doesn’t produces Exception while operating with mathematical values
IEEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately.
Java is following known math facts. 1.0 / 0.0 is infinity, but the others are indeterminate forms, which Java represents as NaN (not a number).
Infinity -Infinity Infinity
This article is contributed by Pankaj kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Java Math sin() method with Examples
- Working with JAR and Manifest files In Java
- Why to use char array over a string for storing passwords in Java?
- Generating Password and OTP in Java
- Java.io.FilterOutputStream Class in Java
- Constructors in Java
- Reading from a URL using URLConnection Class
- ArrayList to Array Conversion in Java : toArray() Methods
- Date class in Java (With Examples)
- Fast I/O in Java in Competitive Programming
- Variable Arguments (Varargs) in Java
- Collections in Java
- BigInteger Class in Java
- 10 Most asked Questions from Java Programmers
- Does Java support goto?