Reputation: 55
I don't know why there is a discrepancy between the two lines of code below. The Numpy array (i.e., "load_values") for some reason is slightly off in the calculation? When I replace the Numpy array index with the actual value that is stored (in this case, 1800), I get the correct answer. Also, I replaced the Numpy array with a regular List and it also got the correct answer. It's only when I use the Numpy array that I am getting the calculation slightly off. Any reason why this would be the case? Is it obvious and I'm just not seeing it?
In [27]: alpha[3] + alpha[2] * 1800 + alpha[1] * (1800 ** 2) + alpha[0] * (1800 ** 3)
Out[27]: 1.2057057142857146
In [28]: alpha[3] + alpha[2] * load_values[2] + alpha[1] * (load_values[2] ** 2) + alpha[0] * (load_values[2] ** 3)
Out [28]: 1.2048772097918872
Edit: Here are the alpha and load_values:
In[54]: alpha
Out[54]: array([ 4.24382716e-13, -1.18055556e-09, -6.69194444e-04, 1.64000000e-03])
In[55]: load_values
Out[55]: array([ 600, 1200, 1800, 2400, 3000])
Upvotes: 0
Views: 555
Reputation: 3527
As mentioned in the comments, the root of the issue is an overflow when doing the operation: load_values[2] ** 3
. The default int-type for numpy
appears to be int32
while the standard python int
appears to be at least the numpy.int64
equivalent. You need to use int64
to properly compute your equation. This can be seen here:
# load_values as int32 array (numpy default):
print(type(load_values[2])) # <class 'numpy.int32'>
print(type(1800)) # <class 'int'>
answer_1 = 1800 ** 3
answer_2 = load_values[2] ** 3
print(answer_1) # 5832000000
print(answer_2) # 1537032704
# load_values as int64 array:
load_values = np.array(load_values, dtype=np.int64)
print(type(load_values[2])) # <class 'numpy.int64'>
print(type(1800)) # <class 'int'>
answer_1 = 1800 ** 3
answer_2 = load_values[2] ** 3
print(answer_1) # 5832000000
print(answer_2) # 5832000000
I will leave the bit-by-bit analysis up to you.
Upvotes: 2