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.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java