Numpy Array Being Rounded? Subtraction Of Small Floats
I am assigning the elements of a numpy array to be equal to the subtraction of 'small' valued, python float-type numbers. When I do this, and try to verify the results by printing
Solution 1:
You're declaring the array with v1 = np.array([0,0,0])
, which numpy assumes you want an int array for. Any subsequent actions on it will maintain this int array status, so after adding your small number element wise, it casts back to int (resulting in all zeros). Declare it with
v1 = np.array([0,0,0],dtype=float)
There's a whole wealth of numpy specific/platform specific datatypes for numpy that are detailed in the dtype docs page.
Solution 2:
You are creating the array with an integer datatype (since you don't specify it, NumPy uses the type of the initial data you gave it). Make it a float:
>>>v1 = np.array([0,0,0], dtype=np.float)>>>v1[0] = pc1x-pc2x>>>print v1
[-0.04401800000000000157 0. 0. ]
Or change the incoming datatype:
>>>v1 = np.array([0.0, 0.0, 0.0])>>>v1[0] = pc1x-pc2x>>>print v1
[-0.04401800000000000157 0. 0. ]
Post a Comment for "Numpy Array Being Rounded? Subtraction Of Small Floats"