1.2-1. == 10.2-10. 1.2-1. != 10.2-10. 0.2-0. 1.2-1. 10.2-10. 100.2-100. 1000.2-1000. 10000.2-10000. %pylab inline import numpy as np import matplotlib.pyplot as plt # remember that in numpy, unless otherwise specified by the user, numbers are treated as floats # it is good practice though to indicate what numbers you consider as floats by using the decimal point ls = np.logspace(1.,25,25) x = 0.2 # operators act elementwise on array x_tilde = (ls+x)-ls absolute_error = np.abs(x_tilde-x) plt.plot(ls, absolute_error) plt.xscale('log') plt.yscale('log') plt.xlabel('$O(x)$') plt.ylabel('$O(\epsilon)$') plt.title('Absolute Error') plt.show() relative_error = np.abs((x_tilde-x)/x) plt.plot(ls, relative_error) plt.xscale('log') plt.yscale('log') plt.xlabel('$O(x)$') plt.ylabel('$O(\epsilon)$') plt.title('Relative Error') plt.ylim(None, 1.0E1) plt.show() 1.2-1. == 1.2-1. 0*2**7 + 1*2**6 + 1*2**5 + 1*2**4 + 1*2**3 + 0*2**2 + 1*2**1 + 1*2**0 1*2**7 + 1*2**6 + 1*2**5 + 1*2**4 + 0*2**3 + 1*2**2 + 1*2**1 + 0*2**0 plt.plot(ls, relative_error) plt.xscale('log') plt.yscale('log') plt.xlabel('$O(x)$') plt.ylabel('$O(\epsilon)$') plt.title('Relative Error') plt.ylim(None, 1.0E1) plt.show() relative_error_2 = np.abs((x_tilde-x)/ls) plt.plot(ls, relative_error_2) plt.xscale('log') plt.yscale('log') plt.xlabel('$O(x)$') plt.ylabel('$O(\epsilon)$') plt.title('Absolute Error') plt.ylim(None, 1.0E1) plt.annotate('$\epsilon_{machine}$', xy=(1.1E1, 1.1E-16), xycoords='data', xytext=(1E6, 1E-10), textcoords='data', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), fontsize=20 ) plt.show() 1E17*2.220446049250313E-16 1e17 - 0.2 1e17 - 2 1e17 - 20 1*16**4 + 14*16**3 + 0*16**2 + 7*16**1 + 8*16**0 1.2-1. == 10.2-10. np.allclose(1.2-1., 10.2-10., rtol=1e-016, atol=1e-15) v = 1.0E154 v**2 # largest representable number v = 1.0E155 v**2 # overflow 1e-16 + 1 - 1e-16 == 1e-16 - 1e-16 + 1 def is_equal(a, b, rtol): return abs(a-b) < max(a,b)*rtol is_equal(1.2-1., 10.2-10., 1.0E-14) np.sqrt(1.0E-16 + 1.) - 1. # numbers bits bits/byte bytes = 1E6**2 * 64 / 8 kbytes = bytes/1024 mbytes = kbytes/1024 gbytes = mbytes/1024 tbytes = gbytes/1024 print bytes print kbytes print mbytes print gbytes print tbytes epsilon_half_16 = 4.88e-04 epsilon_single_32 = 5.96e-08 epsilon_double_64 = 1.11e-16 order = 100. print order*epsilon_half_16 print order*epsilon_single_32 print order*epsilon_double_64 # numbers bits bits/byte bytes = 1E6**2 * 16 / 8 kbytes = bytes/1024 mbytes = kbytes/1024 gbytes = mbytes/1024 tbytes = gbytes/1024 print bytes print kbytes print mbytes print gbytes print tbytes