I am attempting to compare the numerical output of a MATLAB script to that of numpy.
The task, from a homework assignment, is to add the value of 10^-N to itself 10^N times.
The goal is to demonstrate that computers have limitations that will compound in such a calculation. The result should always be 1, but as N increases, the error will also increase. For this problem, N should be 0, 1, 2, ..., 8.
My MATLAB script runs quickly with no issues:
solns = zeros(1, 9);
for N = 0:8
for ii = 1:(10^N)
solns(N+1) = solns(N+1) + 10^-N;
end
end
However, using Python 3.8.12 on an Anaconda installation with numpy, this code will not terminate (See Update, code does execute):
import numpy as np
solns = np.zeros((9, 1))
for N in range(len(solns)):
for _ in range(10**N):
solns[N] += (10**-N)
Is there an error in the Python code or is there some significance difference between the languages that creates this result.
EDIT:
I let the python code run for awhile and it eventually terminated after 230 seconds. The whole array printed out as just [1., 1., 1., etc.].
For reference, using the tic toc commands on MATLAB, the program was executed in 0.155760 seconds