Skip to main content

NaN Not a Number puzzle in java

Question ?

What is the output of below code?


public class BridgeOfLooper
{
  public static void main(String[] args)
  {
    double d = 0.0/ 0.0;
    System.out.println( d - 1 == 0);
  }

}

o/p => false

Some might have thought about 'ArithmeticException :Divide by Zero'

Remember that 0.0 is NaN not a Zero.


Explanation : 

  double d = 0.0 / 0.0 Arithmetic expression evaluates to infinity 
  so d becomes NaN
     d = NaN
  
  TechFacts Any floating point operation evaluates to NaN if one or more 
  of its operands are NaN.

  The rules for computing with NaN is that once it generates NaN, a computation is damaged, and no further computation can repair the damage.

   d -1 = NaN which is not equal to 0

Comments