NegativeZero.java


Below is the syntax highlighted version of NegativeZero.java from §9.1 Scientific Computation.



/******************************************************************************
 *  Compilation:  javac NegativeZero.java
 *  Execution:    java NegativeZero
 *
 *  In Java, x = 0.0 and y = -0.0 are two different representations of zero.
 *  It is true that (x == y). But, (1/x == 1/y) is not true.
 *  
 *  % java Zero
 *  no
 *
 ******************************************************************************/

public class NegativeZero {

   public static void main(String[] args) { 
      double x =  0.0;
      double y = -0.0;
      StdOut.println("x = " + x);
      StdOut.println("y = " + y);
      StdOut.println("(x == y) is " + (x == y));

      double a = 1.0 / x;
      double b = 1.0 / y;
      StdOut.println("1/x = " + a);
      StdOut.println("1/y = " + b);
      StdOut.println("(1/x == 1/y) is " + (a == b));
   }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.