# Floating Point and the Harmonic Series¶

You may know from math that $$\sum_{n=1}^\infty \frac 1n=\infty.$$ Let's see what we get using floating point:

In :
import numpy as np

In :
n = int(0)

float_type = np.float32

my_sum = float_type(0)

while True:
n += 1
last_sum = my_sum
my_sum += float_type(1 / n)

if n % 200000 == 0:
print("1/n = %g, sum0 = %g"%(1.0/n, my_sum))


1/n = 5e-06, sum0 = 12.7828
1/n = 2.5e-06, sum0 = 13.4814
1/n = 1.66667e-06, sum0 = 13.8814
1/n = 1.25e-06, sum0 = 14.1666
1/n = 1e-06, sum0 = 14.3574
1/n = 8.33333e-07, sum0 = 14.5481
1/n = 7.14286e-07, sum0 = 14.7388
1/n = 6.25e-07, sum0 = 14.9296
1/n = 5.55556e-07, sum0 = 15.1203
1/n = 5e-07, sum0 = 15.311
1/n = 4.54545e-07, sum0 = 15.4037
1/n = 4.16667e-07, sum0 = 15.4037
1/n = 3.84615e-07, sum0 = 15.4037
1/n = 3.57143e-07, sum0 = 15.4037

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-3-e529b3a294bb> in <module>()
8     n += 1
9     last_sum = my_sum
---> 10     my_sum += float_type(1 / n)
11
12     if n % 200000 == 0:

KeyboardInterrupt: 
In :