Monday, August 1, 2011

Negative Zero Vs Positive Zero

            Incase if you crossed your SCJP certification, you might aware of negative zero. Otherwise I don’t think we often refer on negative zero in our programming. But it’s one of the strangest stuff within a language.

           As per wiki, IEEE floating point specification differentiates between positive zero and negative zero. But as per Java specifications positive zero and negative zero are same.

   System.out.println(-0.0 == +0.0);  //Prints true

      But if we go little inside java behaves differently between positive zero and negative zero. According to java negative zero is lesser than zero and positive zero is greater than zero.


We can easily prove this with max () and min () functions from java.lang.Math functions,

  String positive = "+0";
      double doubleValue = 11.11;
     
      double dNegative = doubleValue * -0.0;
      double dPositive = Double.parseDouble(positive);
      double dZero = doubleValue * 0.0;

      System.out.println(dZero);                            // 0.0
      System.out.println(dPositive);                        // 0.0
      System.out.println(dNegative);                        // -0.0
      System.out.println(Math.min(dZero, dNegative));       // -0.0
      System.out.println(Math.min(dNegative, dPositive));   // -0.0
      System.out.println(Math.min(dZero, dPositive));       // 0.0
      System.out.println(Math.max(dZero, dNegative));       // 0.0
      System.out.println(Math.max(dNegative, dPositive));   // 0.0
      System.out.println(Math.max(dZero, dPositive));       // 0.0

Negative zero is applicable only with double, its invalid with integers,

  System.out.println();
      System.out.println((-0) * 1);                         // 0
      System.out.println((-0) + 1);                         // 1
      System.out.println((-0) - 1);                         // -1
      System.out.println((-0) / 1);                         // 0
      System.out.println(0 * (-1.1));                       // -0.0
      System.out.println(0 + (-1.1));                       // -1.1
      System.out.println(0 - (-1.1));                       // 1.1
      System.out.println(0 / (-1.1));                       // -0.0

Positive zero and negative zero are equal by primitives and not equal by object,

  System.out.println(dPositive == dNegative);           //true
      System.out.println(dPositive > dNegative);            //false
      System.out.println(dPositive < dNegative);            //false
      System.out.println(Double.compare(dPositive, dNegative));     // 1

      Double DPositive = new Double(dPositive);
      Double DNegative = new Double(dNegative);
      System.out.println(DPositive.equals(DNegative));      //false

Like positive zero and negative zero we have positive infinite and negative infinite also,

   System.out.println(1 / (0.0));        //Infinity
        System.out.println(1 / (-0.0));       //-Infinity

So what’s the conclusion? Ball is in your court. 

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...