Reputation: 77
I have a NumPy array [545, 32] and I pass a function to it:
def obj_func(array):
x = array[0]
y = array[1]
obj_func = (x**2 + y - 11)**2 + (x + y**2 - 7)**2
return obj_func
The formula should look something like
(545^2 + 32 - 11)^2 + (545 + 32^2 - 7)^2
The answer that I got when I passed it through my function is -1955547256, but the answer should be about 8.82387x10^10.
It probably has something to do with manipulating big numbers, since the smaller values I calculated through my function had the correct answer. But why did it return a negative number, etc.? I know for sure this function should not return any negative numbers.
Upvotes: 0
Views: 84
Reputation: 5949
You might like whether to change type of variables in you array or call obj_func
on list instead:
obj_func(np.array([545, 32]))
>>> -1955547256
obj_func([545, 32])
>>> 88238765960
obj_func(np.array([545, 32], dtype=np.int64))
>>> 88238765960
This effect happens when data of array exceeds maximum machine limit for array.dtype = np.int32
It can be inspected like so:
np.iinfo(np.int32)
>>> iinfo(min=-2147483648, max=2147483647, dtype=int32)
np.iinfo(np.int64)
>>> iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)
np.array([2147483647]) + 1
>>> array([-2147483648]) # shifts to minimum machine limit of dtype=int32
Upvotes: 2