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–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:33:43 EDT 2022.