""" Exercise 2.19 from "A primer on..." Demonstrate the impact of roundoff error by taking n repeated square roots followed by n squares. This should bring back the same number, but for large values of n the result is destroyed by roundoff error. """ from numpy import sqrt for n in range(1, 60): r = 2.0 for i in range(n): r = sqrt(r) for i in range(n): r = r**2 print(f'{n} times sqrt and **2: {r:.16f}') """ Terminal> python repeated_sqrt.py 1 times sqrt and **2: 2.0000000000000004 2 times sqrt and **2: 1.9999999999999996 3 times sqrt and **2: 1.9999999999999996 4 times sqrt and **2: 1.9999999999999964 ... 45 times sqrt and **2: 1.9887374575497223 46 times sqrt and **2: 1.9887374575497223 47 times sqrt and **2: 1.9887374575497223 48 times sqrt and **2: 1.9887374575497223 49 times sqrt and **2: 1.8682459487159784 50 times sqrt and **2: 1.6487212645509468 51 times sqrt and **2: 1.6487212645509468 52 times sqrt and **2: 1.0000000000000000 53 times sqrt and **2: 1.0000000000000000 54 times sqrt and **2: 1.0000000000000000 55 times sqrt and **2: 1.0000000000000000 56 times sqrt and **2: 1.0000000000000000 57 times sqrt and **2: 1.0000000000000000 58 times sqrt and **2: 1.0000000000000000 59 times sqrt and **2: 1.0000000000000000 """