# # Demonstrate round-off error # # Matt Bishop, ECS 10, Fall 2012 # # First, set x to 1.0 and print it x = 0.1 print("This should print the value of x as 0.1:", x) # # Now, add x to itself 10 times and print the value y = x + x + x + x + x + x + x + x + x + x print("This should print 1.0, the value of x added to itself 10 times:", y) # # Now compare it to 1.0 -- note it's not the same! print("Now compare y to 1.0:", y == 1.0) # # And here's why -- go out to 20 decimal places print("The actual value of y, to 20 places:", "%.20f" % y)