import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
We discussed in class that we can obtain the range of integers that can be represented exactly as
$$[1,2^{p}]$$
So let's try this using Python (where double precision gives p=53)
# This number is within the range
2**52
# We add one to get the next integer
2**52 + 1
# All good so far, right? Let's check the integer:
2**53
# If I add one, what do you expect us to get?
2**53 + 1
Humm... not what you expected right? Why do you think this happened?
# to find the maximum integer (using 64-bit signed integer)
# uses only 63 bits, since one is reserved for the sign
maxint = 0
for i in range(63):
maxint += 2**i
maxint
A) it won't stop (infinite loop)
B) it will stop when b reaches underflow
C) it will stop when b reaches machine epsilon
D) none of the above
a = 10**4
b = 1.0
i=0
while (a + b > a):
#print("{0:d}, {1:1.16f}". format(i, b))
print(i,b)
b = b / 2.0
i+=1
A) it won't stop (infinite loop)
B) it will stop when b reaches underflow
C) it will stop when b reaches machine epsilon
D) none of the above
a = 1.0
while a > 0.0:
a = a / 2.0
print(a)
#print("% .16e"% (a))
a = 1.0
while a != np.inf:
a = a * 2.0
print(a)
#print("% .16e"% (a))
Let's write the following number (not exacly OFL, but close)
float(2**1023)
# Try the UFL definition
2**(1024)*(1-2**(-53))
# Can we make this work?