Below is the syntax highlighted version of Equality.java
from §9.1 Scientific Computation.
/****************************************************************************** * Compilation: javac Equality.java * Execution: java Equality * * Not all integers can be represented exactly using floating point. * * % java Equality * 16777217 * 16777216.00000000000 * 9007199254740993 * 9007199254740992.00000000000 * ******************************************************************************/ import java.text.DecimalFormat; public class Equality { public static void main(String[] args) { DecimalFormat df = new DecimalFormat("0.00000000000"); int x = 16777217; // 2^24 + 1 float y = 16777217; StdOut.println(x); StdOut.println(df.format(y)); StdOut.println(x == y); StdOut.println(x == (int) y); StdOut.println(); long v = 9007199254740993L; // 2^53 + 1 double w = 9007199254740993L; StdOut.println(v); StdOut.println(df.format(w)); StdOut.println(v == w); StdOut.println(v == (long) w); } }