/****************************************************************************** * Compilation: javac CauchySchwartz.java * Execution: java CauchySchwartz x y * * Tests whether the special case of the Cauchy-Schwartz identity * is true for x and y. * * 2 * (x*x + y*y) >= (x + y) * (x + y) * or * (x*x + y*y) >= 2 * (x * y) * * Surprisingly, it is not always true with floating point numbers! * * % java CauchySchwartz 2.5 3.0 * true * true * * % java CauchySchwartz 0.5 0.51 * true * true * * % java CauchySchwartz 0.5 0.5 * true * true * * % java CauchySchwartz 0.5000000000000002 0.5000000000000001 * false * true * % java CauchySchwartz 0.21 0.2100000000000001 * false * false * * % java CauchySchwartz 0.92 0.9200000000000002 * false * true * * % java CauchySchwartz 0.9200000000000002 .92 * false * true * ******************************************************************************/ public class CauchySchwartz { public static void main(String[] args) { double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); StdOut.println(x); StdOut.println(y); double a = 2 * (x*x + y*y); double b = (x + y) * (x + y); StdOut.println(a >= b); double c = x*x + y*y; double d = 2 * (x * y); StdOut.println(c >= d); } }