CS 3343/3341 Introduction to Algorithms Spring 2012 |
Problems with Termination |
Java loop | Same loop in C |
---|---|
// Termination.java: program that // terminates after 20 iters // prints: 2.0, 1.9, ..., 0.2, 0.1 public class Termination { public void term() { double x = 2.0; while (x != 0.0) { System.out.println(x); x = x - 0.1; } } public static void main(String[] args) { Termination t = new Termination(); t.term(); } } | // termination.c: program that // terminates after 20 iters // prints: 2.0, 1.9,..., 0.2, 0.1 #include |
Java Output | C Output |
2.0 1.9 1.7999999999999998 1.6999999999999997 1.5999999999999996 1.4999999999999996 1.3999999999999995 1.2999999999999994 1.1999999999999993 1.0999999999999992 0.9999999999999992 0.8999999999999992 0.7999999999999993 0.6999999999999993 0.5999999999999993 0.49999999999999933 0.39999999999999936 0.2999999999999994 0.19999999999999937 0.09999999999999937 | 2.000000 1.900000 1.800000 1.700000 1.600000 1.500000 1.400000 1.300000 1.200000 1.100000 1.000000 0.900000 0.800000 0.700000 0.600000 0.500000 0.400000 0.300000 0.200000 0.100000 |